get请求
export function getPdf(id) {
const InfoRequestDTO = {
consumer: store.getters.QLMConfig.application_id,
language: store.getters.language,
serialNo: uuid.v1(),
tag: id
};
return request({
url: store.getters.QLMConfig.qlm_gateway_url + "/specialwork/ZxjlGxcx/getPdf",
method: 'get',
params:InfoRequestDTO,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
responseType: 'arraybuffer'
})
}
调用:
daochu(id) {
this.loading = true;
getPdf(id).then(
data => {
this.loading = false;
let blob = new Blob([data], { type: 'application/vnd.ms-pdf' })
let objectUrl = window.URL.createObjectURL(blob) // 创建URL
let link = document.createElement('a')
link.style.display = 'none'
link.href = objectUrl
link.download = '突出贡献与创新申报表.pdf' // 自定义文件名
document.body.appendChild(link)
link.click() // 下载文件
window.URL.revokeObjectURL(objectUrl) // 释放内存
// const url = window.URL.createObjectURL(new Blob([data], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" }))
// const link = document.createElement('a')
// link.style.display = 'none'
// link.href = url;
// link.setAttribute('download', '突出贡献与创新申报表.pdf')
// document.body.appendChild(link)
// link.click()
// document.body.removeChild(link)
}
)
},
后端:
Controller
/**
* 导出申报表
*
* @param response
* @return
*/
@ApiOperation(value = "导出申报表", notes = "导出申报表")
@GetMapping(value = "/getPdf")
public void getPdf(HttpServletRequest request, HttpServletResponse response) {
// Map<String, Object> root = new HashMap<String, Object>(); // 创建数据模型
try {
String fileName = System.currentTimeMillis() + (int) (Math.random() * 90000 + 10000) + "";
printFile("ndbg.ftl", root, fileName + ".doc", "C://specialworktempfile/", "C://specialworktempfile/");
new PdfConverter().doc2pdf("C://" + fileName + ".doc", "C://" + fileName + ".pdf");
//pdf
String pdffilePath = "C://" + fileName + ".pdf";
File pdffile = new File(pdffilePath);
//doc
String docfilePath = "C://" + fileName + ".doc";
File docfile = new File(docfilePath);
if (pdffile.exists()) {
FileDownload.fileDownload(response, "C://" + fileName + ".pdf", "申报表" + ".pdf");
pdffile.delete();
} else {
}
if (docfile.exists()) {
docfile.delete();
} else {
}
} catch (Exception e) {
e.printStackTrace();
}
}
工具类中核心代码
/**
* @param response
* @param filePath //文件完整路径(包括文件名和扩展名)
* @param fileName //下载后看到的文件名
* @return 文件名
*/
public static void fileDownload(final HttpServletResponse response, String filePath, String fileName) throws Exception {
if (fileName == null) {
fileName = filePath.substring(filePath.lastIndexOf("/") + 1, filePath.length());
}
File file = new File(filePath);
if (file.exists()) {
byte[] data = FileUtil.toByteArray2(filePath);
fileName = URLEncoder.encode(fileName, "UTF-8");
//fileName = new String(fileName.getBytes(), "GBK");
response.reset();
response.addHeader("Access-Control-Allow-Origin", "*");
response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
response.addHeader("Access-Control-Allow-Headers", "Content-Type");
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
response.addHeader("Content-Length", "" + data.length);
//response.setContentType("Content-Type:application/force-download");
response.setContentType("application/octet-stream;charset=UTF-8");
OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
outputStream.write(data);
outputStream.flush();
outputStream.close();
response.flushBuffer();
} else {
throw new RuntimeException("未找到文件");
}
}
网友评论