java.io.FileNotFoundException: data \ downTemplate \ imgTemplate \ radio1.png (system can not find the path specified.)

Problem Scenario:

     Download world document, poi-tl by third party open source template engine implementation, this problem occurs the first time, that is dependent on a third party

Absolute path can only read, but later when the new File reading project with pictures, there was the problem

solve:

     Use request to obtain the absolute path to webroot

to sum up:

     javaweb project when reading files at webroot resources, can only use the physical path (J: \ ideaWorkspace \ EMSystem \ WebRoot \.)

 

 

In javaweb project new File ( "./ aa") and new File ( "aa") will not work

1) the use of Java's class loading mechanism to call XXX.class.getClassLoader () getResource ( "");. Method to get to ClassPath, then the process of obtaining WebRoot directory (this is the only way to class in WebRoot / WEB-INF / lower classes to take effect, if the class is packaged into a jar file, the method fails. in this case you should use the following one way)


2) Servlet using the acquired physical path request the webroot

3) thinking spring frame, the WEB-INF / web.xml, create a webAppRootKey param of, a specified value (default webapp.root) as a key, and then executed by a String webAppRootKey The Listener, or Filter, or Servlet = . getServletContext () getRealPath ( "/"); and webAppRootKey webapp.root respectively corresponding to a Key, Value system properties written to system properties. After obtaining the physical path in the program by WebRoot System.getProperty ( "webapp.root")
 

The first two ways I have used, have not tried it a third

 

 

 Reference: https://blog.csdn.net/zhykhs/article/details/83357046

Published 22 original articles · won praise 21 · views 10000 +

Guess you like

Origin blog.csdn.net/corleone_4ever/article/details/86689159