【发布时间】:2017-04-14 22:19:45
【问题描述】:
我是 Spring Boot 的新手。我在src/main/resource 中有这个emailprop.properties:
//your private key
mail.smtp.dkim.privatekey=classpath:/emailproperties/private.key.der
但我得到的错误是
classpath:\email properties\private.key.der(文件名、目录 名称或卷标语法不正确)
如何正确加载此文件?
Update-1
我的 java 代码是
dkimSigner = new DKIMSigner(emailProps.getProperty("mail.smtp.dkim.signingdomain"), emailProps.getProperty("mail.smtp.dkim.selector"), emailProps.getProperty("mail.smtp.dkim.privatekey"));它作为
"D:\\WorkShop\\MyDemoProj\\EmailService\\src\\main\\resources\\private.key.der"而不是emailProps.getProperty("mail.smtp.dkim.privatekey")工作
Update-2
我试过java代码是
String data = ""; ClassPathResource cpr = new ClassPathResource("private.key.der"); try { byte[] bdata = FileCopyUtils.copyToByteArray(cpr.getInputStream()); data = new String(bdata, StandardCharsets.UTF_8); } catch (IOException e) { e.printStackTrace(); } dkimSigner = new DKIMSigner(emailProps.getProperty("mail.smtp.dkim.signingdomain"), emailProps.getProperty("mail.smtp.dkim.selector"),data);错误是:
java.io.FileNotFoundException: class path resource [classpath:private.key.der] cannot be resolved to URL because it does not exist试过的代码是:
ClassPathResource resource = new ClassPathResource(emailProps.getProperty("mail.smtp.dkim.privatekey")); File file = resource.getFile(); String absolutePath = file.getAbsolutePath();还是一样的错误..
请更新答案..
【问题讨论】:
-
首先,您是如何尝试从资源文件夹中加载文件的?你试过this one 或this answer 吗??
-
File file = new File(String.valueOf(this.getClass().getResource("classpath:/emailproperties/private.key.der"))); -
@GingerHead 感谢重播。我已经尝试过,但错误为 EmailSmsService\class path resource [private.key.der] java.io.FileNotFoundException
标签: spring spring-mvc spring-boot properties-file