公司的項目不少地方要用到文件上傳,之前的上傳主要是用apache的fileupload ,使用的感覺並不太好。今天試了試spring的MultipartFile,感受還不錯,封裝的比較簡潔。前端
固然,中間也排除了很多坑。jquery
1. 配置CommonsMultipartResolverweb
1 <!-- 配置MultipartResolver 用於文件上傳 使用spring的CommosMultipartResolver --> 2 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 3 <property name="defaultEncoding" value="UTF-8"/> 4 <property name="maxUploadSize" value="209715200"/> 5 <property name="maxInMemorySize" value="4096"/> 6 <property name="uploadTempDir" value="fileUpload/temp"/> 7 </bean>
defaultEncoding="UTF-8" 是請求的編碼格式,默認爲iso-8859-1
maxUploadSize="209715200" 是上傳文件的總的大小,單位爲字節,我設置的是200M,
uploadTempDir="fileUpload/temp" 爲上傳文件的臨時路徑ajax
2. 添加apache的 fileupload spring
1 <dependency> 2 <groupId>commons-fileupload</groupId> 3 <artifactId>commons-fileupload</artifactId> 4 <version>1.2.2</version> 5 </dependency>
應該是multipartfile方式上傳底層也會用到commons-fileuploadapache
3.前端用了jquery file uploadapp
1 <span class="btn btn-success fileinput-button"> 2 <i class="glyphicon glyphicon-plus"></i> 3 <span>選擇文件...</span> 4 <!-- The file input field used as target for the file upload widget --> 5 <input id="fileupload" type="file" name="file[]" multiple> 6 </span> 7 #jquery file upload 沒有采用form表單上傳,是ajax方式向後臺發送的請求,
#若是是以表單形式提交上傳請求,不要忘了加上enctype="multipart/form-data"
8 ... 9 10 $('#fileupload').fileupload({ 11 url: "<%=request.getContextPath()%>/aaa/upload.do", 12 sequentialUploads: true, 13 singleFileUploads: false, 14 add: function (e, data) { 15 data.context = $('<button/>').text('Upload') 16 .appendTo(document.body) 17 .click(function () { 18 data.context = $('<p/>').text('Uploading...').replaceAll($(this)); 19 data.submit(); 20 }); 21 }, 22 done: function (e, data) { 23 $.each(data.files, function (index, file) { 24 $('<p/>').text(file.name).appendTo(document.body); 25 });26 } 27 });
jquery file upload 看起來還不錯,就是中文的資料仍是有點少,官網看的頭大。this
4.後臺是咱們的重點,其實我想說,我原本只是想週末找一個好用,好看的前端上傳插件的,結果重點弄到後臺來了。。編碼
因爲配置了CommonsMultipartResolver,文件上傳請求HttpServletRequest會被解析爲MultipartHttpServletRequesturl
1 /** 2 * 多文件上傳 3 * 4 * @param multipartRequest 5 * @return 6 * @throws IOException 7 */ 8 @ResponseBody 9 @RequestMapping(value = "upload", method = RequestMethod.POST) 10 public String handleImport(DefaultMultipartHttpServletRequest multipartRequest) throws IOException { 11 if (multipartRequest != null) { 12 Iterator<String> iterator = multipartRequest.getFileNames(); 13 while (iterator.hasNext()) { 14 15 // //單文件上傳 。 16 // MultipartFile file = multipartRequest.getFile(iterator.next());//一次傳一個文件 17 // if (StringUtils.hasText(file.getOriginalFilename())) { 18 // file.transferTo(new File("E:/upload_" + file.getOriginalFilename())); 19 // } 20 21 //多文件上傳 22 List<MultipartFile> fileList = multipartRequest.getFiles(iterator.next()); //一次選多個文件上傳 23 for (MultipartFile file : fileList) { 24 if (StringUtils.hasText(file.getOriginalFilename())) { 25 file.transferTo(new File("E:/upload_" + file.getOriginalFilename())); 26 } 27 } 28 } 29 } 30 return "success"; 31 }