2017-01-31 203 views
0

我有一个问题宽度这段代码:设置类路径资源

final Map<String, File> attachments = new HashMap<>(); 
 
\t \t final Map<String, String> inlineResources = new HashMap<String, String>(); 
 
\t \t inlineResources.put("logo", "/res/img/IN_payoff.png"); 
 
\t \t for (File certificateFile : certificates) { 
 
\t \t \t attachments.put(certificateFile.getName(), certificateFile); 
 
\t \t } 
 
\t \t YadaEmailParam p = new YadaEmailParam(); 
 
\t \t 
 
\t \t p.inlineResources = inlineResources; 
 

 
\t \t yadaEmailService.sendHtmlEmail(p); 
 
\t }

此错误:

org.springframework.mail.MailSendException: Failed messages: javax.mail.MessagingException: IOException while sending message; 
 
    nested exception is: 
 
\t java.io.FileNotFoundException: class path resource [res/img/IN_payoff.png] cannot be opened because it does not exist

如何设置类路径?

回答

1

如果这是一个Maven项目,必须在类路径中可用的资源必须在src/main/resources中。

+0

不,这不是一个Maven项目。它是GitLab的一个Java项目。这个html文件是src/main/resources中的模板邮件,img是src/res/img –

0

如果是Maven项目,请将您的资源文件放在src/main/resources目录中,因为它将在classpath中结束,并且会自动包含在.jar文件中。那么您的资源将可通过img/IN_payoff.png路径访问。

或者,您可以将您的目录中的pom.xml类路径:

<build> 
    <resources> 
     <resource> 
      <directory>src/main/res</directory> 
     </resource> 
    </resources> 
</build> 
+0

不,这不是Maven项目。它是GitLab的一个Java项目。这个html文件是src/main/resources中的模板邮件,img在src/res/img中 –