On Thu, 2008-05-22 at 21:46 -0700, JBRyu wrote: > Hi, > > I have a problem with downloading a .jar file from a HTTP server. I'm now > using httpclient-4.0-alpha4 and httpcore-4.0-beta1 libraries. I can download > any .jar file using a http input stream, but the download .jar file is > corrupted even if the size of the downloaded file is the same as that of the > original file on the HTTP server so I can't decompress it because it is > broken. Please see Case #1 for details. So, I use ZipInputStream class for > reading the compressed file from the HTTP server. However, when I read bytes > from the ZipInputStream, it always returns -1 at the first time and is > hanging there. It seems like that the stream is not broken but dosen't do > anything. Please seee Case #2 for details. > > I spent lots of time but couldn't figure it out. Is there any one who faced > this problem and resolved it? > > Thank you for all your helps. > JB >
I do not see any obvious problems with your code aside from that fact that you should use HttpEntity#getContentLength() method to find out the length of the incoming content instead of parsing 'content-length' header manually. I wrote this small app to test similar scenario and it worked for me. I got perfectly valid jar. ================================== DefaultHttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet( "http://repo1.maven.org/maven2/org/apache/httpcomponents/httpcore/4.0-beta1/httpcore-4.0-beta1.jar"); System.out.println("executing request: " + httpget.getRequestLine()); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { if (entity != null) { System.out.println("File size: " + entity.getContentLength()); File file = new File("local.jar"); InputStream instream = null; OutputStream outstream = null; try { instream = entity.getContent(); outstream = new FileOutputStream(file); byte[] tmp = new byte[4096]; int l; while ((l = instream.read(tmp)) != -1) { outstream.write(tmp, 0, l); } outstream.flush(); } finally { if (instream != null) { instream.close(); } if (outstream != null) { outstream.close(); } } } } else { if (entity != null) { entity.consumeContent(); } } ================================== Oleg > > <Case #1> > > HttpGet httpGet = new HttpGet(httpFilePath); > HttpResponse response = httpClient.execute(httpGet); > int responseCode = response.getStatusLine().getStatusCode(); > > // Checks the HTTP server response code. > if ( responseCode == > Client_HTTPDownloadConstants.HTTP_RESPONSE_CODE_SUCCESSFUL ) { > // Retrieves the target file size from the response header. > Header[] headers = response.getHeaders("Content-Length"); > > // Retrieves the target file size. > int fileSize = Integer.valueOf(headers[0].getValue()); > > // Prepares an input stream to read the source file bytes from the HTTP > server. > InputStream fis = response.getEntity().getContent(); > > > // Prepares a file output stream to write the downloaded bytes to the > local file. > FileOutputStream fos = new FileOutputStream(tmpFilePath) > > // Initializes the written bytes of the current file. > int bytesWritten = 0; > > // Reads the bytes from the HTTP server. > int bytesRead = fis.read(buffer); > > > while (bytesRead != -1) { > // Writes bytes to the local file. > fos.write(buffer, 0, bytesRead); > > // Reads bytes from the local file. > bytesRead = fis.read(buffer); > } > > > // Closes the input and output streams. > if (fos != null) { > fos.flush(); > fos.close(); > } > if (fis != null) { > fis.close(); > } > } > > > <Case #2> > > HttpGet httpGet = new HttpGet(httpFilePath); > HttpResponse response = httpClient.execute(httpGet); > int responseCode = response.getStatusLine().getStatusCode(); > > // Checks the HTTP server response code. > if ( responseCode == > Client_HTTPDownloadConstants.HTTP_RESPONSE_CODE_SUCCESSFUL ) { > // Retrieves the target file size from the response header. > Header[] headers = response.getHeaders("Content-Length"); > > // Retrieves the target file size. > int fileSize = Integer.valueOf(headers[0].getValue()); > > // Prepares an input stream to read the source file bytes from the HTTP > server. > InputStream fis = response.getEntity().getContent(); > > > // Prepares a ZIP input stream. > ZipInputStream zis = new ZipInputStream(fis); > > // Prepares a file output stream to write the downloaded bytes to the > local file. > FileOutputStream fos = new FileOutputStream(tmpFilePath) > > // Initializes the written bytes of the current file. > int bytesWritten = 0; > > // Reads the bytes from the HTTP server. > int bytesRead = zis.read(buffer); > > > while (bytesRead != -1) { > // Writes bytes to the local file. > fos.write(buffer, 0, bytesRead); > > // Reads bytes from the local file. > bytesRead = zis.read(buffer); > } > > // Closes the input and output streams. > if (fos != null) { > fos.flush(); > fos.close(); > } > if (zis != null) { > zis.close(); > } > if (fis != null) { > fis.close(); > } > } > > > > > -- > View this message in context: > http://www.nabble.com/How-to-download-.jar-file--%28I-think-that-I-read-most-of-threads-and-googled-alot..%29-tp17418619p17418619.html > Sent from the HttpClient-User mailing list archive at Nabble.com. > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]