hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From JBRyu <Jongbeom....@comparatio.com>
Subject How to download .jar file? (I think that I read most of threads and googled alot..)
Date Fri, 23 May 2008 04:46:30 GMT

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


<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: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Mime
View raw message