Return-Path: Delivered-To: apmail-hc-httpclient-users-archive@www.apache.org Received: (qmail 96799 invoked from network); 23 May 2008 09:44:38 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 23 May 2008 09:44:38 -0000 Received: (qmail 58862 invoked by uid 500); 23 May 2008 09:44:32 -0000 Delivered-To: apmail-hc-httpclient-users-archive@hc.apache.org Received: (qmail 58842 invoked by uid 500); 23 May 2008 09:44:32 -0000 Mailing-List: contact httpclient-users-help@hc.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "HttpClient User Discussion" Delivered-To: mailing list httpclient-users@hc.apache.org Received: (qmail 58825 invoked by uid 99); 23 May 2008 09:44:32 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 23 May 2008 02:44:31 -0700 X-ASF-Spam-Status: No, hits=1.4 required=10.0 tests=SPF_NEUTRAL,WHOIS_MYPRIVREG X-Spam-Check-By: apache.org Received-SPF: neutral (nike.apache.org: local policy) Received: from [217.150.250.44] (HELO ok2consulting.nine.ch) (217.150.250.44) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 23 May 2008 09:43:35 +0000 Received: by ok2consulting.nine.ch (Postfix, from userid 1002) id 16D211983F2; Fri, 23 May 2008 11:43:55 +0200 (CEST) Received: from [10.14.1.179] (unknown [213.55.131.1]) by ok2consulting.nine.ch (Postfix) with ESMTP id D89F21983EB for ; Fri, 23 May 2008 11:43:47 +0200 (CEST) Subject: Re: How to download .jar file? (I think that I read most of threads and googled alot..) From: Oleg Kalnichevski To: HttpClient User Discussion In-Reply-To: <17418619.post@talk.nabble.com> References: <17418619.post@talk.nabble.com> Content-Type: text/plain; charset=utf-8 Date: Fri, 23 May 2008 11:19:22 +0200 Message-Id: <1211534362.5997.4.camel@ubuntu> Mime-Version: 1.0 X-Mailer: Evolution 2.22.1 Content-Transfer-Encoding: 8bit X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on ok2consulting.nine.ch X-Spam-Level: X-Virus-Checked: Checked by ClamAV on apache.org X-Old-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=ham version=3.0.3 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 > > > > 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(); > } > } > > > > > 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: httpclient-users-unsubscribe@hc.apache.org > For additional commands, e-mail: httpclient-users-help@hc.apache.org > --------------------------------------------------------------------- To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org For additional commands, e-mail: httpclient-users-help@hc.apache.org