Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (426)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (466)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  downloading files with java problem  (Read 1027 times)
0 Members and 1 Guest are viewing this topic.
Offline kappa
« League of Dukes »

JGO Kernel


Medals: 70
Projects: 15


★★★★★


« Posted 2007-10-05 19:23:53 »

I have an app that downloads a java file using the code below, problem is that it works fine for me and many others but some users get a "Connection timed out: connect" error, not really sure what could be causing this.

One guess is that it might be caused by the the buffer size which is set to 64kb. I have tried a smaller buffer size, like 4kb but found it took a lot longer to download, using the bigger buffer size was much faster, could it be that i need to use a smaller buffer and sacrifice speed?

Not really sure what is the correct way to do this, any help or solution on the best way to do this would be appreciated.

Thanks

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
public void downloadFile(URL url) throws Exception {
     
      // reset percentage
     percentage = 0;

      // create buffer
     byte buffer[] = new byte[65536];
     
      // open connection
     URLConnection urlconnection = url.openConnection();
     
      // get file size
     totalFileSize = urlconnection.getContentLength();

      // get location to save file
     String filename = getFileLocation(url);
     
      InputStream inputstream = urlconnection.getInputStream();
      FileOutputStream fos = new FileOutputStream(filename);
         
      int bufferSize;
     
      while ((bufferSize = inputstream.read(buffer, 0, buffer.length)) != -1) {
         
         fos.write(buffer, 0, bufferSize);
         
         currentFileSize += bufferSize;
         
         percentage = (currentFileSize * 100) / totalFileSize;
      }
}
Offline Mr_Light

Senior Member




shiny.


« Reply #1 - Posted 2007-10-06 01:54:43 »

I haven't looked at the actaul inner workings of the implementation.

but, connection timed out, means in general that it didn't recieve an ack for a specified timeout. this also applies to never have recieved an ack in the first place, in other words there was never a connection.

now you can set a longer time out( http://java.sun.com/javase/6/docs/api/java/net/URLConnection.html#setConnectTimeout(int) ) but under normal circumstances the default should work.

My gues would be that there route to the host is messed (like it beeing absent(no route to host), check if the ppl involved are connected to the internet etc)

Buffer only has a really(really-really) slim chance of effecting the process (mostly though faulty implementations.) I though you got some other exception altogether if the buffer was backed up. but again I must note that I have looked at the implementation specifically.

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Offline Cero
« Reply #2 - Posted 2009-04-08 00:26:37 »

do you still use this code ? any developments since then regarding this problem ?

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline kappa
« League of Dukes »

JGO Kernel


Medals: 70
Projects: 15


★★★★★


« Reply #3 - Posted 2009-04-08 10:44:46 »

wow pretty old post revived, I did eventually find a solution to this, what I did was wait for a specific amount of time, if a connection was not received in that time, then kill the connection and try again.

I believe this was an old bug in the jre that was supposed to be fixed, however it still somehow happens with the Opera Java Plugin.

You can find my final solution to this here in method downloadJars(String path).
Offline Cero
« Reply #4 - Posted 2009-04-08 18:38:54 »

ah ok, thx =D

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (75 views)
2014-04-15 18:08:23

BurntPizza (68 views)
2014-04-15 03:46:01

UprightPath (80 views)
2014-04-14 17:39:50

UprightPath (65 views)
2014-04-14 17:35:47

Porlus (81 views)
2014-04-14 15:48:38

tom_mai78101 (105 views)
2014-04-10 04:04:31

BurntPizza (165 views)
2014-04-08 23:06:04

tom_mai78101 (261 views)
2014-04-05 13:34:39

trollwarrior1 (210 views)
2014-04-04 12:06:45

CJLetsGame (220 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!