java jar文件读取图像文件

时间:2013-02-22 18:32:01

标签: java spring email embedded-resource

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

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

Spring代码正在使用 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

2 个答案:

答案 0 :(得分:1)

您需要将图像作为Stream而不是文件处理。文件是仅在文件系统中有效的概念,但是您试图访问不是文件系统的Jar内部的东西。

答案 1 :(得分:0)

只剩下选项是将图像文件复制到临时文件夹,并从那里引用......

相关问题