Rit*_*tel 3 file-io jsf stream primefaces
我有以下问题:
我正在使用<p:graphicImage>来自 Primefaces 的a 在我的 web 应用程序中显示图像
显示的图像由 bean 作为DefaultStreamedContent. 在我的应用程序中,我有时会删除运行时以这种方式显示的图像。
这总是需要一点时间才能删除图像。在调试了一点之后,我使用了Files.deleteJava 7 并得到以下异常:
The process cannot access the file because it is being used by another process.
Run Code Online (Sandbox Code Playgroud)
因此,我怀疑 Primefaces 没有立即关闭DefaultStreamedContent显示后的流,并且我无法随时删除文件。
有没有办法告诉DefaultStreamedContent阅读后立即关闭自己(我已经查看了文档并没有在 中找到任何合适的方法DefaultStreamedContent,但也许人们可以告诉流或类似的东西?)
好的,我终于知道使用该Unlocker工具发生了什么
(可以在这里下载:http : //www.emptyloop.com/unlocker/#download)
我看到java.exe文件一旦显示就会被锁定。为此在Stream背后StreamedContent不读书后立即关闭。
我的解决方案如下:
我创建了一个扩展 的超类StreamedContent,让它读取输入流并将读取的字节“馈送”到一个新的InputStream. 之后,我关闭了给定的流,以便再次释放其背后的资源。
这个类看起来像这样:
public class PersonalStreamedContent extends DefaultStreamedContent {
/**
* Copies the given Inputstream and closes it afterwards
*/
public PersonalStreamedContent(FileInputStream stream, String contentType) {
super(copyInputStream(stream), contentType);
}
public static InputStream copyInputStream(InputStream stream) {
if (stream != null) {
try {
byte[] bytes = IOUtils.toByteArray(stream);
stream.close();
return new ByteArrayInputStream(bytes);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
System.out.println("inputStream was null");
}
return new ByteArrayInputStream(new byte[] {});
}
}
Run Code Online (Sandbox Code Playgroud)
我很确定图像被检索了 2 次,Primefaces但仅在第一次加载时关闭。一开始我并没有意识到这一点。
我希望这也可以帮助其他一些人:)
| 归档时间: |
|
| 查看次数: |
3848 次 |
| 最近记录: |