在java中加载来自url的图像

时间:2015-01-29 08:48:48

标签: java

我使用过windows 7 os,chrome / 40.0.2214.93

我尝试使用java

从url中获取图像

我的java代码是

    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));
}

}

运行应用程序时的错误

    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)

我已经尝试过与此相关的堆栈流程指南,但问题仍未解决。帮我解决一下。

...谢谢

2 个答案:

答案 0 :(得分:1)

我尝试了你的代码:

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

我得到了与你相同的错误:

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

您收到的错误是由于来自服务器的403回答,而不是由于您的代码。

403 Forbidden

服务器理解请求,但拒绝履行请求。授权无效,请求不应重复。如果请求方法不是HEAD并且服务器希望公开为什么请求没有得到满足,那么它应该描述实体中拒绝的原因。

答案 1 :(得分:0)

您遇到此问题,因为此网站使用SSL。有关更多信息,请查看此链接:403 Forbidden with Java

测试下面的代码,它工作并打印==&gt;差异百分比:1.6255930981604882在您的控制台上

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));
    }