Can't access static file from Spring Boot application running in Tomcat servlet

坚强是说给别人听的谎言 提交于 2019-12-11 05:13:43

问题


All I want to do is access a specific file in a Java class. I am using Spring Boot 2.0.0.M7. When I run from my development machine using mvn spring-boot:run then everything works well. In that case I am using:

File jFile = new File("src/main/resources/static/folder/fileName.txt");

When I deploy this to a Tomcat server (8.5) as a war, the file is put into:

WEB-INF/classes/static/folder/fileName.txt

I've tried everything I could find or think of, but always get an error of some sort, usually a file not found exception.

My most recent attempt was:

URL url = this.getClass().getClassLoader().getResource("fileName.txt");
File jFile = new File(url.getPath());

That produces a null pointer exception with url.getPath().

I have read about needing to access the servlet config, but couldn't ever understand that well enough to get it to work. Does anyone have a clear example so that I can access the file both from the development context and the production context? Thanks!

来源:https://stackoverflow.com/questions/48592302/cant-access-static-file-from-spring-boot-application-running-in-tomcat-servlet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!