About reading files in the server

Reading files in a java web project has always been a headache. It has always bothered me. There is no problem in starting the service locally to read the file, but when it is packaged as a war package and released on other servers, the path cannot be found. Now I would like to share with you the difficulties and solutions I encountered in this process.

  In the beginning, the way to read files in java is private static String FilePath = "./folder/file.txt"; this is no problem when starting the local tomcat, and the path can be found, but when it is released under the linux system , it reported an error that the path could not be found. Then I tried with String fileName=request.getSession().getServletContext().getRealPath("/")+System.getProperty("file.separator")+"WEBINF"+System.getProperty("file.separator")+ "RSAKey.txt"; to read, this is completely fine. But since the service used for development is the distributed development of the dubble service, this method is not good either. In the end, it is directly read by placing the file under the server path.

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326848715&siteId=291194637