我通过 Java Web应用程序生成了 JasperReports 报告。位于我的localhost页面的应用程序:8084 / app / pages.jsp(发送到servlet的表单在哪里)完美地生成报告,问题发生在我生成报告时,我的site.com / app / pages .jsp得到了这个结果,似乎服务器没有解释该文件。 pdf或她的一代。
是运行apache的linux服务器
答案 0 :(得分:1)
这来自一个旧项目,你添加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:我删除了一些在这种情况下无用的代码,因为您可以看到错过了错误管理逻辑的所有部分。