예제 #1
0
  public static void renderFile(
      HttpServletResponse resp,
      String filename,
      InputStream cont_stream,
      boolean showpic,
      boolean ignorespace)
      throws IOException {
    if (cont_stream == null) return;

    if (ignorespace) filename = filename.replace(" ", "");

    if (!showpic)
      resp.addHeader(
          "Content-Disposition",
          "attachment; filename=" + new String(filename.getBytes(), "iso8859-1"));

    resp.setContentType(Mime.getContentType(filename));
    ServletOutputStream os = resp.getOutputStream();
    byte[] buf = new byte[1024];
    int len = 0;
    while ((len = cont_stream.read(buf)) != -1) {
      os.write(buf, 0, len);
    }

    os.flush();
  }
예제 #2
0
  protected void service(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, java.io.IOException {
    String p = req.getParameter("r");
    if (p == null || (p = p.trim()).equals("")) {
      String pi = req.getPathInfo();

      return;
    }

    byte[] cont = res2cont.get(p);
    if (cont != null) {
      resp.setContentType(Mime.getContentType(p));
      ServletOutputStream os = resp.getOutputStream();
      os.write(cont);
      os.flush();
      return;
    }

    InputStream is = null;
    try {
      is = appCL.getResourceAsStream(p); // getInputStreamByPath(p) ;
      if (is == null) {
        is = new Object().getClass().getResourceAsStream(p);
        if (is == null) return;
      }

      byte[] buf = new byte[1024];
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      int len;
      while ((len = is.read(buf)) >= 0) {
        baos.write(buf, 0, len);
      }

      cont = baos.toByteArray();
      res2cont.put(p, cont);
      resp.setContentType(Mime.getContentType(p));
      ServletOutputStream os = resp.getOutputStream();
      os.write(cont);
      os.flush();
      return;
    } finally {
      if (is != null) is.close();
    }
  }
예제 #3
0
  public void close() throws IOException {
    if (closed) {
      throw new IOException("This output stream has already been closed");
    }
    gzipstream.finish();

    byte[] bytes = baos.toByteArray();

    response.addHeader("Content-Length", Integer.toString(bytes.length));
    response.addHeader("Content-Encoding", "gzip");
    output.write(bytes);
    output.flush();
    output.close();
    closed = true;
  }
예제 #4
0
  /**
   * return OutputStream of JasperReport object, this page could only be viewed from localhost for
   * security concern. parameter can be (id), or (table and type)
   *
   * @param id - report id, or
   * @param table - table name
   * @param type - reporttype "s","l","o", case insensitive
   * @param client(*) - client domain
   * @param version - version number, default to -1
   */
  public void process(HttpServletRequest request, HttpServletResponse response) throws Exception {
    String clientName = request.getParameter("client");
    int objectId = ParamUtils.getIntAttributeOrParameter(request, "id", -1);
    if (objectId == -1) {
      // try using table and type
      objectId =
          getReportId(clientName, request.getParameter("table"), request.getParameter("type"));
    }
    if (objectId == -1) {
      logger.error("report not found, request is:" + Tools.toString(request));
      throw new NDSException("report not found");
    }
    int version = ParamUtils.getIntAttributeOrParameter(request, "version", -1);
    File reportXMLFile = new File(ReportTools.getReportFile(objectId, clientName));
    if (reportXMLFile.exists()) {
      // generate jasperreport if file not exists or not newer
      String reportName =
          reportXMLFile.getName().substring(0, reportXMLFile.getName().lastIndexOf("."));
      File reportJasperFile = new File(reportXMLFile.getParent(), reportName + ".jasper");
      if (!reportJasperFile.exists()
          || reportJasperFile.lastModified() < reportXMLFile.lastModified()) {
        JasperCompileManager.compileReportToFile(
            reportXMLFile.getAbsolutePath(), reportJasperFile.getAbsolutePath());
      }
      InputStream is = new FileInputStream(reportJasperFile);
      response.setContentType("application/octetstream;");
      response.setContentLength((int) reportJasperFile.length());

      // response.setHeader("Content-Disposition","inline;filename=\""+reportJasperFile.getName()+"\"");
      ServletOutputStream os = response.getOutputStream();

      byte[] b = new byte[8192];
      int bInt;
      while ((bInt = is.read(b, 0, b.length)) != -1) {
        os.write(b, 0, bInt);
      }
      is.close();
      os.flush();
      os.close();
    } else {
      throw new NDSException("Not found report template");
    }
  }
예제 #5
0
  public static void renderFile(
      HttpServletRequest req,
      HttpServletResponse resp,
      String filename,
      byte[] file_cont,
      boolean showpic,
      Date lastmodify)
      throws IOException {
    if (file_cont == null) return;

    //		if(!showpic)
    //			resp.addHeader("Content-Disposition", "attachment;
    // filename=\""+MimeUtility.encodeText(filename,"UTF-8",null)+"\"");
    filename = filename.replace(" ", "");
    if (!showpic)
      resp.addHeader(
          "Content-Disposition",
          "attachment; filename=" + new String(filename.getBytes(), "iso8859-1"));

    if (lastmodify != null) {
      resp.setDateHeader("Last-Modified", lastmodify.getTime());
    }

    if (req != null) {
      long l = req.getDateHeader("If-Modified-Since");
      if (l > 0) {
        if (lastmodify != null
            && lastmodify.getTime() / 1000 <= l / 1000) { // 由于文件系统的最后修改时间精确到秒,所以需要去除毫秒以便于计算
          resp.setStatus(304);
          return;
        }
      }
    }

    resp.setContentType(Mime.getContentType(filename));
    ServletOutputStream os = resp.getOutputStream();
    os.write(file_cont);
    os.flush();
  }
예제 #6
0
  /**
   * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
   *
   * @param request servlet request
   * @param response servlet response
   * @throws ServletException if a servlet-specific error occurs
   * @throws IOException if an I/O error occurs
   */
  protected void processRequest(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    Player player = ((Player) (request.getSession().getAttribute(CONFIG.PLAYERNAME)));

    String uri = request.getRequestURI();
    String file = uri.substring(uri.lastIndexOf("/") + 1);
    String mima = file.substring(file.lastIndexOf(".") + 1).toLowerCase();

    String type = null;
    if (mima.equals("gif")) {
      type = "gif";
    }
    if (mima.equals("jpg")) {
      type = "jpg";
    }
    if (mima.equals("png")) {
      type = "png";
    }
    if (type == null) {
      System.out.println("Unrecognized image file type." + " - " + mima);
    } else {
      byte[] image = readImageFile.getContent(player, file);

      if (image != null) {
        response.setContentType(type);
        ServletOutputStream out = response.getOutputStream();
        try {
          out.write(image);
        } finally {
          out.close();
        }
      } else {
        System.out.println("NULL image");
      }
    }
  }
예제 #7
0
  public void service(ServletRequest request, ServletResponse response) {
    int font;
    pdflib p = null;
    int i, blockcontainer, page;
    String infile = "boilerplate.pdf";
    /* This is where font/image/PDF input files live. Adjust as necessary.
     *
     * Note that this directory must also contain the LuciduxSans font
     * outline and metrics files.
     */
    String searchpath = "../data";
    String[][] data = {
      {"name", "Victor Kraxi"},
      {"business.title", "Chief Paper Officer"},
      {"business.address.line1", "17, Aviation Road"},
      {"business.address.city", "Paperfield"},
      {"business.telephone.voice", "phone +1 234 567-89"},
      {"business.telephone.fax", "fax +1 234 567-98"},
      {"business.email", "*****@*****.**"},
      {"business.homepage", "www.kraxi.com"},
    };
    byte[] buf;
    ServletOutputStream out;

    try {
      p = new pdflib();

      // Generate a PDF in memory; insert a file name to create PDF on disk
      if (p.begin_document("", "") == -1) {
        throw new Exception("Error: " + p.get_errmsg());
      }

      /*Set the search path for fonts and PDF files */
      p.set_parameter("SearchPath", searchpath);

      p.set_info("Creator", "businesscard.java");
      p.set_info("Author", "Thomas Merz");
      p.set_info("Title", "PDFlib block processing sample (Java)");

      blockcontainer = p.open_pdi(infile, "", 0);
      if (blockcontainer == -1) {
        throw new Exception("Error: " + p.get_errmsg());
      }

      page = p.open_pdi_page(blockcontainer, 1, "");
      if (page == -1) {
        throw new Exception("Error: " + p.get_errmsg());
      }

      p.begin_page_ext(20, 20, ""); // dummy page size

      // This will adjust the page size to the block container's size.
      p.fit_pdi_page(page, 0, 0, "adjustpage");

      // Fill all text blocks with dynamic data
      for (i = 0; i < (int) data.length; i++) {
        if (p.fill_textblock(page, data[i][0], data[i][1], "embedding encoding=unicode") == -1) {
          System.err.println("Warning: " + p.get_errmsg());
        }
      }

      p.end_page_ext(""); // close page
      p.close_pdi_page(page);

      p.end_document(""); // close PDF document
      p.close_pdi(blockcontainer);

      buf = p.get_buffer();

      response.setContentType("application/pdf");
      response.setContentLength(buf.length);

      out = response.getOutputStream();
      out.write(buf);
      out.close();

    } catch (PDFlibException e) {
      System.err.print("PDFlib exception occurred in businesscard sample:\n");
      System.err.print(
          "[" + e.get_errnum() + "] " + e.get_apiname() + ": " + e.get_errmsg() + "\n");
    } catch (Exception e) {
      System.err.println(e.getMessage());
    } finally {
      if (p != null) {
        p.delete(); /* delete the PDFlib object */
      }
    }
  }