2017-04-19 32 views
0

我對PDF base64字符串有疑問,我不知道爲什麼文件名不出現在PDF查看器中,而不是文件名我有一組我認爲與字符串有關的字符字節數組。PDF base64 string文件名

服務器端

@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); 

I'm調用這個REST服務throught JavaScript的應用程序,我不知道我能做些什麼來控制這個文件名。

enter image description here

誰能給我任何線索?在這種情況下,可以改變這個新的文件名嗎?

問候!

回答

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\""); 
+0

它不工作,你知道如果標題信息包含在數據URI內嗎? –

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查看器顯示正確的名稱而不是字節數組代碼。