【发布时间】:2013-02-19 13:23:21
【问题描述】:
我正在使用 JSF 2.1.7 + Prime Faces + Prime Faces Mobile。
我有一个引用图片的页面:
<img src="/charts/chart1.png"/>
有一个单独的后台进程运行以每 2 分钟更新一次 chart1.png。 (它从特定的在线 url 获取图像)。这是我用来保存图像的代码:
public static String saveImage(final String strurl, final String imageFileName){
//assume the url for the image is valid
try {
URL url = new URL(strurl);
HttpURLConnection conn = (HttpURLConnection )url.openConnection();
conn.setConnectTimeout(1000); //1 sec timeout
conn.connect();
File file = new File(imageFileName);
BufferedImage image = ImageIO.read(conn.getInputStream());
ImageIO.write(image, "png", file);
return file.getAbsolutePath();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
ImageIO.write 将删除现有的图像文件并写出一个新的。我相信上述操作会导致图片文件在几毫秒内不可用,因此网络用户将无法看到图片。
我的问题是:
1) 真的会有图片暂时不可用的时候吗?
2) 如果是这种情况,有什么可能的解决方案来解决这个问题,以便用户始终拥有可用的图像?
提前致谢。 =)
【问题讨论】:
标签: image jakarta-ee jsf-2 file-io