Specify files in resource folder in spring application.properties file

I have a Spring boot application, the code should access the file in the resources folder. here is my application.properties file:

cert.file=classpath:/resources/cert.p12 

however, he always complained:

 java.io.FileNotFoundException: classpath:/resources/cert.p12 (No such file or directory) 

I double checked the folder my_project / target / classes so that the cert.p12 file comes out of it.

and in the code I tried to access the file:

 @Value("${cert.file}") private String certFile; .... @Bean public Sender sender() { return new Sender(certFile); } 

what is this class? and why can't he find the file? Thanks!

+5
source share
4 answers

The class path includes what you have inside the dir resources.

Try:

 cert.file=classpath:cert.p12 

I assume you have a standard maven directory structure.

+2
source

You can simply use XXX.class.getResourceAsStream("filename") to get the resource. eg:

 ObjectInputStream ois = new ObjectInputStream(MyClass.class.getResourceAsStream(PUBLIC_KEY_FILE)); Key key = (Key) ois.readObject(); ois.close(); 

And it works in my code. MyClass is a class that uses your crt file. My PUBLIC_KEY_FILE is "/rsa/PublicKey" and just save it in the src/main/resources/rsa folder

resource location

0
source

This syntax does not work with regular FileInputStream. Use Spring Resourceloader instead.

 @Autowired private ResourceLoader resourceLoader; @Value("${property.name}") private String property; File getPropertyFile(){ return resourceLoader.getResource(property).getFile(); } 

application.properties

 property.name=classpath:filename.txt 
0
source

This path worked for me cert.file =. / Build / resources / main / cert.p12

-1
source

All Articles