【问题标题】:download report as pdf using servlet使用 servlet 将报告下载为 pdf
【发布时间】:2012-05-04 13:31:38
【问题描述】:

我的 POJO 类中有 pdf 文件位置和 pdf 文件。我想使用 servlet 下载你的 pdf。请告诉我一些完成它的方法。 文件位置=/tmp/SWBC_444Thu May 03 20:01:07 IST 20124366242221752147545.pdf 使用此文件位置,我想提示用户将文件下载为 pdf。

这是我的代码。

File file = new File(filePath);
  OutputStream responseOutputStream = response.getOutputStream(); 
  response.setContentLength((int)filePath.length());
  FileInputStream fileInputStream = new FileInputStream(file);
  int size = fileInputStream.available();
  byte[] content = new byte[size];
  int bytesRead;
  while ((bytesRead = fileInputStream.read(content)) != -1)   
  {  
   responseOutputStream.write(content, 0, bytesRead);  
  }
  responseOutputStream.flush();
  fileInputStream.close();
  responseOutputStream.close(); 

。我读取并生成文件,但打开文件时它是空的。

谢谢你..!

【问题讨论】:

  • 您可以查看此帖子:JasperReports: Calling report from servlet & Report download not prompting user to save。在 SO 上的搜索效果很好;)
  • 文件 file = new File(filePath); ServletOutputStream servletOutputStream = response.getOutputStream(); ; BufferedInputStream bufferIput = null; FileInputStream fileInputStream= new FileInputStream(file); bufferIput = new BufferedInputStream(fileInputStream); byte[] bBuffer = new byte[fileInputStream.available()]; int nBytes = -1; while ((nBytes = bufferIput.read(bBuffer, 0, bBuffer.length)) != -1) { servletOutputStream.write(bBuffer, 0, nBytes); } 问题解决了。您可以使用此代码读取 pdf 文件并下载
  • 您可以发布解决方案作为帮助他人的答案

标签: servlets jasper-reports pdf-generation


【解决方案1】:

httpservletresponse.setHeader("Content-disposition", "attachment; filename=\"" + title + ".pdf\"");应该做的

【讨论】:

  • 我做到了..我的意思是它是 pdf 的基本内容。无论如何,现在我从指定的文件位置读取 pdf 文件并生成 pdf 文件,但是当我打开文件时,它不显示任何内容,文件
猜你喜欢
  • 2020-08-05
  • 2021-11-09
  • 1970-01-01
  • 1970-01-01
  • 2011-12-29
  • 1970-01-01
  • 2010-10-22
  • 1970-01-01
  • 2014-01-04
相关资源
最近更新 更多