springMVC文件上传

@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");
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值