SpringMVC上下文默認中沒有裝配MultipartResolver,所以默認狀況不能處理文件上傳和下載。若是想使用Spring的文件上傳功能,則須要在上下文中配置MultipartResolver前端
前端表單要求:java
將表單的method設置未POST,並將enctype設置未multipart/form-data。這樣瀏覽器纔會把用戶選擇的文件以二進制數據發送給服務器。web
enctype屬性:spring
<dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.4</version> </dependency>
注意:bean的id必須爲:multipartReolver,不然上傳文件會報錯!瀏覽器
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="utf-8"/> <!-- 上傳文件大小上限,單位爲字節(10485760=10M) --> <property name="maxUploadSize" value="10485760"/> <property name="maxInMemorySize" value="1024"/> </bean>
<form action="${pageContext.request.contextPath}/upload" enctype="multipart/form-data" method="post"> <input type="file" name="file"><br> <input type="submit" value="上傳" name="upload"> </form>
package com.star.controller; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.commons.CommonsMultipartFile; import javax.servlet.http.HttpServletRequest; import java.io.*; @RestController public class fileController { @PostMapping("/upload") public String fileUpload(@RequestParam("file") CommonsMultipartFile file , HttpServletRequest request) throws IOException { //獲取文件名 String uploadFileName = file.getOriginalFilename(); //若是文件名爲空,直接回到首頁! if(uploadFileName==null){ return "redirect:/"; } //設置上傳路徑 String path = request.getServletContext().getRealPath("/upload"); //若是路徑不存在,建立一個 File realPath = new File(path); if(!realPath.exists()){ realPath.mkdir(); } //文件輸入流 InputStream is = file.getInputStream(); //文件輸出流 輸出到上傳路徑下的與該文件名相同的文件中 OutputStream os = new FileOutputStream(new File(realPath, uploadFileName)); //讀取寫出 byte[] bytes = new byte[1024]; int len=0; while ((len = is.read(bytes))!=-1){ os.write(bytes,0,len); os.flush(); } os.close(); is.close(); return "Ok!"; } }
OK!文件上傳成功!緩存
編寫controller服務器
@PostMapping("/upload2") public String fileUpload2(@RequestParam("file") CommonsMultipartFile file , HttpServletRequest request) throws IOException { //設置上傳路徑 String path = request.getServletContext().getRealPath("/upload"); //若是路徑不存在,建立一個 File realPath = new File(path); if(!realPath.exists()){ realPath.mkdir(); } //經過CommonsMultipartFile的方法直接寫文件(注意這個時候) file.transferTo(new File(realPath,file.getOriginalFilename())); return "OK!"; }
文件下載步驟:app
代碼實現:jsp
@RequestMapping("/downLoad") public String fileDownLoad(HttpServletRequest request, HttpServletResponse response) throws Exception { //要下載的圖片地址 String path = request.getServletContext().getRealPath("/statics"); String fileName = "KDA女團.jpg"; response.reset();//設置頁面不緩存,清空buffer response.setCharacterEncoding("utf-8");//字符編碼 response.setContentType("multipart/form-data");//二進制傳輸數據 //設置響應頭 response.setHeader("Content-Disposition", "attachment;fileName="+ URLEncoder.encode(fileName, "UTF-8")); File file = new File(path, fileName); //文件輸入流 InputStream is = new FileInputStream(file); //文件輸入流 OutputStream os = response.getOutputStream(); int len=0; byte[] bytes = new byte[1024]; while ((len=is.read(bytes))!=-1){ os.write(bytes,0,len); os.flush(); } is.close(); os.close(); return ""; }
前端:post
<a href="${pageContext.request.contextPath}/downLoad">點擊下載文件</a>
啓動項目盡心測試:
文件下載OK!