- String tomcatPath = System.getProperty("user.dir");
- out.print("tomcatPath:" + tomcatPath + "<br>");
- String filepath = "";
- if (tomcatPath.contains("//")) {
- filepath = tomcatPath.replace("//bin", "//webapps//CDNManageSystem//") + "speed.txt";
- } else {
- filepath = tomcatPath.replace("/bin", "/webapps/CDNManageSystem/") + "speed.txt";
- }
- URL url = Path.class.getClassLoader().getResource("/a.properties");
- String path = URLDecoder.decode(url.getPath(), "utf-8");
- out.print("path:" + path + "<br>");
- out.print("filepath:" + filepath + "<br>");
如上代码两个linux下结果:
tomcatPath:/home/app_admin/apache-tomcat-6.0.18/logs
path:/home/app_admin/apache-tomcat-6.0.18/webapps/pathtest/WEB-INF/classes/a.properties
filepath:/home/app_admin/apache-tomcat-6.0.18/logsspeed.txt
tomcatPath:/home/liuhy/apache-tomcat-6.0.29/bin
path:/home/liuhy/apache-tomcat-6.0.29/webapps/pathtest/WEB-INF/classes/a.properties
filepath:/home/liuhy/apache-tomcat-6.0.29/webapps/CDNManageSystem/speed.txt
windows下的结果:
tomcatPath:C:/Tomcat6.0/bin
path:/C:/Tomcat6.0/webapps/pathtest/WEB-INF/classes/a.properties
filepath:C:/Tomcat6.0/webapps/CDNManageSystem/speed.txt
总结下:
user.dir获得的路径:在哪执行的java命令,就是哪。可是第一个系统怎么会在logs下????
下面两个在bin目录下很正常。在logs下,这就有点说不过去了
如果从java类中获得path还是用getResource比较靠谱