absolutePath为"C:\\apache-tomcat-6.0.37\\...后面省略";
在tomcat下用Java去File file = new File(absolutePath+"\\"+fileName),然后打印日志会发现在绝对路径前面会多出来一个bin的路径,System.out.println(file.getAbsolutePath()); 为 "C:\apache-tomcat-6.0.37\bin\???C:\apache-tomcat-6.0.37\...\"+fileName,因为这个路径是错误的,所以新建文件就报错了。
正确处理为:把absolutePath+"\\"+fileName 最前面的"C:\\apache-tomcat-6.0.37\\"去掉,改为"..\\..\\",即返回上两级菜单,再用剩下的路径去新建文件,这样文件就可以新建出来了。
String absolutePath = "C:\\apache-tomcat-6.0.37\\...后面省略";
File file = new File(absolutePath+"\\"+fileName);
System.out.println(file.getAbsolutePath());
//"C:\apache-tomcat-6.0.37\bin\???C:\apache-tomcat-6.0.37\...\"+fileName
//正确如下
String absolutePath = "..\\..\\...后面省略"

本文讲述了在Java中如何正确处理Tomcat路径问题,避免因多余路径导致文件创建失败。通过理解并调整绝对路径,解决从C:apache-tomcat-6.0.37到实际文件夹的路径问题,推荐使用 '....' 路径策略。
1057

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



