示例#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 static void renderXormFile(
      HttpServletRequest req,
      HttpServletResponse resp,
      Class xormc,
      String xormprop,
      long pkid,
      String filename,
      boolean showpic,
      Date lastmodify)
      throws ClassNotFoundException, Exception {
    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); // not modify
          return;
        }
      }
    }

    GDB g = GDB.getInstance();

    long filelen = g.getXORMFileContLength(xormc, xormprop, pkid);
    if (filelen < 0) return;

    resp.setContentLength((int) filelen);
    resp.setContentType(Mime.getContentType(filename));
    ServletOutputStream os = resp.getOutputStream();
    GDB.getInstance().loadXORMFileContToOutputStream(xormc, xormprop, pkid, os);
    os.flush();
  }
示例#4
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();
  }