设置类路径资源

时间:2017-01-31 11:51:22

标签: java spring email thymeleaf

我的代码宽度有问题:

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

		yadaEmailService.sendHtmlEmail(p);
	}

此错误:

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

我如何设置课程路径?

2 个答案:

答案 0 :(得分:1)

如果这是一个Maven项目,那么必须在类路径中提供的资源必须放在src/main/resources中。

答案 1 :(得分:0)

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

或者,您可以使用pom.xml将目录添加到classpath:

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