vue方法 java方法 ...
vue方法
handleDownTemplateXls(fileName){
if(!fileName || typeof fileName != "string"){
fileName = "導入模板"
}
let param = {...this.queryParam};
if(this.selectedRowKeys && this.selectedRowKeys.length>0){
param['selections'] = this.selectedRowKeys.join(",")
}
console.log("下載模板參數",param)
downFile(this.url.downTemplateXlsUrl,param).then((data)=>{
if (!data) {
this.$message.warning("文件下載失敗")
return
}
if (typeof window.navigator.msSaveBlob !== 'undefined') {
window.navigator.msSaveBlob(new Blob([data]), fileName+'.xls')
}else{
let url = window.URL.createObjectURL(new Blob([data]))
let link = document.createElement('a')
link.style.display = 'none'
link.href = url
link.setAttribute('download', fileName+'.xls')
document.body.appendChild(link)
link.click()
document.body.removeChild(link); //下載完成移除元素
window.URL.revokeObjectURL(url); //釋放掉blob對象
}
})
},
java方法
application-dev.yml
#xinhua專用配置
xinhua :
path :
#文件上傳根目錄 設置
upload: /Users/zhtony/project/xinhua_git/upFiles
#webapp文件路徑
webapp: /Users/zhtony/project/xinhua_git/webapp
#導入書籍模板文件
book: /Users/zhtony/project/xinhua_git/files/導入書籍模板.xls
#導入學校模板文件
school: /Users/zhtony/project/xinhua_git/files/導入學校模板.xls
@Value(value = "${xinhua.path.book}")
private String uploadBookPath;
@RequestMapping(value = "/downTemplate")
public void downTemplate(HttpServletRequest request, HttpServletResponse response) {
try {
// path是指欲下載的文件的路徑。
File file = new File(uploadBookPath);
// 取得文件名。
String filename = file.getName();
// 取得文件的尾碼名。
String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
// 以流的形式下載文件。
InputStream fis;
fis = new BufferedInputStream(new FileInputStream(uploadBookPath));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
// 設置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
response.addHeader("Content-Length", "" + file.length());
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}