downloading files with java problem

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

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;
		}
}

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.

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

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).

ah ok, thx =D