Spring mvc批量图片上传

来源:互联网 发布:淘宝直播抽奖狂点屏幕 编辑:程序博客网 时间:2024/05/16 09:24
@RequestMapping(value="referenceBmpSave",method = RequestMethod.POST)public String referenceBmpSave(@RequestParam(value="file",required=false) MultipartFile[] file,HttpServletRequest request,String orderId,Model model, RedirectAttributes redirectAttributes,ReferenceBmp referenceBmp) throws IllegalStateException, IOException {//获得物理路径webapp所在路径          String pathRoot = request.getSession().getServletContext().getRealPath("");          List<ReferenceBmp> listImagePath=new ArrayList<ReferenceBmp>();          String path="";          try {         listImagePath=new ArrayList<ReferenceBmp>();          for (MultipartFile mf : file) {          if(!mf.isEmpty()){          //文件名称         String fileName=mf.getOriginalFilename();        String name=fileName.substring(0, fileName.indexOf("."));        //获得文件类型(可以判断如果不是图片,禁止上传)          // String contentType=mf.getContentType();          //获得文件后缀名称          //String imageName=contentType.substzring(contentType.indexOf("/")+1);          path="/referenceBmp/"+orderId+"/"+fileName;          File tempFile = new File(Global.getUserfilesBaseDir() + path);        //如果目录不存在           if (!tempFile.getParentFile().exists()) {         //创建目录        tempFile.getParentFile().mkdirs();          }          if (!tempFile.exists()) {          tempFile.createNewFile();          }        mf.transferTo(tempFile);          referenceBmp.setBmpPath(path);        referenceBmp.setOrderId(orderId);        referenceBmp.setName(name);        orderService.referenceBmpSave(referenceBmp);        listImagePath.add(referenceBmp);          }          }  } catch (Exception e) {e.printStackTrace();  addMessage(redirectAttributes, "上传参考图失败"); }        model.addAttribute("listImagePath", listImagePath);return "modules/sys/order/referenceBmpForm";}

原创粉丝点击