Spring mvc 获取项目运行目录的两种办法

2017-11-21 13:18:12 admin ...

第一种方法:

//获取物理路径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;
    }

相似文章