You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Siddharth Godbole <si...@iiitb.ac.in> on 2006/07/16 18:52:31 UTC

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

Re: Getting Images from the http server

Posted by Siddharth Godbole <si...@iiitb.ac.in>.
Hi Julius,

   Thaks for your advice. That things works very well.

Regards,
Sid
----- Original Message ----- 
From: "Julius Davies" <ju...@cucbc.com>
To: "HttpClient User Discussion" <ht...@jakarta.apache.org>; 
"HttpClient List" <ht...@jakarta.apache.org>
Sent: Monday, July 17, 2006 2:15 AM
Subject: RE: Getting Images from the http server


***********************
Your mail has been scanned by InterScan VirusWall.
***********-***********


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




-- 
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.1.394 / Virus Database: 268.10.1/389 - Release Date: 7/14/2006



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


RE: Getting Images from the http server

Posted by Julius Davies <ju...@cucbc.com>.
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