我想访问我的程序,并在浏览器中获得pdf数据显示,如下所示:

但是你可以看到左上角是我的视图路径,而不是我的文件名,我怎么才能改变它?
src如下:
@GetMapping("/getPDF")
public ResponseEntity<byte[]> getPDF() throws IOException {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.parseMediaType("application/pdf"));
String filename = "测试.pdf";
File file = new File("E:\\2018版计信本科人才培养方案总2019.11.15(适用于2018级及以后年级)(1).pdf");
headers.add("content-disposition", "inline;filename=" + URLEncoder.encode(filename,"UTF-8"));
headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(Files.readAllBytes(file.toPath()), headers, HttpStatus.OK);
return response;
}发布于 2020-10-26 17:21:56
成功了!
您可以使用一些工具,如pdfbox和使用流输出
核心src如下:
File file = new File(pathPlace);
if (!file.exists()) {
throw new MyException(ResultEnum.FILE_EXIST);
}
String fileName = saveFileDirectoryMapper.getFileNameById(fileId);
response.setHeader("content-disposition", "inline;filename=" + URLEncoder.encode(fileName, "UTF-8"));
try (OutputStream out = response.getOutputStream();
// load pdf
PDDocument document = PDDocument.load(file)) {
// get document attribute
PDDocumentInformation info = document.getDocumentInformation();
// change title
info.setTitle(fileName);
document.setDocumentInformation(info);
// output data to stream
document.save(out);
}https://stackoverflow.com/questions/64534283
复制相似问题