Vaadin Spring Application无法访问的图像

ste*_*ess 5 java image spring-boot vaadin-spring-boot

在这里给出的例子中:

https://vaadin.com/docs/framework/application/application-resources.html

images-folder放在应用程序的WEB-INF目录中.

在我的Vaadin Spring应用程序中,我没有WEB-INF目录,因此我将images文件夹放在"resources"文件夹中.这里是"src/main/resources"里面和"target"里面的文件夹结构:

在此输入图像描述

问题是我的应用程序无法访问那里的图像.我总是得到相同的"找不到文件"例外.

我尝试了不同的路径描述,其中包括以下内容:

VaadinService.getCurrent().getBaseDirectory().getAbsolutePath() + "images/pic.png"

VaadinService.getCurrent().getBaseDirectory().getAbsolutePath() + "/classes/images/pic.png"

VaadinService.getCurrent().getBaseDirectory().getAbsolutePath() + "/target/classes/images/pic.png"

VaadinService.getCurrent().getBaseDirectory().getAbsolutePath() + "/applicationName/target/classes/images/pic.png"
Run Code Online (Sandbox Code Playgroud)

......什么都没有用!!

如何使我的Vaadin Spring应用程序可以访问这些图像?

Kea*_*eks 6

您链接到的示例使用FileResource.它不适用于jar包装的资源.

最好将图像放入src/main/resources/VAADIN/themes/{theme}/并使用ThemeResource:

// Image as a file resource
FileResource resource = new ThemeResource("images/image.png");
Run Code Online (Sandbox Code Playgroud)

或者,将您的资源放到src/main/resources /然后从classpath访问它:

getClass().getResourceAsStream("/images/image.png")
Run Code Online (Sandbox Code Playgroud)