0

i have used windows 7 os, chrome/40.0.2214.93

I try to fetch image from url using java

My java code is

    public static void main(String[] args) {
    // TODO Auto-generated method stub
     BufferedImage img1 = null;
        BufferedImage img2 = null;
        InputStream inputstream=null;
        URLConnection urlcon=null;
        try {
          URL url1 = new URL("http://rosettacode.org/mw/images/3/3c/Lenna50.jpg");
          URL url2 = new URL("http://rosettacode.org/mw/images/b/b6/Lenna100.jpg");

          urlcon=url1.openConnection();
          urlcon.addRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.93 Safari/537.36");

          img1 = ImageIO.read(url1.openStream());
          img2 = ImageIO.read(url2.openStream());
        } catch (IOException e) {
          e.printStackTrace();
        }
        int width1 = img1.getWidth(null);
        int width2 = img2.getWidth(null);
        int height1 = img1.getHeight(null);
        int height2 = img2.getHeight(null);
        if ((width1 != width2) || (height1 != height2)) {
          System.err.println("Error: Images dimensions mismatch");
          System.exit(1);
        }
        long diff = 0;
        for (int y = 0; y < height1; y++) {
          for (int x = 0; x < width1; x++) {
            int rgb1 = img1.getRGB(x, y);
            int rgb2 = img2.getRGB(x, y);
            int r1 = (rgb1 >> 16) & 0xff;
            int g1 = (rgb1 >>  8) & 0xff;
            int b1 = (rgb1      ) & 0xff;
            int r2 = (rgb2 >> 16) & 0xff;
            int g2 = (rgb2 >>  8) & 0xff;
            int b2 = (rgb2      ) & 0xff;
            diff += Math.abs(r1 - r2);
            diff += Math.abs(g1 - g2);
            diff += Math.abs(b1 - b2);
          }
        }
        double n = width1 * height1 * 3;
        double p = diff / n / 255.0;
        System.out.println("diff percent: " + (p * 100.0));
}

}

The error when i run the application

    java.io.IOException: Server returned HTTP response code: 403 for URL: http://rosettacode.org/mw/images/3/3c/Lenna50.jpg
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at java.net.URL.openStream(Unknown Source)
    at ImgDiffPercent.main(ImgDiffPercent.java:32)
Exception in thread "main" java.lang.NullPointerException
    at ImgDiffPercent.main(ImgDiffPercent.java:37)

Already i tried guidelines in stack flow related to that but still the problem not solved. Help me to solve it.

Thanks...

4
  • 2
    403 Forbidden means that your client is not allowed to acces the URL. What does the server require? Commented Jan 29, 2015 at 8:52
  • The error 403 its about "u don't have permission"... Commented Jan 29, 2015 at 8:52
  • The question should be "What is HTTP response code: 403?". Just googling http 403 will return you enough answers to retract the question. Commented Jan 29, 2015 at 8:54
  • possible duplicate of 403 Forbidden vs 401 Unauthorized HTTP responses Commented Jan 29, 2015 at 9:07

2 Answers 2

1

I tried your code:

public static void main(String[] args) throws IOException {
    BufferedImage image = ImageIO.read(new URL("http://rosettacode.org/mw/images/3/3c/Lenna50.jpg"));
}

and I got the same error as yours:

Exception in thread "main" javax.imageio.IIOException: Can't get input stream from URL!
Caused by: java.io.IOException: Server returned HTTP response code: 403 for URL: http://rosettacode.org/mw/images/3/3c/Lenna50.jpg

The error you got is due to a 403 answer from the server and not due to your code.

403 Forbidden:

The server understood the request, but is refusing to fulfill it. Authorization will not help and the request SHOULD NOT be repeated. If the request method was not HEAD and the server wishes to make public why the request has not been fulfilled, it SHOULD describe the reason for the refusal in the entity.

Sign up to request clarification or add additional context in comments.

Comments

0

you've got this problem because this site uses SSL. for more information check this link : 403 Forbidden with Java

test the below code, it works and print ==> diff percent: 1.6255930981604882 on your console

public static void main(String[] args)
    {
        // TODO Auto-generated method stub
        BufferedImage img1 = null;
        BufferedImage img2 = null;

        try
        {
            URL url1 = new URL("http://rosettacode.org/mw/images/3/3c/Lenna50.jpg");
            URL url2 = new URL("http://rosettacode.org/mw/images/b/b6/Lenna100.jpg");

            URLConnection conn1 = url1.openConnection();
            conn1.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
            InputStream in1 = conn1.getInputStream();

            URLConnection conn2 = url2.openConnection();
            conn2.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
            InputStream in2 = conn2.getInputStream();


            img1 = ImageIO.read(in1);
            img2 = ImageIO.read(in2);
        } catch (IOException e)
        {
            e.printStackTrace();
        }
        int width1 = img1.getWidth(null);
        int width2 = img2.getWidth(null);
        int height1 = img1.getHeight(null);
        int height2 = img2.getHeight(null);
        if ((width1 != width2) || (height1 != height2))
        {
            System.err.println("Error: Images dimensions mismatch");
            System.exit(1);
        }
        long diff = 0;
        for (int y = 0; y < height1; y++)
        {
            for (int x = 0; x < width1; x++)
            {
                int rgb1 = img1.getRGB(x, y);
                int rgb2 = img2.getRGB(x, y);
                int r1 = (rgb1 >> 16) & 0xff;
                int g1 = (rgb1 >> 8) & 0xff;
                int b1 = (rgb1) & 0xff;
                int r2 = (rgb2 >> 16) & 0xff;
                int g2 = (rgb2 >> 8) & 0xff;
                int b2 = (rgb2) & 0xff;
                diff += Math.abs(r1 - r2);
                diff += Math.abs(g1 - g2);
                diff += Math.abs(b1 - b2);
            }
        }
        double n = width1 * height1 * 3;
        double p = diff / n / 255.0;
        System.out.println("diff percent: " + (p * 100.0));
    }

2 Comments

Thanks for your response. I solve the problem by your reply @Elyas
@Asha your welcome,in your code instead of passing url1.openStream() to ImageIO.read you you should pass urlcon.getInputStream() so you need two URLConnection for two image happy codding. ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.