1. 程式人生 > >SpringMVC整合Mybatis之檔案上傳與下載

SpringMVC整合Mybatis之檔案上傳與下載

工程結構


第一步:

匯入commons-fileupload-1.3.1.jar和commons-io-2.4.jar以及SpringMVC與Mybatis的整合jar包

第二步:在applicationContext.xml中配置檔案上傳的bean

	<!-- 配置檔案上傳 -->
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="maxUploadSize">
			<!-- 最大上傳大小:5M -->
			<value>5242880</value>
		</property>
	</bean>
單檔案上傳

index.jsp

<!-- 由於上傳檔案,資料型別是複合型別,所以enctype="multipart/form-data -->
  	單檔案上傳<br/><br/>
  	<form action="/springmvc_mybatis_test12_upload/test_upload.action" method="post" enctype="multipart/form-data">
  	<input type="file" name="multipartFile">
  		<input type="submit" value="上傳">
  	</form>
控制器Upload_Download.java
//單檔案上傳,引數必須是MultipartFile型別,用來接收上傳的檔案
	@RequestMapping("test_upload.action")
	public String test_upload(MultipartFile multipartFile) throws Exception{
		if(multipartFile != null){
			//檔案上傳的儲存路徑
			String path="F:\\";
			//圖片的原始名稱(不含路徑)
			String origanlFileName = multipartFile.getOriginalFilename();
			//上傳的圖片的儲存的新名稱,隨機生成檔名稱+.jpg
			String newFileName = UUID.randomUUID()+origanlFileName.substring(origanlFileName.lastIndexOf("."));
			File file = new File(path+newFileName);
			//將記憶體的資料寫到磁碟上
			multipartFile.transferTo(file);
		}
		return "/WEB-INF/jsp/success.jsp";
	}

多檔案上傳:

index.jsp

  	<form action="/springmvc_mybatis_test12_upload/test_uploadMoreFile.action" method="post" enctype="multipart/form-data">
  		<!-- 上傳多個檔案,用List<MultipartFile> pic接收 -->
  		<input type="file" name="pic[0]"><br/><br/>
  		<input type="file" name="pic[1]"><br/><br/>
  		<input type="file" name="pic[2]"><br/><br/>
  		<input type="file" name="pic[3]"><br/><br/>
  		<input type="submit" value="多檔案上傳">
  	</form>
單檔案上傳時,Controller方法中的引數是MultipartFile型別接收上傳的檔案的資訊,多檔案上傳時,可以自定義包裝類將MultipartFile型別進行包裝

MultipartFiles.java

/*
 * 上傳多個檔案,每個檔案都用Controller方法中的MultipartFiles型別的形參接收,所以自定義pojo包裝類
 * */
public class MultipartFiles {
	private List<MultipartFile> pic;//用List接收,每個檔案對應一個MultipartFile

	public List<MultipartFile> getPic() {
		return pic;
	}

	public void setPic(List<MultipartFile> pic) {
		this.pic = pic;
	}
	
}
Upload_Download.java
	//多檔案上傳,MultipartFiles是自定義包裝類,用List<MultipartFile>接收
	@RequestMapping("/test_uploadMoreFile.action")
	public String test_uploadMoreFile(MultipartFiles multipartFiles) throws Exception{
		
		List<MultipartFile> list = multipartFiles.getPic();	//得到上傳的檔案連結串列
		for(MultipartFile multipartFile:list){
			//上傳圖片的儲存路徑
			String path = "F:\\";
			//圖片的原始名稱(不含路徑)
			String origanlFileName = multipartFile.getOriginalFilename();
			//新圖片的名稱,隨機生成檔名稱+.jpg
			String newFileName = UUID.randomUUID()+origanlFileName.substring(origanlFileName.lastIndexOf("."));
			//新圖片
			File file = new File(path+newFileName);
			//將記憶體中的資料寫入磁碟
			multipartFile.transferTo(file);
		}
		return "/WEB-INF/jsp/success.jsp";
	}


檔案下載

Upload_Download.java

//檔案下載
	//請求<a href="/springmvc_mybatis_test12_upload/test_download.action?filename=趙奕歡.jpg">點選下載資源</a>
	@RequestMapping("/test_download.action")
	//String filename 是請求引數
	public String test_download(HttpServletRequest request,HttpServletResponse response,String filename) throws Exception{
		
		filename = new String(filename.getBytes("iso-8859-1"),"UTF-8");//get提交,修改編碼
		
		String path = request.getRealPath("/WEB-INF/picture/"+filename);//得到資源在硬碟上的絕對的路徑	
		File file = new File(path);
		response.setCharacterEncoding("UTF-8");
		filename = URLEncoder.encode(filename, "UTF-8");//將中文轉為瀏覽器可認識的編碼
		response.setHeader("content-disposition", "attachment;filename="+filename);//設定響應頭為檔案下載
		response.setContentLength((int)file.length());
		int len = 0;
		byte []buffer = new byte[1024];
		InputStream is = new FileInputStream(file);
		OutputStream os= response.getOutputStream();//向瀏覽器寫資料
		while((len = is.read(buffer)) != -1){
			os.write(buffer,0,len);
		}
		is.close();
		os.close();
		return null;	//一定要返回null,執行後不跳轉
	}