我有一个通过Java Web应用程序生成的JasperReports报告。位于我的本地主机页面上的应用程序:8084 / app / pages.jsp(发送到servlet的表单在哪里)完美地生成了报告,出现了问题,当我生成报告时,将其提供给了site.com/app/pages .jsp得到了这个结果,看来服务器没有解释该文件。 PDF或她的一代。
是运行apache的linux服务器
最佳答案
这来自一个旧项目,您添加response.setContentType("application/pdf");
并将文件的字节写为输出。
public class ReportSintesiServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/pdf");
Lookup<IReportGeneration> l = new Lookup<IReportGeneration>(true, IReportGeneration.class);
IReportGeneration g = null;
try {
g = l.lookup();
} catch (Exception ex) {
return;
}
if (g != null) {
String param = request.getParameter("idc");
System.out.println("genero il report per idc " + param);
Integer idc = Integer.parseInt(param);
byte[] doc = (byte[]) g.generaSintesiAccordoIniziale(idc);
if (doc != null) {
FilePathManager fpm = new FilePathManager();
String pathtofile = fpm.pathToNuovaSintesi(idc);
File temp = new File(pathtofile);
FileUtil.writeBytesToFile(temp, doc);
response.addHeader("Content-Disposition", "attachment; filename=" + temp.getName());
response.setContentLength((int) temp.length());
FileInputStream fileInputStream = new FileInputStream(temp);
OutputStream responseOutputStream = response.getOutputStream();
int bytes;
while ((bytes = fileInputStream.read()) != -1) {
responseOutputStream.write(bytes);
}
}
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}
}
PS:我删除了一些对这种情况没有帮助的代码,因为您会看到您错过了错误管理逻辑的所有部分。