1,获得项目中的文件(test.txt)的路径
String filePath=this.getCalss().getClassLoader().getResource("test.txt").getPath();
2,将文件放进tomcat文件内,在java中获取文件
String filePath=request.getServletContext().getRealPath("/");
filePath=filePath.substring(0,filePath.lastIndexOf("\\"));
filePath=filePath.substring(0,filePath.lastIndexOf("\\"));
filePath=filePath.substring(0,filePath.lastIndexOf("\\"));
filePath=filePath+"\\test.txt";
最后得到的filePath路径为:D:\java\tomcat7\test.txt
本文介绍如何在Java中获取位于Tomcat服务器内的文件路径,包括使用getClass().getClassLoader().getResource()方法获取资源文件路径,以及通过request.getServletContext().getRealPath()方法结合字符串操作来定位文件的具体位置。
1万+

被折叠的 条评论
为什么被折叠?



