【发布时间】: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);
}
【问题讨论】:
-
阅读 Try/Catch - 您可以响应异常并处理它而不是崩溃。你可以从这里开始阅读:docs.oracle.com/javase/tutorial/essential/exceptions/try.html
-
这很可能只是网络相关的问题。确保您的应用可以连接到互联网(防火墙、代理等)。