String filepath=request.getServletContext().getRealPath("/")+"files\\";
获取的当前文件在实际运行的tomcat地址目录
String path = ClassUtils.getDefaultClassLoader().getResource("").getPath()+"tmp/files/";
获取的是当前文件的静态目录
但是以上两种方式在linux或者jar包中会失效,想要在jar包中或者linux下同样获取到文件,可以用下面这种:WORD_TEMPLATE是文件在resource下路径
public String getString() throws IOException {
String WORD_TEMPLATE = "/file/2.txt";
InputStream io = this.getClass().getResourceAsStream(WORD_TEMPLATE);
BufferedReader br = new BufferedReader(new InputStreamReader(io));
String line;
String result = "";
while ((line = br.readLine()) != null) {
result = result+" "+line;
}
br.close();
return result.trim();
}