Get an image from a website
I cant figure out how to get an image off of a website, is their a class that can do this?...if so could some one kindly point me to the docs of said class
I cant figure out how to get an image off of a website, is their a class that can do this?...if so could some one kindly point me to the docs of said class
BufferedImage im = ImageIO.read(url);
http://java.sun.com/javase/6/docs/api/javax/imageio/ImageIO.html
Image file should have its URL. You can open and read the URLConnection on that.
Ah, yes, BDLH's code is much more simple.
Message was edited by:
hiwa
import java.net.*;
import java.io.*;
import java.util.Scanner;
class ImageGrabber {
public static void main(String[] argv) throws Exception {
FileOutputStream fos;
Socket s;
String pathToImage;
String file;
InputStream is;
Scanner kbd = new Scanner(System.in);
URLConnection connection;
URL url;
int in;
System.out.print("Enter URL of picture: ");
pathToImage = kbd.nextLine();
System.out.print("File to save to: ");
file = kbd.nextLine();
connection = new URL(pathToImage).openConnection();
connection.connect();
is = connection.getInputStream();
fos = new FileOutputStream(file);
while( (in = is.read()) != -1 ) {
fos.write(in);
}
System.out.println("Done.");
fos.close();
is.close();
}
}
> ... I prefer the LONG way ...
I guess it all comes down to how you define "how to get an image".
OP: you are being too vague! What are you trying to do? Be clear!