File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes how to save images Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "how to save images " Watch "how to save images " New topic
Author

how to save images

bryan lim
Ranch Hand

Joined: Dec 26, 2008
Posts: 140
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.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
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.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

and also go through this example .it may help you
bryan lim
Ranch Hand

Joined: Dec 26, 2008
Posts: 140
thank you for your reply.

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: 42276
    
  64
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.


Ping & DNS - my free Android networking tools app
bryan lim
Ranch Hand

Joined: Dec 26, 2008
Posts: 140
thanks again for your reply.

how do i download the image ?

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..
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
As said before, use java.net.URLConnection.

Keep in mind: this has completely nothing to do with servlets.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
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
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...

URLConnection conn = url.openConnection();
InputStream is = conn.getInputStream();



<html>
<head>
<title>659002.jpg (image)</title>
<script type="text/javascript">
<!--
if (top.location != self.location) top.location = self.location;
// -->
</script>
</head>
<body bgcolor="#ffffff" text="#000000">

</body>
</html>
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42276
    
  64
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
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...


I repeat: use java.net.URLConnection.

You can use it to get an InputStream of every public URL you want. So also those of the images. Do you understand it now?

To write it to a local file, just use FileOutputStream. To persist it in a DB, just use PreparedStatement#setBinaryStream().
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to save images