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()));
String dirpath= new File(path).getParent();
得到的是D://XXXXX/XXXX/XXXX/XXXXX 获得jsp的目录 最后面是没有 /
所以dirpath后面加文件名 需要在文件名前加/
比如String filepath = dirpath+"/a.txt";