javajar文件读取图像文件

javajar文件读取图像文件,java,spring,email,embedded-resource,Java,Spring,Email,Embedded Resource,我有一个独立的应用程序,其中有一个模块可以发送电子邮件。此应用程序打包为可执行JAR,包含包括图像在内的所有资源文件 我使用spring发送电子邮件,其中包含以下内联代码: Spring代码正在使用org.springframework.core.io.FileSystemResource 注意: 应用程序在eclipse的开发环境中执行时工作良好,因为它是分解格式,非jar 例外情况: java.io.FileNotFoundException: class path resource

我有一个独立的应用程序,其中有一个模块可以发送电子邮件。此应用程序打包为可执行JAR,包含包括图像在内的所有资源文件

我使用spring发送电子邮件,其中包含以下内联代码:

Spring代码正在使用org.springframework.core.io.FileSystemResource

注意: 应用程序在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

您需要将图像作为流而不是文件处理。文件是仅在文件系统中有效的概念,但您正在尝试访问Jar中的非文件系统的内容。

唯一遗漏的选项是将图像文件复制到临时文件夹中,从那里引用…

您可以尝试使用ClassPathResource而不将其转换为文件。那么它应该可以从一个JAR中工作。这个更改也不太好:(“您需要将图像作为一个流来处理。”。或者URL…“而不是一个文件。”我已尝试使用ClassPathResource中的InputStreamSource对象传递InputStream,但仍然找不到。在您的jar中,文件在哪里?在jar文件中,有两个主要文件夹com和resources/resources/images/img001.png…那么此行不正确:ClassPathResource res=new ClassPathResource(“./images/”+name);应该是:ClassPathResource res=new ClassPathResource(“/resources/images/”+name);
    ClassPathResource res = new ClassPathResource("./images/" + name);
    if (log.isTraceEnabled()) {
        log.trace(res.getFile().getAbsolutePath());
    }
    file = res.getFile();
    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