我对PDF base64字符串有疑问,我不知道为什么文件名没有出现在pdf查看器中,而不是文件名我有一组字符,我认为这些字符与字节数组有关。 / p>
服务器端
@RequestMapping(value = "/report", produces = "application/pdf")
public @ResponseBody HttpEntity<byte[]> reportPdf(HttpServletRequest request, HttpServletResponse response)
...
byte[] document = report.getBytes(StandardCharsets.UTF_8);
...
HttpHeaders header = new HttpHeaders();
header.setContentType(new MediaType("application", "pdf"));
header.set("Content-Disposition", "inline; filename=asdfasdfasdf.pdf");
header.setContentLength(document.length);
return new HttpEntity<byte[]>(document,header);
我通过JavaScript应用程序调用此REST服务,我不知道如何控制此文件名。
有人能给我任何线索吗?在这种情况下可以更改这个新文件名吗?。
问候!
答案 0 :(得分:0)
url编码文件名更安全。
String fileName = "asdfasdfasdf.pdf";
URLEncoder.encode(fileName, "utf-8");
HttpHeaders header = new HttpHeaders();
header.setContentType("application/pdf");
header.set("Content-Disposition", "inline; filename=" + fileName);
或者您甚至可以尝试将文件名放在双引号""
HttpHeaders header = new HttpHeaders();
header.setContentType("application/pdf");
header.set("Content-Disposition", "inline; filename=\"asdfasdfasdf.pdf\"");
答案 1 :(得分:0)
最后,我找到了类似的解决方法:
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
//Setting up Jasper print
JasperPrint print = getJasperPrint(REPORTS_PATH + reportPath, result, params);
//Setting up Exporter configuration
SimplePdfExporterConfiguration pdfExportConfiguration = new SimplePdfExporterConfiguration();
pdfExportConfiguration.setMetadataTitle(title);
//Setting up Jasper exporter
JRPdfExporter exporter = new JRPdfExporter();
exporter.setExporterInput(new SimpleExporterInput(print));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outputStream));
exporter.setConfiguration(pdfExportConfiguration);
//Exporting pdf
exporter.exportReport();
enter code here
使用SimplePdfExporterConfiguration,您将在报告中添加额外的信息,因此PDF查看器显示正确的名称而不是字节数组代码。