participate


Java Programming [Archive] - Get Image from given URL and save to local drive
<<   Back to Forum  |   Give us Feedback
2 Duke Stars available
This topic has 13 replies on 1 page.
StarCB
Posts:28
Registered: 6/10/03
Get Image from given URL and save to local drive   
Aug 12, 2003 2:42 AM

 
Hi all,
I have following code... How do i save the image to the file in my local hard drive?

try {
// Create a URL for the image's location
java.net.URL url = new java.net.URL(jTextFieldURL.getText());

// Get the image
java.awt.Image image = java.awt.Toolkit.getDefaultToolkit().getDefaultToolkit().createImage(url);

} catch (java.net.MalformedURLException e) {
System.out.println(e.toString());
} catch (java.io.IOException e) {
System.out.println(e.toString());
}
 
_nuhb_
Posts:695
Registered: 7/9/03
Re: Get Image from given URL and save to local drive      
Aug 12, 2003 2:50 AM (reply 1 of 13)  (In reply to original post )

 
Here's an example. Modify it for your own needs.

import java.net.*;
import java.io.*;
public
class ImageDownload
{
  public static void 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 = new byte[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();
  }
}
 
DevMentee
Posts:904
Registered: 5/16/03
Re: Get Image from given URL and save to local drive      
Aug 12, 2003 2:51 AM (reply 2 of 13)  (In reply to original post )

 
try something like this....

    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) {
    }
 
_nuhb_
Posts:695
Registered: 7/9/03
Can that be done?   
Aug 12, 2003 3:05 AM (reply 3 of 13)  (In reply to #2 )

 
Yo DevMentee,

try something like this....

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)
 
ahaweb
Posts:312
Registered: 2/17/98
Re: Can that be done?   
Aug 12, 2003 3:11 AM (reply 4 of 13)  (In reply to #3 )

 
I ran nuhb's code and it works.
 
DevMentee
Posts:904
Registered: 5/16/03
Re: Can that be done?   
Aug 12, 2003 3:12 AM (reply 5 of 13)  (In reply to #3 )

 
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????
 
_nuhb_
Posts:695
Registered: 7/9/03
Re: Can that be done?   
Aug 12, 2003 3:19 AM (reply 6 of 13)  (In reply to #5 )

 
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).

Greets
 
StarCB
Posts:28
Registered: 6/10/03
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) :

read : 2633
read : 4096
read : 284
read : 4096
read : 284
read : 4096
read : 284
read : 2471

read : -1 -- quite the loop and continue to write the file.

total byte save =18244 or 18KB only....
 
ahaweb
Posts:312
Registered: 2/17/98
Re: Get Image from given URL and save to local drive   
Aug 13, 2003 5:21 AM (reply 8 of 13)  (In reply to #7 )

 
I tested it with a 280k file and it downloaded the whole thing.
 
StarCB
Posts:28
Registered: 6/10/03
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....

Thanks for your help and explaination.
 
ahaweb
Posts:312
Registered: 2/17/98
Re: Get Image from given URL and save to local drive      
Aug 13, 2003 8:07 PM (reply 10 of 13)  (In reply to #9 )

 
List the URL's that don't work.
 
StarCB
Posts:28
Registered: 6/10/03
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.

Sorry my mistake. Thoudsand of appolozes :-P.

Thanks for helping.
 
StarCB
Posts:28
Registered: 6/10/03
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


thanks in advance for help :-)
 
StarCB
Posts:28
Registered: 6/10/03
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


thanks in advance for help :-)
 
This topic has 13 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 25
  • Guests : 132

About Sun forums
  • Oracle Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Oracle Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums