如何解决使用 Vaadin 14
我写这篇文章是因为我需要一些帮助。 我无法根据应用中的特定路径显示图像。
基本上它在做什么:我有一个名为 Sector 的模块,每个 Sector 都可以有一个与之相关的图像。在使用Vaadin的Upload组件时,我将图片的路径保存到我的数据库中的一个表格中,以便它可以显示之前选择的图片。
图片的实际路径很奇怪,好像是Vaadin把图片复制到了一个动态随机文件夹。它不能使用图像的实际路径似乎是合乎逻辑的。
但问题在于:路径已正确输入到数据库中,但是当我重新加载页面 (F5) 时,Vaadin 无法再显示图像。这让我很不高兴,因为它应该显示得很好。
Vaadin 上传图片创建的路径:VAADIN/dynamic/resource/2/c1ef7b9d-8f2b-4354-a97e-fe1fd4e868e7/551434.jpg
如果有帮助,我可以放一些代码。
屏幕截图显示了我刷新浏览器页面后正在执行的操作。
这是我处理上传图片的代码部分:
upload.addSucceededListener(e -> {
Component component = createComponent(e.getMIMEType(),e.getFileName(),buffer.getInputStream());
showOutput(e.getFileName(),component,output);
//imgUpload = (Image) component;
InputStream inputStream = buffer.getInputStream();
targetFile = new File(PATH + currentProjetId + "\\secteur" + currentSecId + "\\photoSec.png");
try {
FileUtils.copyInputStreamToFile(inputStream,targetFile);
} catch (IOException e1) {
e1.printStackTrace();
Notification.show("Error");
}
System.out.println("PATH : " + targetFile.getPath());
});
解决方法
我认为您正在使用刷新视图时丢弃的内存资源。您必须获取文件的内容并将其保存在服务器文件系统目录内的文件中。举个例子:
FileBuffer receiver = new FileBuffer();
Upload upload = new Upload(receiver);
upload.setAcceptedFileTypes("text/plain");
upload.addSucceededListener(event -> {
try {
InputStream in = receiver.getInputStream();
File tempFile = receiver.getFileData().getFile();
File destFile = new File("/some/directory/" + event.getFileName());
FileUtils.moveFile(tempFile,destFile);
} catch (IOException e) {
e.printStackTrace();
Notification.show("Error").
}
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。