Problem downloading rar file using HttpURLConnection

I'm trying to read a rar(archiving) file from the Internet using a HttpUrlConnection.

The program seems to work fine - however the download is not the same size as the file if downloaded through a browser.

The archive is 52,332kb when downloaded through a browser

and 4kb smaller when I try to download it.

When I try to extract the downloaded file (with winRAR) the header is corrupt and there is an unexpected end of archive.

Here is the pertinent code.

try

{

// configure and open connection

HttpURLConnection conn = (HttpURLConnection)currURL.openConnection();// currURL is a URL object

conn.setRequestProperty("Connection","Keep-Alive");

conn.setRequestProperty("Accept-Encoding","gzip,deflate" );

conn.setRequestProperty("Content-type","application/x-rar-compressed" );

conn.setRequestProperty("Cookie", cookie);

conn.connect();

// get an input stream from the HttpURLConnection object

InputStreamReader isr =new InputStreamReader( conn.getInputStream() );

BufferedReader in =new BufferedReader( isr );

/* the string file_name has been parsed from the url

* and is in the format file_name.rar */

File dest =new File("C:\\my_folder\\"+file_name);

// open output stream

OutputStream os =new FileOutputStream( dest );

OutputStream buffer =new BufferedOutputStream( os );

int data;

while ( (data = in.read() ) != -1 )

{

buffer.write(data);

}

// close streams

in.close();

os.close();

}

catch( IOException iox)

{

iox.printStackTrace();

}

Thanks in advance.

[2535 byte] By [simmiea] at [2007-11-26 21:44:52]
# 1
the data response from server is include http header , you should check and remove it to get only your data.
secmaska at 2007-7-10 3:33:04 > top of Java-index,Core,Core APIs...
# 2
That statement is rubbish and in any case it wouldn't explain why his download via this Java code is too short? Please don't post misinformation.@OP: You need to close 'buffer', not 'os'.
ejpa at 2007-7-10 3:33:04 > top of Java-index,Core,Core APIs...
# 3

Thanks ejp. I closed 'buffer' instead - that fixes the size problem.

The archive is now exactly the same size as it should be - but the header information is still corrupt.

When I compared the files using a binary comparison program it

said that the files although the same size were different by approx. 6.7mb out of 53.5mb.

I'm perplexed as to what this difference could stem from; is it the encoding, the charset, something similar?

The underlying file I'm trying to download is an avi if that makes any difference.

simmiea at 2007-7-10 3:33:04 > top of Java-index,Core,Core APIs...
# 4
Use an InputStream not a Reader.
ejpa at 2007-7-10 3:33:04 > top of Java-index,Core,Core APIs...
# 5

That worked - I changed it to the InputStream instead of the Reader:

//...

InputStream is = conn.getInputStream();

BufferedInputStream in = new BufferedInputStream(is);

//...

And now the downloaded archives are extractable. Thanks ejp.

simmiea at 2007-7-10 3:33:04 > top of Java-index,Core,Core APIs...