如何从 spring 引导资源文件夹嵌入图像和附加文件
How to embed images and attach files from spring boot resources folder
无法从 spring 引导资源文件夹
嵌入图像和附加文件
我使用 Spring 引导创建了一个 Restful 网络服务(Jar 文件而不是 War 文件)。有些服务会发送电子邮件,有些服务会发送带附件的电子邮件(将动态创建)。 Web 部件 (Angular) 驻留在部署在不同服务器中的 Apache 服务器中。
我正在使用 Freemarker 模板撰写电子邮件并使用 Amazon SES 发送电子邮件。
来自 freemarker 模板
<IMG src="cid:gridEmailHeaderImage">
添加图片的代码
MimeBodyPart inlineImage = new MimeBodyPart();
DataSource fds = new FileDataSource(imagePath.getAbsolutePath());
inlineImage.setDataHandler(new DataHandler(fds));
inlineImage.setHeader("Content-ID", "<" + contentId + ">");
inlineImage.setFileName(fds.getName());
content.addBodyPart(inlineImage);
如果我提供绝对路径,我可以嵌入和附加文件。但是,如果提供相对路径,它就不起作用。
我的文件夹结构
C:\workspace\service-1.0\src\main\resources\images\header.png
C:\workspace\service-1.0\src\main\resources\attachements\test-attachment-1.txt
我尝试了以下但没有成功
方法一
ServletContext context;
String path = context.getRealPath("/resources/images")+"/header.png";
正在以下文件夹中查找图像,但该文件夹中没有图像。
C:\Users\username\AppData\Local\Temp\tomcat-docbase..\resources\images/header.png
方法二
basePath = this.getClass().getClassLoader().getResource("/images/").getPath();
C:\workspace\service-1.0\src\main\resources\images\header.png
/C:/workspace/service-1.0/build/libs/service-1.0.jar!/BOOT-INF/classes!/images/
(仅适用于 eclipse 但不适用于命令提示符 java -jar build\lib\myapp.jar)
方法 3
ClassPathResource file = new ClassPathResource("header.png");
String mypath = file.getFile().getAbsolutePath();
(这个也没用)
如果我把图片放到resource下的images文件夹里。我可以通过以下 URL.
查看图像
http://localhost:7075/myservice/v1/images/header.png
从资源文件夹加载图片可以吗? spring 启动 jar 会在运行时爆炸吗?从 spring 引导 jar 文件加载图像的正确方法是什么。
由于您的资源是 jar 容器的一部分,因此您不能使用文件路径,而只能使用类加载器提供的 URLs。根据您是从 IDE 还是部署的 jar 启动它,这将是 file://...
或 jar:...
URL。资源文件夹不是 spring 引导结构的一部分,因此您不能使用它。使用 URLDataSource 你可以这样做:
URLDataSource source = new URLDataSource(this.getClass().getResource("/images/header.png"));
无法从 spring 引导资源文件夹
嵌入图像和附加文件我使用 Spring 引导创建了一个 Restful 网络服务(Jar 文件而不是 War 文件)。有些服务会发送电子邮件,有些服务会发送带附件的电子邮件(将动态创建)。 Web 部件 (Angular) 驻留在部署在不同服务器中的 Apache 服务器中。
我正在使用 Freemarker 模板撰写电子邮件并使用 Amazon SES 发送电子邮件。
来自 freemarker 模板
<IMG src="cid:gridEmailHeaderImage">
添加图片的代码
MimeBodyPart inlineImage = new MimeBodyPart();
DataSource fds = new FileDataSource(imagePath.getAbsolutePath());
inlineImage.setDataHandler(new DataHandler(fds));
inlineImage.setHeader("Content-ID", "<" + contentId + ">");
inlineImage.setFileName(fds.getName());
content.addBodyPart(inlineImage);
如果我提供绝对路径,我可以嵌入和附加文件。但是,如果提供相对路径,它就不起作用。
我的文件夹结构
C:\workspace\service-1.0\src\main\resources\images\header.png
C:\workspace\service-1.0\src\main\resources\attachements\test-attachment-1.txt
我尝试了以下但没有成功
方法一
ServletContext context;
String path = context.getRealPath("/resources/images")+"/header.png";
正在以下文件夹中查找图像,但该文件夹中没有图像。
C:\Users\username\AppData\Local\Temp\tomcat-docbase..\resources\images/header.png
方法二
basePath = this.getClass().getClassLoader().getResource("/images/").getPath();
C:\workspace\service-1.0\src\main\resources\images\header.png
/C:/workspace/service-1.0/build/libs/service-1.0.jar!/BOOT-INF/classes!/images/ (仅适用于 eclipse 但不适用于命令提示符 java -jar build\lib\myapp.jar)
方法 3
ClassPathResource file = new ClassPathResource("header.png");
String mypath = file.getFile().getAbsolutePath();
(这个也没用)
如果我把图片放到resource下的images文件夹里。我可以通过以下 URL.
查看图像http://localhost:7075/myservice/v1/images/header.png
从资源文件夹加载图片可以吗? spring 启动 jar 会在运行时爆炸吗?从 spring 引导 jar 文件加载图像的正确方法是什么。
由于您的资源是 jar 容器的一部分,因此您不能使用文件路径,而只能使用类加载器提供的 URLs。根据您是从 IDE 还是部署的 jar 启动它,这将是 file://...
或 jar:...
URL。资源文件夹不是 spring 引导结构的一部分,因此您不能使用它。使用 URLDataSource 你可以这样做:
URLDataSource source = new URLDataSource(this.getClass().getResource("/images/header.png"));