java代码
try {
//获取文件路径
String pdf = invWriteOffService.getWriteOffPdf(invWriteOffModel);
File file = new File(pdf);
//设置数据类型,好让客户端浏览器区分不同种类的数据
response.setContentType("text/xml;charset=utf-8");
//设置编码格式
response.setCharacterEncoding("UTF-8");
//清除头和状态码,任何打开流关闭之后都不在reset
response.reset();
//将文件流写入浏览器
response.setHeader("Content-disposition", "attachment;filename=" + ExcelTemplateUtil.convertFileName(invWriteOffModel.getPeriod() + "-测试.pdf"));
ServletOutputStream outputStream = response.getOutputStream();
FileInputStream fileInputStream = new FileInputStream(file);
ExcelTemplateUtil.copyStream(fileInputStream, outputStream, true);
fileInputStream.close();
outputStream.close();
fileInputStream = null;
outputStream = null;
file = null;
}catch (Exception e){
e.printStackTrace();
}
前端代码
stampAcc(){
this.loadings = true
let param = {
packNo:this.packNo,
reimNum: this.queryParam.reimNum
}
this.$http({url:this.url.fileListPdfUrl,
//请求方式
method:'get',
//设置响应类型
responseType:'arraybuffer',
params:param}).then(res =>{
const binaryData = [];
binaryData.push(res)
//获取blob链接
this.pdfUrl = window.URL.createObjectURL(new Blob(binaryData, { type: 'application/pdf' }));
window.open(this.pdfUrl);
this.loadings = false
})
},