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...

Asha
  • 750
  • 1
  • 6
  • 22
  • 2
    [`403 Forbidden`](https://en.wikipedia.org/wiki/HTTP_403) means that your client is not allowed to acces the URL. What does the server require? –  Jan 29 '15 at 08:52
  • The error 403 its about "u don't have permission"... – Alist3r Jan 29 '15 at 08:52
  • The question should be "What is HTTP response code: 403?". Just googling *http* *403* will return you enough answers to retract the question. – Franky Jan 29 '15 at 08:54
  • possible duplicate of [403 Forbidden vs 401 Unauthorized HTTP responses](http://stackoverflow.com/questions/3297048/403-forbidden-vs-401-unauthorized-http-responses) – Alessandro Da Rugna Jan 29 '15 at 09:07

2 Answers2

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.

Giulio Biagini
  • 935
  • 5
  • 8
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));
    }
Community
  • 1
  • 1
Elyas Hadizadeh
  • 3,289
  • 8
  • 40
  • 54
  • Thanks for your response. I solve the problem by your reply @Elyas – Asha Jan 29 '15 at 09:21
  • @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. ;-) – Elyas Hadizadeh Jan 29 '15 at 09:28