获取当前服务器目录文件

String path= this.getServletContext().getRealPath("") +request.getRequestURI().substring(request.getContextPath().length());

(request.getContextPath()是项目名 所以uri里面需要去掉项目名  this.getServletContext().getRealPath("")已经包含了项目名

String dirpath = new File(path).getParent();

this.getServletContext().getRealPath("") 也可以用application.getRealPath(""),不够好像说bean中不推荐使用

也可以直接写进里面

String path= this.getServletContext().getRealPath(request.getRequestURI().substring(request.getContextPath().length()));//获取到的是当前JSP文件的路径
String dirpath= new File(path).getParent();    //只是返回一个“/”

得到的是D://XXXXX/XXXX/XXXX/XXXXX     获得jsp的目录 最后面是没有  /  

所以dirpath后面加文件名 需要在文件名前加/

比如String filepath = dirpath+"/a.txt";

http://blog.csdn.net/zbz0425/article/details/8109406

String realPath = request.getRealPath("");//项目绝对路径

String realPath = request.getRealPath("/upload");//该根目录路径下指定文件夹的路径

这个方法获取的就是你部署的工程的根路径

http://wenda.haosou.com/q/1378282656063583

原文地址:https://www.cnblogs.com/LuoXiaoTing604404828/p/4722605.html