• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can I retrieve an "Image" from a Remote Site?es be  RSS feed

 
Priya Madhuri
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am aware of retrieving data from a Remote Site.
But, I want to retreive an "Image" from a Remote Site.
How can it be done?
I need your help.
Can you please write here or send me through e-mail (priya_madhuri@usa.net), a sample program to retrieve "Image" from a remote Site.
Thank you
-Priya
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To give any more help we need to know where the image is stored, and how (if at all) it is made available. The fact that it is an image is probably the least important bit of information.
 
Priya Madhuri
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me explain you clearly.
I use the folling program to retrieve data(Text).
-------------------------------------
import java.net.*;
import java.io.*;
public class URLConnectionReader
{
public static void main(String args[])throws Exception
{
URL yahoo =new URL("http://www.yahoo.com/index.html");
URLConnection con=yahoo.openConnection();
BufferedReader br=new BufferedReader(new InputStreamReader(con.getInputStream()));
String line=null;
FileWriter fw=new FileWriter("File1.html");
while((line=br.readLine())!=null)
{
fw.write(line);
System.out.println(line);
}
br.close();
}

}
------------------------
But how can I retrieve for retrieving Images in the remote location?
-Priya
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like the code you have provided will work. Have you tries it?
 
Rahul Rathore
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think, the image data is binary- so you should not use reader/writer classes. Because the reader/writer classes deal with the stream as consisting of characters and translate from unicode encoding into default platform encoding or specified platform encoding and vice-versa.
So I think we should iterate through the input stream and write the bytes to a FileOutputStream, to "yourfile.gif".

[This message has been edited by Rahul Rathore (edited March 14, 2001).]
 
Priya Madhuri
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried with InputStreams also.
But it didn't work.
I will be grateful anybody help me in this program.
Thanks
-Priya
 
Rahul Rathore
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try making the following changes:-
1. If the site requires a user-name and password, then do:-

String username=...; String password=...;
byte[] buf=(username + ":" + password).getBytes();
String encoding=new sun.misc.BASE64Encoder().encode(buf);
URL url=new URL(...);
URLConnection connection=url.openConnection()
connection.setRequestProperty("Authorization", "Basic", encoding);
connection.connect();
...

2. Since the binary data will be received from the server encoded in BASE64 we must decode it before storing it in "yourfile.gif". Do this (all these statements can throw IOException etc.):-

InputStream is=connection.getInputStream();
FileOutputStream fos= ...;
new sun.misc.BASE64Decoder().decodeBuffer(is,fos);

I think you can further improve the above code by wrapping the Streams in BufferedInputStream and BufferedOutputStream, before passing to the decodeBuffer(InputStream,OutputStream) method.
The above should do the work. Please let us know the result.
To be doubly sure that the data you are getting is in BASE64 encoding do this:-
System.out.println(connection.getContentEncoding());
If the above shows BASE64 then the above method should work.
If you face any problem please let us know the specific problem. If you solve it some other way, please let us know what way you solved it.
Incidentally the BASE64 encode and decode methods are also available in javax.mail.internet.MimeUtility. You can use that instead of sun.misc.

[This message has been edited by Rahul Rathore (edited March 16, 2001).]
 
Rahul Rathore
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was hoping that some expert would challenge/correct me on suggestion no. 2 in the previous post. Will the binary image data be received in BASE64 encoding? Even if the binary data is being received in BASE64, it may be internally decoded by the input stream object (that we retrieve from connection.getInputStream())- So there would be no need for BASE64 decoding by us. Is some guru listening??
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!