ImageIO: Read JPG image from URL with header: referer?

R

Rune

Hi,

I'm trying to make thumbnails of some images on the net. So I though I'd use
the ImageIO library. But apparently it only has this method to read images
from the net: read(java.net.URL). But I need to set the header referer
property on the net connection before reading the image. Which I had done
before with the URLConnection object.

(e.g.
URLConnection urlConn = url.openConnection();
urlConn.setRequestProperty("Referer", referer);
urlConn.connect();
)

But ImageIo.read() doesn't accept URLConnection. What do I do?
 
R

Roland

Hi,

I'm trying to make thumbnails of some images on the net. So I though I'd use
the ImageIO library. But apparently it only has this method to read images
from the net: read(java.net.URL). But I need to set the header referer
property on the net connection before reading the image. Which I had done
before with the URLConnection object.

(e.g.
URLConnection urlConn = url.openConnection();
urlConn.setRequestProperty("Referer", referer);
urlConn.connect();
)

But ImageIo.read() doesn't accept URLConnection. What do I do?

There's also ImageIO.read(java.io.InputStream). I haven't tried it, but
I'd say the following should work:

URLConnection urlConn = url.openConnection();
urlConn.setRequestProperty("Referer", referer);
urlConn.connect();
InputStream urlStream = urlConn.getInputStream();
image = ImageIO.read(urlStream);

--
Regards,

Roland de Ruiter
___ ___
/__/ w_/ /__/
/ \ /_/ / \
 
T

Thomas Weidenfeller

Rune said:
URLConnection urlConn = url.openConnection();
urlConn.setRequestProperty("Referer", referer);

ImageIO.read(urlConn.getInputStream());

/Thomas
 
R

Rune

There's also ImageIO.read(java.io.InputStream). I haven't tried it, but
I'd say the following should work:

URLConnection urlConn = url.openConnection();
urlConn.setRequestProperty("Referer", referer);
urlConn.connect();
InputStream urlStream = urlConn.getInputStream();
image = ImageIO.read(urlStream);

Works like a charm. Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top