Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Need Help in copying files from the web

jspiderjspider Posts: 3Member
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: ry.img. Source code would be a big help because I am new to java.
Thank you,

Comments

  • Josh CodeJosh Code Posts: 675Member
    : 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: ry.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()





Sign In or Register to comment.