简体   繁体   English

在服务器上存储文件的同时下载文件? 如何将文件的下载功能提供给客户端以在服务器上存储文件?

[英]download file withough storing file on server? How to give download functionality of file to client withought storing file on server?

I am using jasper reports in my liferay portlet and I want that user download the reports directly from the output stream. 我在liferay portlet中使用jasper报告,我希望该用户直接从输出流下载报告。 I don't want to store the reports pdf file on my server. 我不想将报告pdf文件存储在我的服务器上。

How can I do this with the jasper report? 如何使用jasper报告执行此操作?

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

Right now I'm doing this which generate pdf file in some directory and then I provide download link to user. 现在,我正在执行此操作,从而在某个目录中生成pdf文件,然后向用户提供下载链接。 But I want that user just download the pdf from direct ouptut stream. 但我希望该用户仅从直接ouptut流下载pdf。

In your portlet action: 在您的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();

}

Then create the resource download url like this: 然后创建资源下载网址,如下所示:

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

If I understood the question correctly you coud try 如果我正确理解了这个问题,您可以尝试

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM