Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get Image from web site

Status
Not open for further replies.

developer155

Programmer
Jan 21, 2004
512
US
I need to get an image in byte code from a web site ( i have the URL). Along with the image there are a few lines of comments. I need to copy the image from web site to my directory (/images/image.gif).
What I am doing now is establishing the connection to the web site and getting the byte code and then writing it to RandoMAccessFile. But for some reason the file is empty. WHat coudl be wrong?

thanks
 
Is this post the same as the other one you did ? If so, please do not post the same issue in two threads.

If not ... why are you using a RandomAccessFile ?
 
umm, no this is nto the same issue. I am using RAF because that is how my htm docs are represented and passed to other functions. Does that make a difference? The thing is it looks like the program is copying about 375 bytes from the URL, but when I try to write these bytes into .gif file, it does not work. However, it works when I try to write it into htm file (but I really need to save the image as gif). Here is the code:
DataInputStream input = new DataInputStream(connect.getInputStream());


byte buffer[] = new byte[1024];
int i;
while((i = input.read(buffer)) != -1) {
{
if ( first ) {
file = new RandomAccessFile(target, "rw");
file.setLength(0);
first = false;
}
file.write(buffer, 0, i);

target is the .gif file where I want to write (like server\directory\image.gif)

thanks
 
Sedj, by the way, here is what the byte code is (the code that I am getting from web page). When I saved the web page as htm file, this is what I see (along with regular html tags that I had to create to make it htm file):


<!-- InterConnect 4.6 connect_4.6_mqlib6.4.2_12:15:58_MST_11/02/1999 -->

<! Opening html/map.html >

<IMG SRC=" but when I try to save the same thing from URL to gif, it does nto work. The gif file is created, but it is 1KB and when I try to open it I get a message "Cannot determine type"

hope this clarifies thing a bit...

thanks for all your help
 
I still do not ubderstand why you are using a RandowmAccessFile - this for is used when you need to read and write to a file at the same'ish time, or possibly write something depending on what you read, or wrote earlier. Its quite an overhead because it involves lots of OS level IO seeks and so on.

Anyway, if you want to download an image, just use something such as :

Code:
import java.io.*;
import java.net.*;
public class DownloadImage {

    public static void main(String args[]) throws IOException {

		URL testURL = new URL("[URL unfurl="true"]http://mqmapgend.mapquest.com/mqmapgend?MQMapGenRequest=FDT7w%7clztlg6%24.902wlaz%3a%26%40%24%3a%26%40r2u67%3al67%3al67%3a%2aE%14QXO%2ax0za0u%40_n1u1xly%24%3a%26%40%24%3a%26%40a%3a%2aE%14Ek5wc2cM6bd%2an5ub2%26u7n%2aE%14Ynosr7O%15%7c9r2xu");[/URL]
		HttpURLConnection c = (HttpURLConnection)(testURL.openConnection());

		c.setDoInput(true);
		c.setRequestProperty("Connection","Keep-Alive");
		c.setRequestProperty("Proxy-Connection","Keep-Alive");
		c.setRequestMethod("GET");
		c.setUseCaches(false);
		c.connect();
		FileOutputStream fos = new FileOutputStream("out.gif");
		int cl = c.getContentLength();
		DataInputStream dis = new DataInputStream(c.getInputStream());

		// content-length header not set - read the stream a byte at a time
		if (cl == -1) {
			boolean bb = true;
			try {
				while(bb) {
					fos.write(dis.readByte());
				}
			} catch (EOFException eof) {}
		// cool, content-length is set, so read the stream in one chunk
		} else {
			byte[] buf = new byte[cl];
			dis.readFully(buf);
			fos.write(buf);
		}

		fos.close();
		fos.flush();
		dis.close();

		c.disconnect();
	}
}
 
Correction, this :

fos.close();
fos.flush();

should be :

fos.flush();
fos.close();
 
Sedj, when I run the code I get the .gif file which is 353 bytes and when I try to open it I still get the same errors like Cannot Determine Type, etc. Maybe I am not getting the byte code from the web site?. Check out this website that I am passing and tell me if this will send me the byte code of the image

Looks like it is just passing me image source and not the actual image, that is why the image file that is created is not really an image

thanks for all your help
 
I don't know what you mean - the code I gave you works fine ... I just tested it again ... make sure you are checking the right file. The out.gif should be 15KB.
 
Well, not being funny, but it works for every other url I've tried, and we use similar code in our production software ...

For the precise URL you have tried (the second one) the actual error is that their server is returning a premature EOF marker - so I would say it is their fault.

Try and add a little debug yourself, or experiment yourself - that's what programming is about ...
 
Sedj, basically this URL is passed


and the URL that you were passing in your example (that I sent yesterday) is part of the source code of the URL above. So what I need to do is get the http... part and get connection to it and then will get byte code from that (because that will be the true image). So I need to parse the bytes code to get http part. How to parse byte code? I tried to make it into string but get garbage

thanks
 
I really feel that its time for you to stand a little on your own - do some research, use google, read the fine manuals, get a book. I've given you a working piece of code to download an image - I reckon you need to take the rest further yourself in order to teach yourself.

This is a site to help professionals - to point them in the right direction - not a "Java help desk".
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top