【问题标题】:Load live image into JLabel not working for some urls将实时图像加载到 JLabel 中不适用于某些 url
【发布时间】:2021-04-17 03:36:27
【问题描述】:

我想将实时 URL 图像加载到 JLabel 中。 我已经尝试过代码,但它不适用于某些 URL。 不知道会发生什么? 也不会发生异常。

    try {
        String imageUrl = "https://www.mouser.com/images/molex/images/501331-001a.jpg"; // not working url
       // String imageUrl = "http://in.element14.com/productimages/standard/en_GB//1458900-40.jpg"; // not working url
       // String imageUrl = "https://euroc-static.s3.amazonaws.com/verifiedcomp/pkg_files/parts_files/63661/63661.jpg"; // working url   
    
       URL url = new URL(imageUrl);     
       Image image = ImageIO.read(url);
       JFrame frame = new JFrame();
       frame.setSize(300, 300);
       JLabel label = new JLabel(new ImageIcon(image));
       frame.add(label);
       frame.setVisible(true);
    } catch (Exception e) {
      e.printStackTrace();
    }

【问题讨论】:

  • @Bhavin S. 寻找我编辑的答案,它应该可以解决您的问题
  • @Szprota21 感谢您的努力...您的代码在 Java11 中运行良好。谢谢。
  • 很高兴能帮到你。但是您可能应该更改该帖子的标题。也许以后会对某人有所帮助:)

标签: java image swing url


【解决方案1】:

编辑:此处的工作解决方案(仅从 Java 11 开始,对于旧版本,您需要找到不同的休息框架)

同样在这张图片“in.element14”中,你应该有 https 协议。

由于返回的内容长度为负数,因此无法正常工作。 HttpUrlConnection 似乎不支持这种情况。

@Andrew Thompson 不需要转换...

private void initUI() {
        JPanel images = new JPanel();
        BoxLayout boxLayout = new BoxLayout(images, BoxLayout.Y_AXIS);
        images.setLayout(boxLayout);
        images.setVisible(true);
        images.add(testLabel(
                "https://imageproxy.themaven.net//https%3A%2F%2Fwww.history.com%2F.image%2FMTY4OTA4MzI0ODc4NjkwMDAw%2Fchristmas-tree-gettyimages-1072744106.jpg"));
        images.add(testLabel("https://www.mouser.com/images/molex/images/501331-001a.jpg"));
        images.add(testLabel("https://euroc-static.s3.amazonaws.com/verifiedcomp/pkg_files/parts_files/63661/63661.jpg"));
        images.add(testLabel("https://in.element14.com/productimages/standard/en_GB/1458900-40.jpg"));
        this.add(images);
}

private JPanel testLabel(String imageUrl) {
        try {
            HttpClient client = HttpClient.newHttpClient();
            HttpRequest request = HttpRequest.newBuilder()
                                             .uri(URI.create(imageUrl)).GET()
                                             .build();
            HttpResponse<byte[]> response = client.send(request,
                                                        responseInfo -> HttpResponse.BodySubscribers.ofByteArray());
            InputStream targetStream = new ByteArrayInputStream(response.body());
            Image image = ImageIO.read(targetStream);

            JPanel panel = new JPanel();
            panel.setPreferredSize(new Dimension(200, 200));
            JLabel label = new JLabel(new ImageIcon(image));
            panel.add(label);
            panel.setVisible(true);
            return panel;

        } catch (Exception ex) {
            ex.printStackTrace();
            JPanel panel = new JPanel();
            panel.setSize(300, 300);
            JLabel label = new JLabel("Loading failed");
            panel.add(label);
            panel.setVisible(true);
            return panel;
        }
    }

【讨论】:

    【解决方案2】:

    试试这个代码。

          // String imageURL="https://imageproxy.themaven.net//https%3A%2F%2Fwww.history.com%2F.image%2FMTY4OTA4MzI0ODc4NjkwMDAw%2Fchristmas-tree-gettyimages-1072744106.jpg";
           String imageUrl = "https://www.mouser.com/images/molex/images/501331-001a.jpg"; // not working url
           // String imageUrl = "http://in.element14.com/productimages/standard/en_GB//1458900-40.jpg"; // not working url
           // String imageUrl = "https://euroc-static.s3.amazonaws.com/verifiedcomp/pkg_files/parts_files/63661/63661.jpg"; // working url   
        
            Image image = null;
            try {
                URL url = new URL(imageURL);
                image = ImageIO.read(url);
            } 
            catch (IOException e) {
            }
            JFrame frame = new JFrame();
            JLabel label = new JLabel(new ImageIcon(image));
            frame.getContentPane().add(label, BorderLayout.CENTER);
            JPanel Panel = new JPanel(new BorderLayout());
            Panel.add(label);
            frame.add(Panel);
            frame.setSize(300, 400);
            frame.setVisible(true);
    

    这应该可以完美运行

    【讨论】:

    • 您基本上确实粘贴了与 OP 相同的代码。它不会工作,因为它挂在 ImageIO.read 上,可能是服务器/代理问题
    • 没有,我试过了,当它没有得到图像时它会返回nullpointerexception或一些错误
    • 我确实复制粘贴了您的代码,但它在读取时挂起
    • 我正在检查是否是 url 的问题。
    • @ProgrammingGeek Bhavin 的问题是他面临着在 Jlabel 中加载图像的特定 URL 的问题。就像他分享了@98​​7654321@。试试这个网址来解决他的问题。否则他的代码也适用于您在代码中使用的网址。
    【解决方案3】:

    你确定

    // String imageUrl = "http://in.element14.com/productimages/standard/en_GB//1458900-40.jpg";
    

    此网址无效。我怀疑这是一个 SSL 问题。这意味着您无法访问图片的 startWith 'https://' URL。

    【讨论】:

    • 好的,没错。当我测试它时,结果是一样的。然而,我发现了你可以考虑的问题。仅当传递的 URL 是安全的(即 HTTPS)时才会发生这种情况,并且在 HTTP URL 的情况下,它可以按预期工作。
    • 解决方案:www.mouser.com;将证书放入您的 AEM 服务器信任库。这意味着问题来自 imageUrl,而不是您的代码。如果你想访问这个URL,你应该得到URL服务器的承认。
    • 我确认另一个 url 地址,在这种情况下是正确的。如我公司商品图片如下:https://facebook-dev.oss-cn-shenzhen.aliyuncs.com//20210105/10c617f99a3c42bf83982a58d208bcfb.png
    • "表示无法访问startWith 'https://' URL" 最后一张图片,注释为// working url是一个 HTTPS URL。
    猜你喜欢
    • 2014-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多