Skip to content

Latest commit

 

History

History
52 lines (36 loc) · 1.95 KB

README.md

File metadata and controls

52 lines (36 loc) · 1.95 KB

springbootreadfilejar

Demo project for reading Files from within Spring Boot Fat-jar with Java NIO.2

Wait, what??!

There is an Issue with loading Files inside of a SpringBoot Fatjar with Java NIO.2 Files and especially Paths.get()-Methode, which is needed to use Files. Oracle has something for ya.

HowTo

Run it inside of eclipse (after mvn eclipse:eclipse) and firering a GET to

localhost:8080/files

(e.g. with Postman) should work (giving something like "File reading successfull"). Inside your IDE, the Path will look something like this:

jar:file:/C:/dev/temp/springbootreadfilejar/target/springbootreadfilejar-0.0.1-SNAPSHOT.jar!/files/homepage-feature-projects.png

Then, run it with something like

java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=n -jar springbootreadfilejar-0.0.1-SNAPSHOT.jar

and create a remote-debug-configuration in eclipse, when you´re inside the project.

Then run your debugger. Firering to the url again - does it work?

Now the Path is magically looking like

jar:file:/C:/dev/temp/springbootreadfilejar/target/springbootreadfilejar-0.0.1-SNAPSHOT.jar!/files/homepage-feature-projects.png

But that´s all to much effort!

Because you can just use org.springframework.core.io.Resource with @Value-Annotation - and all the Stuff is handled by Spring for you!

Look at the second Method inside FileReader-Class and run the second test. Just

@Value("classpath:files/homepage-feature-projects.png")
private Resource file;

file.getInputStream();

That´s it.