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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top