hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Julius Davies" <juliusdav...@cucbc.com>
Subject RE: Getting Images from the http server
Date Sun, 16 Jul 2006 20:45:40 GMT
Hi, Siddharth,

I think the cast to (char) is probably corrupting your image download.  Images are binaries,
so it's best to treat them as bytes and not chars.

I recommend using the (usually) higher performing read( byte[] ) and write( byte[] ) methods
of InputStream and OutputStream.


FileOutputStream out = null;
try
{
  out = new FileOutputStream( "/downloads/image.jpg" );
  byte[] buf = new byte[ 4096 ];
  int r = in.read( buf );
  while ( r >= 0 )
  {
    if ( r > 0 )
    {
      out.write( buf, 0, r );
    }
    r = in.read( buf );
  }
}
finally
{
  try { if ( out != null ) out.close(); } catch ( IOException ioe ) {}
}





yours,

Julius


-----Original Message-----
From:	Siddharth Godbole [mailto:siddharth.m.godbole@iiitb.ac.in]
Sent:	Sun 7/16/2006 9:52 AM
To:	HttpClient List
Cc:	
Subject:	Getting Images from the http server

Hi,

   I wanted to know about how i can get images from the server. Right now the method i used
was like this:

1. Create a GET method for the image and Execute it.
2. Get its input stream 
3. Use while loop as follows:
while((web_data = in.read()) != -1) {
      dataFile.writeOneChar((char)web_data);
      }
4. And then close the file and the connection.

But this is not working. Please can anyone tell me how can i achieve this ?

Regards,
Sid



---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org


Mime
View raw message