Java中的ImageIcon更新(来自互联网)?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java中的ImageIcon更新(来自互联网)?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2856字,纯文字阅读大概需要5分钟。
内容图文
我有一些带有图像的URL.此图像在每个请求期间更新(即,对(相同)URL的每个请求都返回一个新图像).比如说,这个URL指向CAPTCHA.
我的目标是在我的程序中加载和显示几个这样的图像.
以下代码将这些图像加载到我的本地文件系统并且工作正常(即,所有图像都不同,唯一):
String filePath;
String urlPath;
int numOfFilesToDownload;
//Here filePath and urlPath are initialized.
//filePath points to the directory, where to save images
//urlPath is the url from where to download images
//numOfFilesToDownload is the number of files to download
for(int i = 0; i < numOfFilesToDownload; i++){
//Initializing connection
URL url = new URL(urlPath);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//Downloading image
try(InputStream is = conn.getInputStream();
FileOutputStream os = new FileOutputStream(filePath + "img" + i + ".jpg")){
int b;
while((b = is.read()) != -1)
os.write(b);
}
}
但是当我尝试以下事情时,会发生一些奇怪的事情:
for(int i = 0; i < numOfFilesToDownload; i++){
//Initializing image from the url
URL url = new URL(urlPath);
javax.swing.ImageIcon ico = new javax.swing.ImageIcon(url);
//Showing the graphical dialog window with the image
javax.swing.JOptionPane.showMessageDialog(null, ico);
}
在后一种情况下,每个对话框包含在第一次迭代期间下载的相同图像,即一个图像.
此外,实验表明,如果你将“?r =”连接到urlPath(即一个简单的GET请求参数),那么url仍然有效.
以下代码似乎是有效的,并且完全符合它的要求(即显示的每个图像与前一个不同):
for(int i = 0; i < numOfFilesToDownload; i++){
//Initializing image from the url
URL url = new URL(urlPath + "?r=" + i);
javax.swing.ImageIcon ico = new javax.swing.ImageIcon(url);
//Showing the graphical dialog window with the image
javax.swing.JOptionPane.showMessageDialog(null, ico);
}
因此,我可以得出结论,ImageIcon以某种方式记住它处理的URL,并且根本不打算两次执行相同的工作……为什么以及如何?关于它的javadocs什么也没有.
解决方法:
当我尝试更改代码时,它运行正常.我的SSCCE:
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
public class TestUrls {
public static final String BASE_URL_PATH = "http://static.ed.edmunds-media.com/" +
"unversioned/adunit/homepage_showcase/";
public static final String[] URL_PATHS = {
"honda-odyssey-2013.png",
"chevrolet-impala-2013.png",
"mazda-cx9-2013.png",
"toyota-rav4-2013-2.png"
};
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
for (String urlPath : URL_PATHS) {
String fullUrlPath = BASE_URL_PATH + urlPath;
try {
URL url = new URL(fullUrlPath);
BufferedImage img = ImageIO.read(url);
ImageIcon icon = new ImageIcon(img);
JOptionPane.showMessageDialog(null, icon);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
}
}
内容总结
以上是互联网集市为您收集整理的Java中的ImageIcon更新(来自互联网)?全部内容,希望文章能够帮你解决Java中的ImageIcon更新(来自互联网)?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。