springMVC文件上传

来源:互联网 发布:几何网络是什么意思 编辑:程序博客网 时间:2024/06/06 19:58
@RequestMapping(params = "doAdd")    public ModelAndView doAdd(ProjectMainAttEntity projectMainAtt, HttpServletRequest request, @RequestParam("file") MultipartFile[] file) {        String message = null;        message = "待审核经费添加成功";        String yyyyMM = DateUtils.date2Str(DateUtils.date_yyyy_mm);        String path = request.getSession().getServletContext().getRealPath("upload/") + "/" +"moneyfile"+"/" + yyyyMM + "/";        String[] detailNames = request.getParameterValues("detailName"); //明细名称        String[] detailMoneys = request.getParameterValues("detailMoney"); //实际支出金额        String[] realDateArray = request.getParameterValues("realDate"); //实际支出时间        int i = 0;//经费明细索引        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); //字符串转时间        try{            projectMainAtt.setCheckedStatus("0");            projectMainAttService.save(projectMainAtt);            systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);        }catch(Exception e){            e.printStackTrace();            message = "待审核经费添加失败";            throw new BusinessException(e.getMessage());        }        //明细存储        for(MultipartFile f:file){            String fileName = f.getOriginalFilename().substring(0,f.getOriginalFilename().lastIndexOf("."));//文件名            String fileType = f.getOriginalFilename().substring(f.getOriginalFilename().lastIndexOf(".")+1);//文件名            String targetFileName = this.pmMoneyFileService.targetFileName(fileType);            PmMoneyFileEntity moneyFileEntity = new PmMoneyFileEntity();            moneyFileEntity.setDetailName(detailNames[i]);            moneyFileEntity.setDetailMoney(detailMoneys[i]);            Date realDate = null;            try {                realDate = sdf.parse(realDateArray[i]);            } catch (ParseException e1) {                // TODO Auto-generated catch block                e1.printStackTrace();            }            moneyFileEntity.setRealDate(realDate);            //制造文件存放相对路径            String filePath="upload" + "/" + "moneyfile"+ "/" + yyyyMM + "/";            filePath = filePath + targetFileName;//相对路径            moneyFileEntity.setFilePath(filePath);            moneyFileEntity.setFileName(fileName);            moneyFileEntity.setDocumentId(projectMainAtt.getId());            try {                pmMoneyFileService.save(moneyFileEntity);            } catch (Exception e1) {                // TODO Auto-generated catch block                e1.printStackTrace();            }            File targetFile = new File(path,targetFileName);            //如果文件夹不存在 就创建文件夹            if(!targetFile.exists()){                  targetFile.mkdirs();              }            try {                f.transferTo(targetFile); //上传文件至服务器指定路径(path)            } catch (IllegalStateException e) {                // TODO Auto-generated catch block                e.printStackTrace();            } catch (IOException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }            i++;        }        request.setAttribute("message", message);        request.setAttribute("returnURL", "projectMainAttController.do?list");        return new ModelAndView("success");    }