gpt4 book ai didi

jsf - 读取后 Primefaces 不会立即关闭 DefaultStreamedContent 流

转载 作者:行者123 更新时间:2023-12-04 00:41:35 46 4
gpt4 key购买 nike

我有以下问题:

我正在使用 <p:graphicImage> 在我的网络应用程序中显示图像来自 Primefaces

显示的图像由 bean 作为 DefaultStreamedContent 传递.在我的应用程序中,我有时会在运行时删除以这种方式显示的图像。

这总是需要一点时间才能删除图像。稍微调试后,我使用了 Files.delete Java 7 并得到以下异常:

The process cannot access the file because it is being used by another process.

因此我怀疑 Primefaces 不会立即关闭 DefaultStreamedContent 后面的流显示后,我无法随时删除文件。

有没有办法告诉DefaultStreamedContent阅读后立即关闭自身(我已经查看了文档并没有在 DefaultStreamedContent 中找到任何合适的方法,但也许有人可以告诉流或类似的东西?)

最佳答案

好吧,我终于找到了使用 Unlocker 工具发生了什么

(可以在这里下载:http://www.emptyloop.com/unlocker/#download)

我看到 java.exe 在文件显示后将其锁定。因此 StreamedContent 后面的 Stream 在读取后不会立即关闭。

我的解决方案如下:

我创建了一个扩展 StreamedContent 的父类(super class),让它读取输入流并将读取的字节“馈送”到新的 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[] {});
}
}

我很确定图像被 Primefaces 检索了 2 次,但仅在第一次加载时关闭。我一开始没有意识到这一点。

我希望这也能帮助其他人:)

关于jsf - 读取后 Primefaces 不会立即关闭 DefaultStreamedContent 流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18400078/

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com