【问题标题】:detecting missing jpegs over the internet通过互联网检测丢失的 jpeg
【发布时间】:2014-06-20 09:07:45
【问题描述】:

我的应用程序在 jpeg 中读取并将它们显示在 jlabel 上的问题(这些是书籍的图片) 与本地版本一起使用时一切正常,例如从 C 驱动器读取,但是一旦我尝试通过 Internet 执行此操作,就会出现我尝试纠正但没有成功的问题 设想 如果 url 末尾不存在 jpeg,我会收到以下错误

javax.imageio.IIOException: Can't get input stream from URL!

在从本地驱动器读取的版本中,我检测文件是否存在并解决了这个问题,但是我尝试了很多发布的想法,但我根本不知道如何检测 jpeg 不存在! 请有人帮忙 这是两个版本的代码

从本地驱动器 C 读取

private void showcover() {
    String stockPic;
    String partofISBN;
    String completeurl;
    jButton9.setVisible(true);
    stockPic = jTextField1.getText();// get the current isbn
    partofISBN = stockPic.substring(0, 7); // get first 7 numbers
    String picUrl;
    stockPic = stockPic + localNumber + ".jpg";
    picUrl = partofISBN + "\\" + stockPic;
    completeurl = "C:\\Apicture\\" + picUrl;

    File pf = new File(completeurl);

    if (!pf.exists()) {

        jLabel9.setIcon(new ImageIcon("C:\\Apicture\\" + picUrl));
        jLabel9.setIcon(new ImageIcon("C:\\Apicture\\nojpegs.jpg"));
        jLabel9.setText("NO Jpeg");
    }

    jLabel9.setIcon(new ImageIcon(completeurl));

}

适应从url读取

URL url;
url = new URL("http://ebid.s3.amazonaws.com/upload_big/9/1/1/1401018425-17770-385.jpg");

Image image = null;
try {
    image = ImageIO.read(url);
} catch (IOException ex) {
    Logger.getLogger(baseframe.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (MalformedURLException ex) {
    Logger.getLogger(baseframe.class.getName()).log(Level.SEVERE, null, ex);
}

【问题讨论】:

标签: java url


【解决方案1】:

javax.imageio.IIOException 表示您没有得到图像。

因此,在您的 catch(IOException ) 块中添加更多代码以故障转移到备用 URL/磁盘。

【讨论】:

    【解决方案2】:

    读取 http HEAD 请求和响应代码 404 以发现未找到。 Http是你的朋友。

    在 GET 之前找出资源是否存在。

    【讨论】:

      【解决方案3】:

      您可能想查看我刚刚提供的这个答案:I can't download a specific image using java code

      亚马逊似乎也在检查您的用户代理,因此您可能需要在代码开头添加类似的内容

      System.setProperty("http.agent", "Mozilla/5.0 (Windows NT 5.1; rv:19.0) Gecko/20100101 Firefox/19.0");
      

      编辑:“在代码的开头”实际上意味着类似于“在创建任何与 URL 相关的对象之前”。我的意思是,我不想指代您发布的代码,而是指整个应用程序。

      【讨论】:

        【解决方案4】:

        使用这个:

        URL url;
        url = new URL("http://ebid.s3.amazonaws.com/upload_big/9/1/1/1401018425-17770-385.jpg");
        url.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:19.0) Gecko/20100101 Firefox/19.0");
        
        Image image = null;
        try {
            image = ImageIO.read(url);
        } catch (IOException ex) {
            Logger.getLogger(baseframe.class.getName()).log(Level.SEVERE, null, ex);
        }
        } catch (MalformedURLException ex) {
            Logger.getLogger(baseframe.class.getName()).log(Level.SEVERE, null, ex);
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-04-08
          • 1970-01-01
          • 2012-01-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-09-25
          • 2010-11-13
          相关资源
          最近更新 更多