说明: 1、 案例实现了一种简单的方式,即当没有生成过报表时,则生成报表,否则显示报表。这样的改动最小。 2、 如果希望生成报表和显示报表完全分开,需要给report组件增加一个方法,可以仿照refresh方法写,再仿照ReportBrowse.j写一个.j文件来实现, 3、 案例中对report组件id等于grid的报表进行了单独处理,会将报表相关的4个文件一次生成,以后则会直接显示。关于生成的这4个文件的文件名取自报表组件中的reportName属性,这4个文件存放的目录暂时是tomcat的temp目录,这个目录应该修改为一个合适的目录。 ReportBrowse.java代码如下 - public class ReportBrowse extends JProcessorImpl {
- ……
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- try {
- //初始化报表服务器地址(UIServer/justep.xml)
- init(request);
- businessCookie = getBusinessSessionID(request).toString();
- String tempUrl = request.getRequestURL().toString();
- String uiServerURL = tempUrl.substring(0, tempUrl.indexOf("/UI/"));
- String jsonString = getRequestJSONString(request);
- // rdl和json的数据整合,(sql与页面设置)
- ReportJSONHelper reportJSONHelper = new ReportJSONHelper(jsonString, (String) businessCookie, uiServerURL, REPORT_SERVER);
- Element requestE = reportJSONHelper.execute(null);
- String outputType = reportJSONHelper.getOutputType();
- String reportName = reportJSONHelper.getReportName();
- String reportID = reportJSONHelper.getReportID();
- String fileDir;
- String filename;
- String filenameEncode;
- String filenamedownload;
- if ("grid".equals(reportID)) {//只有报表组件的ID是grid的报表生成指定名称的报表文件
- fileDir = System.getProperty("java.io.tmpdir");//需要保存的报表文件,应该放到其它目录中
- //用reportName生成文件名
- filename = reportName + "." + outputType;
- filenameEncode = java.net.URLEncoder.encode(reportName, "utf-8") + "." + outputType;
- if ("preview".equals(outputType) || "print".equals(outputType)) {
- filename = reportName + ".pdf";
- filenameEncode = java.net.URLEncoder.encode(reportName, "utf-8") + ".pdf";
- }
- filenamedownload = filename;
- }
- else{//其它报表用GUID作为文件名
- fileDir = System.getProperty("java.io.tmpdir");
- filename = java.util.UUID.randomUUID().toString();
- if ("preview".equals(outputType) || "print".equals(outputType)) {
- filename = filename + ".pdf";
- filenamedownload = reportName + ".pdf";
- }else{
- filename = filename + "." + outputType;
- filenamedownload = reportName + "." + outputType;;
- }
- filenameEncode = filename;
- }
- String fullFileName = fileDir + "/" + filename;
- File reportFile = new File(fullFileName);
-
- if ("grid".equals(reportID)) {//只有报表组件的ID是grid的报表才一次生成4个文件
- if (!reportFile.exists()) {//如果文件不存在则生成4个报表文件
- createReportFile(reportJSONHelper, "xhtml", fileDir + "/" + reportName + ".xhtml");
- createReportFile(reportJSONHelper, "doc", fileDir + "/" + reportName + ".doc");
- createReportFile(reportJSONHelper, "xls", fileDir + "/" + reportName + ".xls");
- createReportFile(reportJSONHelper, "pdf", fileDir + "/" + reportName + ".pdf");
- }
- }else{
- if ("preview".equals(outputType) || "print".equals(outputType)) {
- createReportFile(reportJSONHelper, "pdf", fullFileName);
- }else{
- createReportFile(reportJSONHelper, outputType, fullFileName);
- }
- }
- //将报表文件传回界面
- if ("print".equals(outputType)) {
- PrintWriter pw = response.getWriter();
- pw.print("<object id='pdf' type='application/pdf' width='100' height='100'>" +
- "<param name='src' fmt:url-type='action' value='"+uiServerURL+"/UI/system/service/report/printGetFile.j?filename="
- + fileDir + "/" + filenameEncode+"' />" +
- "</object>" +
- "<script type='text/javascript'>" +
- "document.pdf.printWithDialog();" +
- "document.pdf.focus();" +
- "</script>" +
- "");
- pw.flush();
- pw.close();
- } else {
- FileInputStream inputStream = new FileInputStream(reportFile);
- response.setCharacterEncoding("UTF-8");
-
- if ("xhtml".equals(outputType)) {
- response.setHeader("Cache-Control", "no-cache");
- response.setHeader("Content-Type", "text/html");
- } else if ("doc".equals(outputType)) {
- response.setHeader("Cache-Control", "no-cache");
- response.setHeader("Content-Type", "application/ms-word");
- response.addHeader("Content-disposition", "attachment;filename="" + filenamedownload + """);
- } else if ("xls".equals(outputType)) {
- response.setHeader("Cache-Control", "no-cache");
- response.setHeader("Content-Type", "application/ms-excel");
- response.addHeader("Content-disposition", "attachment;filename="" + filenamedownload + """);
- } else if ("preview".equals(outputType)) {
- response.setHeader("Content-Type", "application/pdf");
- response.addHeader("Content-disposition", "inline;filename="" + filenamedownload + """);
- } else {
- response.setHeader("Cache-Control", "no-cache");
- response.setHeader("Content-Type", "application/pdf");
- response.addHeader("Content-disposition", "attachment;filename="" + filenamedownload + """);
- }
- OutputStream out = response.getOutputStream();
- byte[] bt = new byte[1024];
- int i = -1;
- while ((i = inputStream.read(bt)) != -1) {
- out.write(bt, 0, i);
- }
- out.flush();
- out.close();
- }
- } catch (Exception e) {
- throw new ServletException("报表服务请求失败", e);
- }
- }
- ……
- }
复制代码
PrintGetFile.java代码如下- public class PrintGetFile extends JProcessor {
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- String filename = request.getParameter("filename");
- if(filename.indexOf("/") == -1)
- filename = System.getProperty("java.io.tmpdir") + "/" + filename;
- OutputStream out = null;
- try{
- File reportFile = new File(filename);
- FileInputStream inputStream = new FileInputStream(reportFile);
- out = response.getOutputStream();
- byte[] bt = new byte[1024];
- int i = -1;
- while((i = inputStream.read(bt)) != -1){
- out.write(bt, 0, i);
- }
- out.flush();
- }finally{
- out.close();
- }
- }
- }
复制代码 |