2017-01-31 260 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中 –