i want to create a servlet to recursively save the image in a webpage when given a starting point(the webpage url).
but my problem is how to save the images? what should i do with the image's url once i successfuly obtained it?
thank you.
Bauke Scholtz
Ranch Hand
Joined: Oct 08, 2006
Posts: 2458
posted
0
ytbryan lim wrote:hi all,
i want to create a servlet to recursively save the image in a webpage when given a starting point(the webpage url).
but my problem is how to save the images? what should i do with the image's url once i successfuly obtained it?
thank you.
A servlet to save images from an external resource? Well, you normally don't use servlets for that, so I at highest assume that you want to execute this application logic using a servlet. It is at least in no way possible with the request and response objects you have in a servlet.
Best what you can do is to use java.net.URLConnection to get an inputstream of the specified URL.
But i do not want to save it to the database and the image is from the internet and not my localhost.
if i use getinputStream, i will be getting a html page of the image and not the image.
am i right? if so, how do i really get the image off the webpage to my computer.
thank you again!!
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35241
7
posted
0
You will need to parse the HTML for <img> tags, extract the value of their source attribute, reconstruct full URLs from that, and then download the image from that URL. Be sure to handle relative as well as absolute URLs, and consider the effects of a <base href="..."/> tag.
Also note that images can be loaded in JavaScript and CSS code.
because my code is able to retrieve url already.. so i have all the urls of the images i want... but i dont know how to download them to my localhost..
Keep in mind: this has completely nothing to do with servlets.
Bauke Scholtz
Ranch Hand
Joined: Oct 08, 2006
Posts: 2458
posted
0
And please be careful with articles at Roseindia. A fairly lot of the articles over there contains bad practices.
bryan lim
Ranch Hand
Joined: Dec 26, 2008
Posts: 140
posted
0
thank you for your reply.
but i tried to use URLConnection and getInputStream. I got a html page (below) with the img src again..... i want to download the file but how can i do it? please advice me...
These two statements seem to contradict each other:
but i tried to use URLConnection and getInputStream. I got a html page (below) with the img src again.....
because my code is able to retrieve url already.. so i have all the urls of the images i want... but i dont know how to download them to my localhost..
What URL did you use, exactly?
Bauke Scholtz
Ranch Hand
Joined: Oct 08, 2006
Posts: 2458
posted
0
ytbryan lim wrote:thank you for your reply.
but i tried to use URLConnection and getInputStream. I got a html page (below) with the img src again..... i want to download the file but how can i do it? please advice me...