如果为体现项目的层次感,当我们使用request的时候,一般都是在控制层中使用的,没有必要将request也传递到service使用,所以首先是讲一下在controller获取当前的项目的绝对路径
public Message<Object> uploadImgsFile(HttpServletRequest request) { //1 String
realPath =
request.getSession().getServletContext().getRealPath(File.separator);
System.out.println(realPath); //2 String realPath2 =
request.getServletContext().getRealPath(File.separator);
System.out.println(realPath2); return null; }
如果当前想要获取当前项目的绝对路径,而此时又不在controller,同时又不想传递request的参数到service层,可以这样操作
//代码片段 //现获取当前的路径 WebApplicationContext webApplicationContext =
ContextLoader.getCurrentWebApplicationContext(); ServletContext servletContext
= webApplicationContext.getServletContext(); String realPath =
servletContext.getRealPath(File.separator); System.out.println(realPath);
经过上面的操作可以获取项目的路径,以此来方便自己项目的需求

技术
今日推荐
PPT
阅读数 99
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信