Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to save images

 
bryan lim
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and also go through this example .it may help you
 
bryan lim
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
bryan lim
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As said before, use java.net.URLConnection.

Keep in mind: this has completely nothing to do with servlets.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And please be careful with articles at Roseindia. A fairly lot of the articles over there contains bad practices.
 
bryan lim
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic