Java

Moderators: zibadian
Number of threads: 7836
Number of posts: 18235

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Need Help in copying files from the web Posted by jspider on 8 Mar 2007 at 1:07 PM
I wrote a program that downloads a web page. I need to know how to download the images associated with the web page. Given an absolute address like http://www.yahoo.com/try.img, the program should copy try.img to C:\try.img. Source code would be a big help because I am new to java.
Thank you,
Report
Re: Need Help in copying files from the web Posted by Josh Code on 8 Mar 2007 at 4:02 PM
: I wrote a program that downloads a web page. I need to know how to download the images associated with the web page. Given an absolute address like http://www.yahoo.com/try.img, the program should copy try.img to C:\try.img. Source code would be a big help because I am new to java.
: Thank you,
:

Assuming the page's source is XML compliant, you can use Java's API for XML parsing. Through the generated Document, you could access the img elements with the src attributes and get local and occationally absolute references to the images.

Parsing of an XML file: http://kickjava.com/2515.htm

If you can't assume the page is XML compliant, you will have to find your own way of accessing image references. Maybe you could search the whole page's source for substrings like "src=" and work from there.

Now you have a url for an image to download. You could create a URL object to wrap around that String. This URL object can be used a couple different ways. You could either load the image into an Image object or directly save the downloaded data into a file.


To load the image into an image object, make a call to the getImage method of the Toolkit class.

Toolkit's getImage method: http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Toolkit.html#getImage(java.net.URL)

As long as the image is in a supported method, that would work. jpg, png, gif are all supported.

For writing the image to a file:
http://java.sun.com/developer/JDCTechTips/2004/tt0217.html


To write the data directly to a file, open a FileOutputStream so you can read from the URL's openStream() and immediately write all the data into your file.

URL's openStream method: http://java.sun.com/j2se/1.3/docs/api/java/net/URL.html#openStream()








 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.