在java中设置响应中的内容标题名称

时间:2015-06-17 09:10:05

标签: java jasper-reports

我编写以下代码以rtf格式编写jasper报告。它运行良好,但它以默认名称file.rtf打开或保存。 我想将名称设置为报告名称。

        oConn = oPool.getConnection();
        ServletContext sServletContext = request.getSession().getServletContext();
        String sReportPath = sServletContext.getRealPath(sFileName);
        JasperReport jReport = (JasperReport) JRLoader.loadObject(sReportPath);
        JasperPrint jPrint = JasperFillManager.fillReport(jReport, hParameters, oConn);
        request.getSession().setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE,jPrint);
        response.setContentType("application/rtf");     
        response.setHeader("Content-disposition","attachment; filename=\"" +sReportName +"\"");
        RequestDispatcher oRequestDispatcher = request.getRequestDispatcher("servlets/rtf");            
        oRequestDispatcher.forward(request, response);

1 个答案:

答案 0 :(得分:0)

检查公共JRViewer - 您可以创建JRViewer(java.lang.String fileName)

或setParameter - 参考代码

String fname= "fileName.jasper";
JasperPrint print = JasperFillManager.fillReport(yourDir+"jasper/"+fname);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE, fname);
porterParameter.OUTPUT_FILE_NAME, nfl);
exporter.exportReport();