import java.net.*;
import java.io.*;
publicclass ImageDownload
{
publicstaticvoid main(String[] args)
throws Exception
{
// url to image
URL url = new URL(args[0]);
// input from image
InputStream in = new BufferedInputStream(url.openStream());
// downloaded bytes
ByteArrayOutputStream out = new ByteArrayOutputStream();
// output file
RandomAccessFile file = new RandomAccessFile("image","rw");
// download buffer
byte[] buffer = newbyte[4096];
// download the bytes
for (int read=0;(read=in.read(buffer))!=-1;out.write(buffer,0,read));
// write all data out to the file
file.write(out.toByteArray());
// close file
file.close();
}
}
Object object = /// your image
try {
// Serialize to a file
ObjectOutput out = new ObjectOutputStream(new FileOutputStream("filename.ser"));
out.writeObject(object);
out.close();
// Serialize to a byte array
ByteArrayOutputStream bos = new ByteArrayOutputStream() ;
out = new ObjectOutputStream(bos) ;
out.writeObject(object);
out.close();
// Get the bytes of the serialized object
byte[] buf = bos.toByteArray();
} catch (IOException e) {
}
I just had a question for you: does this work for images? I can't find anything about the java.awt.Image being Serializable. I also get strange errors:
~ $ java -classpath . ImageDownload file:///home/bhun/image.gif
Exception in thread "main" java.io.IOException: failed to load image contents
at javax.swing.ImageIcon.writeObject(ImageIcon.java:415)
and
~ $ java -classpath . ImageDownload http://developer.java.sun.com/images/v4_java_logo.gif
Exception in thread "main" java.io.IOException: failed to load image contents
at javax.swing.ImageIcon.writeObject(ImageIcon.java:415)
Yo! it probably is not.......I didn't try it with image (sorry!) but 99% of the time it works with other object types.....does your solution work well with images????
Yo! it probably is not.......I didn't try it with
image (sorry!) but 99% of the time it works with other
object types.....does your solution work well with
images????
Well, it's all bytes anyway. I just save the original byte data. You can also get Serialization to work if you subclass ImageIcon and do override the Serialization (I guess). It's a pity it doesn't work with serialization (the ImageIcon).
Re: Get Image from given URL and save to local drive
Aug 13, 2003 2:50 AM
(reply 7
of 13) (In reply to
#1 )
Hi nuhb,
thanks for your help. I have modify the code you gave, and I was able to get the file(image *.jpg) from the URL given. However, It doesn't work with the large file. I was able to get the file where the file size is 58KB or less, but it only save 18KB size when i download the file which it size is 485KB.
I try to trace the exception, but it doesn't gave me any exception.
Is url.openStream() has limitation on the size??? Or do i need to compress the stream/file before download???
below is the byte read when i trace the code (actual file size = 485KB) :
Re: Get Image from given URL and save to local drive
Aug 13, 2003 7:48 PM
(reply 9
of 13) (In reply to
#8 )
hi nuhb & ahaweb,
I discover that for some of the URL, i can download the file > 1150KB, but for some URL, it doesn't download complet byte....
Is in = new BufferedInputStream(url.openStream()); depends on the url connection? any restriction when getting the InputStream? or any time out for getting InputStream? How can i check whether url.openStream() returning complete InputStream?
Sorry to ask so many question as I'm quite new to the URL & Stream read & write....
Re: Get Image from given URL and save to local drive
Aug 14, 2003 8:41 PM
(reply 11
of 13) (In reply to
#10 )
hi nuhb & ahaweb,
It was my mistake on the URL. It should be the html file but i save it as the JPG file. This happen because when I manually enter the URL, the browser will redirect it to another html page instead of bring me to the JPG file. I change the file extension to *.htm, I can see the content of the html.
Re: Get Image from given URL and save to local drive
Sep 12, 2003 3:59 AM
(reply 12
of 13) (In reply to
#11 )
Hi all... back to this issue again...
How can i avoid this to happen :
When I manually enter the URL(url for the image .jpg), the browser will redirect it to another html page instead of bring me to the JPG file. But if i follow the link from the browser, it bring me to the correct site/url and i can right click the image and save it to my local drive.
How java can directly connect to the URL without redirect it to another html page.
I using the follwoing method :
URL urlSource = new URL(as_url);
URLConnection urlSourceCon = urlSource.openConnection();
example url : http://www23.smutserver.com/asian/avidols/hsuchi/pix/hcpic327.jpg
Re: Get Image from given URL and save to local drive
Sep 12, 2003 4:00 AM
(reply 13
of 13) (In reply to
#11 )
Hi all... back to this issue again...
How can i avoid this to happen :
When I manually enter the URL(url for the image .jpg), the browser will redirect it to another html page instead of bring me to the JPG file. But if i follow the link from the browser, it bring me to the correct site/url and i can right click the image and save it to my local drive.
How java can directly connect to the URL without redirect it to another html page.
I using the follwoing method :
URL urlSource = new URL(as_url);
URLConnection urlSourceCon = urlSource.openConnection();
example url : http://www23.smutserver.com/asian/avidols/hsuchi/pix/hcpic326.jpg