java jar文件阅读图像文件
问题描述:
我有独立的应用程序,其中有一个模块发送电子邮件。此应用程序打包为可执行JAR,其中包含所有资源文件,包括图像。java jar文件阅读图像文件
我使用弹簧用于发送电子邮件,其中包含内联下面的代码:
春天代码使用org.springframework.core.io.FileSystemResource
//IN-LINE ATTCHEMENTS
if (null != msg.getInlineAttachments() && msg.getInlineAttachments().size() > 0) {
for (Map.Entry<String, File> e : msg.getInlineAttachments().entrySet()) {
if (log.isTraceEnabled()) {
log.trace("Conntent-ID:" + e.getKey() + ", Resource:" + e.getValue());
}
try {
helper.addInline(e.getKey(), new FileSystemResource(e.getValue()));
} catch (Exception e1) {
log.error(e1);
}
}
}
文件图像使用以下代码传递给上述代码:
ClassPathResource res = new ClassPathResource("./images/" + name);
if (log.isTraceEnabled()) {
log.trace(res.getFile().getAbsolutePath());
}
file = res.getFile();
注意: 在Eclipse中的开发环境中执行时,应用程序工作正常,因为它是分解格式,非jar。
例外:
java.io.FileNotFoundException: class path resource [images/app_logo.png]
cannot be resolved to absolute file path because it does not reside in the file system:
jar:file:/C:/TEMP/app-1.0/app-1.0.jar!/images/app_logo.png
答
唯一的选择是进行图像复制文件到临时文件夹,并参考来自那里......
你可以尝试使用使用ClassPathResource而不将其转换到一个文件。然后它应该从JAR工作。 – vikingsteve 2013-02-22 18:37:29
也没有这个变化的运气:( – gpa 2013-02-22 19:22:16