Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

HttpURLConnection hangs downloading big files

 
Maria Rozenfeld
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everybody,

I am having problems with downloading big files (over 150MB) from a
servlet using POST method. Even for files over 125MB I had to use
-Xms and -Xmx options to make it work. The connection is https, but
I do not think that matters. Below is a relevant part of my program.
The process hangs on the line conn.getResponseCode(), but any reference
to connection object after closing the output stream would hang as well.
Any file below 125MB works fine. Files between 125 - 150 work with
bigger heap size. File of 300MB does not work with any heap settings.

Any suggestions would be greately appreciated.

B.T.W. I am running this program on Linux.

URL url = new URL("https://www.server.com/servlet/DirectDownload");

String postData ="uid=" + uid + "&pwd=" + pwd + "&filename=" + fileName + "&reloadflag=" + reloadFlag;

// POST requests are required to have Content-Length

int stringLength = postData.length();

String lengthString = (new Integer(stringLength)).toString();

conn = (HttpsURLConnection) url.openConnection() ;

conn.setRequestProperty
("Content-Length", lengthString);

conn.setRequestMethod("POST");

conn.setRequestProperty
("Content-Type", "application/x-www-form-urlencoded");

conn.setUseCaches(false);


// allow this program to send data to server.

conn.setDoOutput(true);

// allow this program to receive data from server.

conn.setDoInput(true);

conn.connect();

DataOutputStream o = new DataOutputStream(conn.getOutputStream());

o.writeBytes(postData);

o.flush();

o.close();

int responseCode =conn.getResponseCode();

String responseMessage = conn.getResponseMessage();

if (responseCode > 200)
{
System.exit(1);
}


FileOutputStream fos = null;

fos = new FileOutputStream(fullTargetName);

int bytesRead = 0;

byte[] buffer = new byte[bufferSize];

is = conn.getInputStream();

while (true)
{

bytesRead = is.read(buffer);

if (bytesRead == -1)

break;

fos.write(buffer, 0, bytesRead);

totalFileSize = totalFileSize + bytesRead;


}

fos.close();

o.close();

conn.disconnect();

Thanks,

Maria
 
Maria Rozenfeld
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sometimes it gives me "Unexpected end of file from server" error instead
of hanging.

Help!

Maria
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!