简体   繁体   English

在Java中从URL加载图像

[英]Load image from url in java

i have used windows 7 os, chrome/40.0.2214.93 我用过Windows 7 OS,chrome / 40.0.2214.93

I try to fetch image from url using java 我尝试使用Java从网址获取图片

My java code is 我的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));
}

} }

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

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答复,而不是由于您的代码。

403 Forbidden : 403禁止

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. 如果请求方法不是HEAD,并且服务器希望公开为什么未满足请求,则应在实体中描述拒绝原因。

you've got this problem because this site uses SSL. 您遇到了此问题,因为此站点使用SSL。 for more information check this link : 403 Forbidden with Java 有关更多信息,请检查此链接: 403 Java禁止

test the below code, it works and print ==> diff percent: 1.6255930981604882 on your console 测试以下代码,它可以正常工作并在控制台上打印==>差异百分比: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));
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM