⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 reportbean.java~1~

📁 一个基于Java的新闻发布系统
💻 JAVA~1~
字号:
package com.hope.speedway.shared;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import com.hope.speedway.shared.*;
import com.hope.speedway.shared.JRListDataSource;
import net.sf.jasperreports.engine.*;
import java.sql.*;

public class ReportBean {
  private HttpServletRequest request = null;
  private HttpServletResponse response = null;
  private String reportFile = "";

  private int rows = 0;
  private String exportType = "pdf";
  private ResultSet rs = null;
  private Map hm = null;

  public ReportBean(HttpServletRequest request, HttpServletResponse response) {
    this.request = request;
    this.response = response;
  }

  public void setMap(HashMap hm) {
    if (hm == null) {
      this.hm = new HashMap();
    }
    else {
      this.hm = hm;
    }
  }
  public void setRs(ResultSet rs){
    this.rs=rs;
  }

  public void setReportInfo(String reportFile, int rows,
                            String exportType) {
      this.reportFile = reportFile;
      this.rows = rows;
      this.setExportType(exportType);
  }

  public void setReportInfo(String reportFile, int rows) {
    this.setReportInfo(reportFile, rows, "pdf");
  }

  public String getExportType() {
    return exportType;
  }

  public void setExportType(String exportType) {
    if (exportType.equalsIgnoreCase("pdf") ||
        exportType.equalsIgnoreCase("html")) {
      this.exportType = exportType;
    }
  }

  public void export() throws
      ServletException, IOException {
    if (this.exportType.equalsIgnoreCase("html")) {
      this.exportHtml();
    }
    else {
      this.exportPdf();
    }
  }

  private void exportHtml() throws
      ServletException, IOException {
    /**暂无代码**/
  }

  private void exportPdf() throws
      ServletException, IOException {
    byte[] bytes = null;
    try {
      File report = new File(reportFile);
      bytes =
          JasperRunManager.runReportToPdf(
          report.getPath(),
          this.hm,
          new JRListDataSource(this.rs, this.rows));
    }
    catch (JRException e) {
      e.printStackTrace();
      SysException sysExcpt = new SysException("报表生成过程中发生错误");
      this.toErrMsg(request, response, sysExcpt);
      return;

    }
    catch (Exception error) {
      SysException sysExcpt = new SysException("发生错误,可能没有找到相应的jasper文件");
      this.toErrMsg(request, response, sysExcpt);
      return;

    }

    if (bytes != null && bytes.length > 0) {
      response.setContentType("application/pdf");
      response.setContentLength(bytes.length);
      ServletOutputStream ouputStream = response.getOutputStream();
      ouputStream.write(bytes, 0, bytes.length);
      ouputStream.flush();
      ouputStream.close();
    }
    else {
      SysException sysExcpt = new SysException("报表输出过程中发生错误");
      this.toErrMsg(request, response, sysExcpt);
      return;
    }
  }

  public void toErrMsg(HttpServletRequest request,
                       HttpServletResponse response,
                       SysException sysExpt) throws
      ServletException, IOException {
    request.setAttribute("sysException", sysExpt);
    doForward(request, response, "../sys_info/sysInfo.jsp");
  }

  public void doForward(HttpServletRequest request,
                        HttpServletResponse response,
                        String url) throws
      ServletException, IOException {
    request.getRequestDispatcher(url).forward(request, response);
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -