tags:

views:

40

answers:

2

I have servlet in my web application that serves images, and when I visit those urls with browser images are server correctly. Then I have this other servlet that resizes images, idea is to visit get image by url in resize servlet and then resize image. But for some reason all following methods return null, but when I visit given url with browser, image is shown correctly.

    URL imageURL = new URL(fullUrl);
    // Case 1
    RenderedImage img = ImageIO.read(imageURL);

    // Case 2
    BufferedImage img = JAI.create("url", imageURL).getAsBufferedImage();

    // Case 3
    Image img = java.awt.Toolkit.getDefaultToolkit().getDefaultToolkit().createImage(imageURL);
+2  A: 
URL url = new URL("http://www.digitalphotoartistry.com/rose1.jpg");
Image image = ImageIO.read(url);  

or

URL url = new URL("http://www.digitalphotoartistry.com/rose1.jpg");
java.awt.Image image = java.awt.Toolkit.getDefaultToolkit().getDefaultToolkit().createImage(url);   

Update:

This code works for me Try checking your URL.

public static void main(String[] args) throws Exception {
   URL imageURL = new URL("http://www.digitalphotoartistry.com/rose1.jpg");
    // Case 1
    RenderedImage img = ImageIO.read(imageURL);
    System.out.println(img);
}

output:

BufferedImage@e80a59: type = 5 ColorModel: #pixelBits = 24 numComponents = 3 col
or space = java.awt.color.ICC_ColorSpace@1ff5ea7 transparency = 1 has alpha = fa
lse isAlphaPre = false ByteInterleavedRaster: width = 553 height = 737 #numDataE
lements 3 dataOff[0] = 2
org.life.java
As you can see I did try that, but it returned null.
newbie
@newbie @ T.J. Crowder I missed that thing :) , btw have you checked url ? my code is working like..
org.life.java
@newbie: try with other urls, for example set `fullUrl="http://www.digitalphotoartistry.com/rose1.jpg";`
Michał Niklas
A: 

From the docs:

If no registered ImageReader claims to be able to read the resulting stream, null is returned.

Could it be that you have no registered ImageReader for the image type?

T.J. Crowder
I found out that servlet getting image didn't have access rights to servlet serving the image, so result was always null.
newbie
@newbie: Odd, I would have expected an `IOException`.
T.J. Crowder