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

Discussion in 'Java' started by Rune, Feb 16, 2005.

  1. Rune

    Rune Guest

    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?
    Rune, Feb 16, 2005
    #1
    1. Advertising

  2. Rune

    Roland Guest

    On 16-2-2005 11:40, Rune wrote:
    > 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_/ /__/
    / \ /_/ / \
    Roland, Feb 16, 2005
    #2
    1. Advertising

  3. Thomas Weidenfeller, Feb 16, 2005
    #3
  4. Rune

    Rune Guest

    > 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.
    Rune, Feb 16, 2005
    #4
  5. Rune

    Rune Guest

    > ImageIO.read(urlConn.getInputStream());

    Yes this works. Thank you.
    Rune, Feb 16, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. F C
    Replies:
    2
    Views:
    5,130
  2. Patrick
    Replies:
    1
    Views:
    720
  3. Davidski
    Replies:
    0
    Views:
    3,867
    Davidski
    Nov 5, 2004
  4. Replies:
    3
    Views:
    21,393
    Knute Johnson
    Nov 18, 2005
  5. Trung Chinh Nguyen
    Replies:
    2
    Views:
    1,681
    Trung Chinh Nguyen
    Apr 26, 2006
Loading...

Share This Page