2012-12-21 170 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已在使用中 –

相关问题