vaadin14 - 在 Vaadin 14/RapidclipseX IMG 组件中访问本地资源(本地目录-项目外)
问题描述
如何在图像组件中从我的硬盘驱动器(外部项目文件夹)访问本地图像文件(以制作图像库)?如何写文件的路径?在 Vaadin 14/RapidclipseX 中。
它只需要来自项目或 URL 的路径。在我的项目中,用户必须上传图片,我想制作一个展示图片的画廊。
我尝试了以下所有方法,但没有奏效:
D:\\Canavans\\Reports\\256456\\424599_320943657950832_475095338_n.jpg
D:\Canavans\Reports\256456\424599_320943657950832_475095338_n.jpg
代码(也试过这种方式):
for(final File file: files)
{
System.out.println(file.getName());
this.log.info(file.getName());
this.log.info(file.getAbsolutePath());
final String path = "file:\\\\" + file.getAbsolutePath().replace("\\", "\\\\");
this.log.info(path);
final Image img = new Image();
img.setWidth("300px");
img.setHeight("300px");
img.setSrc("file:\\\\D:\\\\Canavans\\\\Reports\\\\256456\\\\IMG20171002142508.jpg");
this.flexLayout.add(img);
}
请帮忙!提前致谢。或者有没有其他方法可以创建图片库?
解决方案
嗯,据我所知,如果你想访问资源文件夹之外的资源,你可以创建一个 StreamResource 并用它初始化图像。如果您想使用图像“src”属性,则该文件必须位于 Vaadin 的资源文件夹之一中(例如“webapp”文件夹)。
下面是一个关于如何使用 StreamResource 创建图像的简单示例:
final StreamResource imageResource = new StreamResource("MyResourceName", () -> {
try
{
return new FileInputStream(new File("C:\\Users\\Test\\Desktop\\test.png"));
}
catch(final FileNotFoundException e)
{
e.printStackTrace();
return null;
}
});
this.add(new Image(imageResource, "Couldn't load image :("));
希望这可以帮助 :)
推荐阅读
- dart - Dart 构造函数中的大括号有什么作用?
- python - 即时编译为字节码/本机代码
- facebook-graph-api - 如何在 Firebase 云功能中正确执行 axios POST 请求
- apache-kafka - NiFi 的 ConsumeKafka_2_0 支持带有 ScramLoginModule 的 JAAS SASL
- java - java - 将复杂的 java 对象转换为 json
- linux - 如何通过管道将终端命令输出到 Zenity 进度对话框文本?
- ios - 如何使用cordova将自定义框架添加到“链接框架和库”?
- mysql - 优化 mySQL 查询 - 更新 JOIN 2 表
- php - 将递归数组转换为二维数组
- c++ - 我是否需要保护仅在同一线程中修改的变量的读取?