springmvc controller获得绝对路径及MultipartFile保存文件

@RequestMapping(value = "/add")
public String addGeneDiagProj(AddReq addReq,
                @RequestParam("diagnoseFile") MultipartFile diagnoseFile,
            HttpServletRequest request,HttpServletResponse response,ModelMap model)
    {

            String fileSep = System.getProperty("file.separator");
            String filePath = request.getSession().getServletContext().getRealPath(fileSep);
            File foler = new File(filePath.concat("diagnoseFile").concat(fileSep).concat(fileSep+"projectNo"));
            if(!foler.exists())
            {
                boolean f = foler.mkdirs();
                if(!f)
                {
                    LOG.error(getClass().getSimpleName()+" foler.mkdirs failed,folder:"+foler.getAbsolutePath());
                }
            }

            File dest = new File(foler.getAbsolutePath()+fileSep+diagnoseFile.getOriginalFilename());
            if(!dest.exists())
            {
                boolean cf = dest.createNewFile();
                if(cf)
                {
                    geneDiagnoseFile.transferTo(dest);//MultipartFile能自动转存文件
                   //other action
                }else
                {
                    LOG.error(getClass().getSimpleName()+" dest.createNewFile failed,folder:"+foler.getAbsolutePath());
                }
            }
  }

猜你喜欢

转载自halfsking.iteye.com/blog/2346924