springmvc文件上傳下載實現起來非常簡單,此springmvc上傳下載案例適合已經搭建好的ssm框架(spring+springmvc+mybatis)使用,ssm框架項目的搭建我相信你們已經搭建好了,這里不再贅述,下面就開始吧!
ssm框架整合詳情請看:http://www.tpyyes.com/a/javaweb/2016/1103/23.html
1.首先我們創建一個測試用的jsp頁面,代碼如下。
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 7 <title>文件上傳下載</title> 8 </head> 9 <body> 10 <form action="http://localhost:8080/uploadDemo/rest/file/upload" method="post" enctype="multipart/form-data"> 11 選擇文件:<input type="file" name="file" width="120px"> 12 <input type="submit" value="上傳"> 13 </form> 14 <hr> 15 <form action="http://localhost:8080/uploadDemo/rest/file/down" method="get"> 16 <input type="submit" value="下載"> 17 </form> 18 </body> 19 </html>
2.在我們的maven項目的pom.xml文件中添加fileupload文件上傳下載jar包,不然后面的操作可能會報錯,如下。
1 <!-- 文件上傳 --> 2 <dependency> 3 <groupId>commons-fileupload</groupId> 4 <artifactId>commons-fileupload</artifactId> 5 <version>1.3</version> 6 </dependency>
3.在spring的servlet視圖解析器下面定義CommonsMultipartResolver文件解析器,就是加入這個的時候運行項目,如果沒有fileuload相關的jar包就會報錯。
1 <!-- 定義文件解釋器 --> 2 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 3 <!-- 設置默認編碼 --> 4 <property name="defaultEncoding" value="utf-8"></property> 5 <!-- 上傳圖片最大大小5M--> 6 <property name="maxUploadSize" value="5242440"></property> 7 </bean>
4.在controller層寫上springmvc上傳下載的代碼,如下。
1 package com.baidu; 2 @RequestMapping("file") 3 @Controller 4 public class FileController { 5 /** 6 * 文件上傳功能 7 * @param file 8 * @return 9 * @throws IOException 10 */ 11 @RequestMapping(value="/upload",method=RequestMethod.POST) 12 @ResponseBody 13 public String upload(MultipartFile file,HttpServletRequest request) throws IOException{ 14 String path = request.getSession().getServletContext().getRealPath("upload"); 15 String fileName = file.getOriginalFilename(); 16 File dir = new File(path,fileName); 17 if(!dir.exists()){ 18 dir.mkdirs(); 19 } 20 //MultipartFile自帶的解析方法 21 file.transferTo(dir); 22 return "ok!"; 23 } 24 25 /** 26 * 文件下載功能 27 * @param request 28 * @param response 29 * @throws Exception 30 */ 31 @RequestMapping("/down") 32 public void down(HttpServletRequest request,HttpServletResponse response) throws Exception{ 33 //模擬文件,myfile.txt為需要下載的文件 34 String fileName = request.getSession().getServletContext().getRealPath("upload")+"/myfile.txt"; 35 //獲取輸入流 36 InputStream bis = new BufferedInputStream(new FileInputStream(new File(fileName))); 37 //假如以中文名下載的話 38 String filename = "下載文件.txt"; 39 //轉碼,免得文件名中文亂碼 40 filename = URLEncoder.encode(filename,"UTF-8"); 41 //設置文件下載頭 42 response.addHeader("Content-Disposition", "attachment;filename=" + filename); 43 //1.設置文件ContentType類型,這樣設置,會自動判斷下載文件類型 44 response.setContentType("multipart/form-data"); 45 BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream()); 46 int len = 0; 47 while((len = bis.read()) != -1){ 48 out.write(len); 49 out.flush(); 50 } 51 out.close(); 52 } 53 }
springmvc上傳下載很方便,代碼直接復制使用。