简体   繁体   English

在 Spring 引导应用程序中从 JAR 上的资源文件夹中获取图像

[英]Get images from resources folder on a JAR in a Spring boot application

I have a spring boot application.我有一个 spring 启动应用程序。 When I deploy mi application in a local Tomcat as a war, I'm able to use images from the resources folder by this way:当我在本地 Tomcat 部署 mi 应用程序作为战争时,我可以通过这种方式使用资源文件夹中的图像:

ResourceUtils.getFile("classpath:Logo.png")

My problem is when I package the applications as a JAR. Seems that is not able to find the images... After some time searching the inte.net, I tried a few different things, but none have worked so far.我的问题是当我将应用程序 package 作为 JAR 时。似乎无法找到图像...在搜索 inte.net 一段时间后,我尝试了一些不同的方法,但到目前为止都没有用。

Resource resource  = new ClassPathResource("classpath:Logo.png");
InputStream inputStream = resource.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
imagePart.attachFile(FileCopyUtils.copyToString(reader));

Edit: Project Structure编辑:项目结构

在此处输入图像描述

Could you please share the project structure?你能分享一下项目结构吗? If the images are placed in test/resources/ or main/resources folder then the jar will not have any resources from tests or main folder.如果图像放置在test/resources/main/resources文件夹中,则 jar 将没有来自 tests 或 main 文件夹的任何资源。 That could be one possibility.这可能是一种可能性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何动态获取spring boot应用程序jar父文件夹路径? - How to get spring boot application jar parent folder path dynamically? 使用 Spring Boot Maven 插件时,jar 文件中缺少 Spring Boot 应用程序中的资源 - resources in a Spring Boot application are missing from jar file when using Spring Boot Maven Plugin 使用Spring Boot Maven插件时无法将资源文件夹放入jar - cannot get resources folder into jar when using spring boot maven plugin Spring boot:从 jar 读取时尝试读取资源文件夹中的目录内容会产生 FileNotFoundException - Spring boot: trying to read directory content inside resources folder produces FileNotFoundException when reading from jar Spring Boot无法在/ resources / static / images文件夹中找到图像 - Spring Boot unable to find the images in /resources/static/images folder 如何从 Spring Boot 应用程序的文件夹作为 jar 运行主应用程序(不是 Spring Boots 应用程序) - How to run a main application (not a spring boots appl) from a folder of Spring Boot application as jar 在 Spring Boot 中从 resources 文件夹中读取文件 - Read file from resources folder in Spring Boot Spring 引导使用 Docker 从外部文件夹获取图像 - Spring Boot get images from an external folder with Docker 如何动态地将静态资源添加到spring-boot jar应用程序? - How to dynamically add static resources to spring-boot jar application? 如何在Spring Boot应用程序中从maven目录结构的resources文件夹中拾取文件? - How to pick up a file from resources folder of maven directory structure in a spring boot application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM