2012-12-21 178 views
0

我在我的liferay portlet中使用jasper報告,我希望該用戶直接從輸出流中下載報告。我不想將報告pdf文件存儲在我的服務器上。下載文件存儲在服務器上的文件?如何將文件的下載功能提供給客戶端,並在服務器上存儲文件?

我如何用碧玉報告做到這一點?

File pdf = File.createTempFile("output.", ".pdf"); 
JasperExportManager.exportReportToPdfStream(jasperPrint, new FileOutputStream(pdf)); 

現在我正在做這個在某些目錄中生成pdf文件,然後我提供下載鏈接給用戶。但我希望該用戶只需從直接輸出流下載PDF。

回答

0

如果我理解正確的問題,你coud嘗試

ByteArrayOutputStream bous = new ByteArrayOutputStream(); 
    JasperExportManager.exportReportToPdfStream(jasperPrint, bous); 
    byte[] bytes = bous.toByteArray(); 
1

在portlet動作:

public void serveResource(ResourceRequest request,ResourceResponse response) throws PortletException, IOException { 

    response.setContentType("application/application-download"); 
    response.setProperty("Content-disposition", "attachement; filename=<some file.pdf>"); 

    OutputStream responseStream = response.getPortletOutputStream(); 

    jasperPrint=... 
    JasperExportManager.exportReportToPdfStream(jasperPrint, responseStream); 

    responseStream.flush(); 
    responseStream.close(); 

} 

然後創建資源下載網址是這樣的:

<a href="<portlet:resourceURL/>">Download Report</a> 
+0

..just這個我想在我的serveresource方法中寫入這個後在ma jsp頁面中寫入? Download Report

+0

這會拋出java.lang.IllegalStateException之類的錯誤:無法獲取OutputStream,因爲Writer已在使用中 –

相關問題