SpringMVC实现文件上传

来源:互联网 发布:淘宝可以贷款么 编辑:程序博客网 时间:2024/05/29 09:22
      结合工作中的实现,来总结下springmvc的文件上传!以下是对单个文件和多文件上传的例子:

      applicationContext.xml的配置:

  1. <!-- 配置MultipartResolver 用于文件上传 使用spring的CommosMultipartResolver -->    2.     <beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"    3.         p:defaultEncoding="UTF-8"    4.         p:maxUploadSize="5400000"    5.         p:uploadTempDir="fileUpload/temp"    6.      >    7.     </beans:bean>  

     上传的单文件上传的Controller代码

/**     * 上传图片     * @param file     * @return     */    @RequestMapping(value = "/uploadEnquiryCompareImg", method = RequestMethod.POST)    @ResponseBody    public JsonResult<Map<String,String>> uploadEnquiryCompareImg(@RequestParam(value = "file",required = false) MultipartFile file) {        Long size = file.getSize();        if (size == 0l) {            JsonResult<Map<String,String>> jsonResult = new JsonResult<>();            jsonResult.setStatus(JsonResult.FORBID);            jsonResult.setMsg("无图片文件!");            return jsonResult;        }        String filename = file.getOriginalFilename();        String fileType = FileUtil.getFileType(filename);        if (!fileType.toLowerCase().equals("jpg") && !fileType.toLowerCase().equals("png") && !fileType.toLowerCase().equals("jpeg")) {            JsonResult<Map<String,String>> jsonResult = new JsonResult<>();            jsonResult.setStatus(JsonResult.FORBID);            jsonResult.setMsg("图片格式不正确!");            return jsonResult;        }        String attachmentPath = fileUploadWeblogic.uploadWebFile(file, GroupName.USER_BUSI_PUB);        String path = dfsSession.getUserResourcePath() + attachmentPath;        JsonResult<Map<String,String>> jsonResult = new JsonResult<>();        HashMap<String, String> hashMap = new HashMap<>();        hashMap.put("path",path);        hashMap.put("attachmentPath",attachmentPath);        hashMap.put("attachmentType", String.valueOf(AttachmentTypeEnum.PIC.getCode()));        jsonResult.setData(hashMap);        return jsonResult;    }
        以下为多文件上传controller的代码:

  @RequestMapping("filesUpload")      public String filesUpload(@RequestParam("files") MultipartFile[] files) {          //判断file数组不能为空并且长度大于0          if(files!=null&&files.length>0){              //循环获取file数组中得文件              for(int i = 0;i<files.length;i++){                  MultipartFile file = files[i];                  //保存文件                  saveFile(file);              }          }          // 重定向          return "redirect:/list.html";      }  

      以下为html的代码

  1. <body>    2.     <h2>上传多个文件 实例</h2>    3.     4.     5.     <form action="filesUpload.html" method="post"    6.         enctype="multipart/form-data">    7.         <p>    8.             选择文件:<input type="file" name="files">    9.         <p>    10.             选择文件:<input type="file" name="files">    11.         <p>    12.             选择文件:<input type="file" name="files">    13.         <p>    14.             <input type="submit" value="提交">    15.     </form>    16. </body>  
     如果是给app提供接口出现问题,上一篇文章也许能提供帮助!
    


原创粉丝点击