我使用 Eclipse + Vaadin 框架。
首先我将图像保存到 WEB-INF 文件夹中。 然后我将此图像加载到 FileResource 变量中,创建 Image 变量,然后将 Image 变量添加到 Layout 中。
图像资源的页面 URL 如下所示:http://servername/sitename/APP/connector/0/16/source/qwerty1.png
如何获取该格式的 URL 以供外部使用图像?
变量 basepath 返回本地路径:“..../WEB-INF/qwerty1.png”
String str = (String) VaadinService.getCurrent().getBaseDirectory().
getAbsolutePath() +"/WEB-INF/qwerty1.png";
File temp = new File(str);
FileOutputStream fos = new FileOutputStream(temp);
fos.write(os.toByteArray());
fos.flush();
fos.close();
String basepath = VaadinService.getCurrent().getBaseDirectory().
getAbsolutePath() +"/WEB-INF/qwerty1.png";
FileResource resource = new FileResource(new File(basepath));
Image image2 = new Image("Image from file", resource);
addComponent(image2);
最佳答案
如果您将文件放在 ...src/main/webapp/VAADIN/image.png
中,它应该可以使用例如 localhost:8080/VAADIN/image.png
。
关于java - 如何获取Vaadin资源路径的URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39600732/