前端代码如下:
1.发送请求时进行设置:
handleDetail(e){
let id = e.id
this.axios({
//设置请求路径并传递参数
url:this.url.getPoi+id,
//设置请求的方法
method:'post',
//设置响应体类型为blob
responseType: 'blob',
//设置格式
type: 'multipart/form-data'
}).then(res => {
//浏览器界面展示的文件名
const fileName = e.contractNumber+ '.pdf'
const elink = document.createElement('a')
elink.download = fileName
elink.style.display = 'none'
let binaryData = [];
binaryData.push(res);
elink.href = window.URL.createObjectURL(new Blob(binaryData));
document.body.appendChild(elink);
elink.click();
URL.revokeObjectURL(elink.href); // 释放URL 对象
document.body.removeChild(elink);
this.$message.success("下载成功");
})
}
后端代码:
File file1=new File(File1); InputStream inputStream=new FileInputStream(file1); //再次去读取到本地的pdf文件 int len; byte[] buf=new byte[1024]; OutputStream stream=httpServletResponse.getOutputStream(); while ((len=inputStream.read(buf))!=-1){ stream.write(buf,0,len); } inputStream.close(); httpServletResponse.setContentType("pdf/test.pdf"+";charset=utf-8"); stream.flush(); stream.close();