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.完成