第一种方法:
//获取物理路径webapp所在路径
WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
ServletContext servletContext = webApplicationContext.getServletContext();
// 得到文件绝对路径
String pathRoot = servletContext.getRealPath("/Uploads");
第二种方法
@RequestMapping("categoryHome")
public ModelAndView categoryHome(ParamModel pm,HttpServletRequest req) {
String path=req.getServletContext().getContextPath();
System.out.println(path);
String realPath=req.getServletContext().getRealPath("/uploadFile");
System.out.println(realPath);
ModelAndView mv = new ModelAndView("back/category/CategoryHome");
mv.addObject("pm", pm);
return mv;
}