Springmvc的文件下載

1.前臺實現

所謂的前臺實現非常簡單,就是給前臺的下載按鈕追加一個點擊事件;

當我們點擊這個下載按鈕的時候。開始下載文件。

 

				$("#downTemplete").linkbutton({    
				    iconCls: 'icon-excel',
				    onClick:function(){
				    	location.href='questions/single/downImportBatchTemplete.do';
				    	$('#singleDialog').dialog('close')
				    }
				});  


如上,我前臺使用的easyUI,所以代碼如上。

 

 

2.服務端實現

創建一個方法

 

	@RequestMapping("/downImportBatchTemplete.do")
	@ResponseBody
	public ResponseEntity<byte[]> downImportBatchTemplete(HttpServletRequest request) throws Exception{
		
		String path = request.getServletContext().getRealPath("/upload/templete/templete.zip");
		File file = new File(path) ;
		FileInputStream fis = new FileInputStream(file) ;
		byte[] bs = new byte[(int) file.length()] ;
		fis.read(bs);
		HttpHeaders h = new HttpHeaders() ;
		h.add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
		h.add("Content-Disposition", "attchement;filename="+file.getName());
		
		ResponseEntity<byte[]> re = new ResponseEntity<byte[]>(bs,h,HttpStatus.OK) ;
		fis.close();
		return re ;
	}

 

 

3.完成

 

 

發佈了42 篇原創文章 · 獲贊 17 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章