1.java直接读取客户端txt文件
关于java的文件读取,网上也有许多资料,但是直接读取客户端文件的案例很少,大多是把文件上传到服务器上,再进行读取,读取后再删除。这种方法以前也试过,但是现在遇
到这种情况,就不想再这样处理。
2.JavaWeb中关于读取文件资源的路径问题
在jsp和class文件中调用的相对路径不同。在jsp里,根目录是WebRoot 在class文件中,根目录是WebRoot/WEB-INF/classes 当然你也可以用System.getProperty("user.dir")获取你工程的绝对路径。( 不过听说在linux下无法获取,未试验)
另:在Jsp,Servlet,Java中详细获得路径的方法!
2.1 jsp中取得路径:
以工程名为TEST为例:
(1)得到包含工程名的当前页面全路径:request.getRequestURI()
结果:/TEST/test.jsp
(2)得到工程名:request.getContextPath()
结果:/TEST
(3)得到当前页面所在目录下全名称:request.getServletPath()
结果:如果页面在jsp目录下 /TEST/jsp/test.jsp
(4)得到页面所在服务器的全路径:application.getRealPath("页面.jsp")
结果:D:\resin\webapps\TEST\test.jsp
(5)得到页面所在服务器的绝对路径:absPath=new java.io.File(application.getRealPath(request.getRequestURI())).getParent();
结果:D:\resin\webapps\TEST
2.2 在类中取得路径:
(1)类的绝对路径:Class.class.getClass().getResource("/").getPath()
结果:/D:/TEST/WebRoot/WEB-INF/classes/pack/
(2)得到工程的路径:System.getProperty("user.dir")
结果:D:\TEST
2.3 在Servlet中取得路径:
(1)得到工程目录:request.getSession().getServletContext().getRealPath("") 参数可具体到包名。
结果:E:\Tomcat\webapps\TEST
(2)得到IE地址栏地址:request.getRequestURL()
结果:http://localhost:8080/TEST/test
(3)得到相对地址:request.getRequestURI()
结果:/TEST/test
struts2设置了struts.multipart.saveDir后会在根目录建立文件夹,这样会涉及linux下的权限问题,
最好不要设置,使用struts默认
需要使用路径时,用下面的方法取得项目根目录的绝对路径(Tools为方法类)
public static String getRootPath() {
String classPath = Tools.class.getClassLoader().getResource("/").getPath();
String rootPath = "";
//windows下
if("\\".equals(File.separator)){
rootPath = classPath.substring(1,classPath.indexOf("/WEB-INF/classes"));
rootPath = rootPath.replace("/", "\\");
}
//linux下
if("/".equals(File.separator)){
rootPath = classPath.substring(0,classPath.indexOf("/WEB-INF/classes"));
rootPath = rootPath.replace("\\", "/");
}
return rootPath;
}
2.4 自己在项目中的测试,最后使用的 destFilePath3
// String destFilePath = request.getSession().getServletContext().getRealPath("/webapp/page/spssFileMore") + "\\" + site + ".html"; //目标文件 站点.html
// System.out.println("destFilePath---"+destFilePath);
// String destFilePath2 = request.getRequestURI();
// System.out.println("destFilePath2---"+destFilePath2);
// String destFilePath3 = request.getSession().getServletContext().getRealPath("/");
// System.out.println("destFilePath3---"+destFilePath3);
// String destFilePath4 = this.getClass().getClassLoader().getResource("/").getPath();
// System.out.println("destFilePath4---"+destFilePath4);
//
// String destFilePath5 = request.getSession().getServletContext().getRealPath(request.getRequestURI());
// System.out.println("destFilePath5---"+destFilePath5);
//
// String destFilePath6 = Thread.currentThread().getContextClassLoader().getResource("").getPath();
// System.out.println("destFilePath6---"+destFilePath6);
// // 第一种:获取类加载的根路径 D:\git\daotie\daotie\target\classes
// File f = new File(this.getClass().getResource("/").getPath());
// System.out.println(f);
//
// // 获取当前类的所在工程路径; 如果不加“/” 获取当前类的加载目录 D:\git\daotie\daotie\target\classes\my
// File f2 = new File(this.getClass().getResource("").getPath());
// System.out.println(f2);
3.关于编译后的项目路径,当时我一直没搞对的地方
这边项目的路径是找到编译之后的路径,我一直认为是源始文件的路径。从而致使我怀疑上面的获取路径是错误的。
我当时使用的开发工具是IDEA,项目编译之后的路径为
,然后再根据项目的路径去定位到我的文件路径。
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
浏览的网页:
http://blog.csdn.net/tomcat_2014/article/details/50624120
java中几种获取路径方式:http://blog.sina.com.cn/s/blog_637bfd4a010163jb.html
获取项目路径总结:http://lwkjob.iteye.com/blog/1674206