Ejemplo n.º 1
0
 public void init(ServletConfig config) throws ServletException {
   super.init(config);
   try {
     // 应用的相对路径前缀
     String prefix = config.getServletContext().getRealPath("/");
     if (prefix == null) prefix = "";
     // 获取配置文件的文件名称
     uploadPath = config.getInitParameter(this.UPLOAD_FILE_PATH_PARAM);
     // 文件不存在提醒信息
     alarmMessage =
         config.getInitParameter(this.FILE_UN_EXIST_ALARM) == null
             ? alarmMessage
             : config.getInitParameter(this.FILE_UN_EXIST_ALARM);
     // 是否是相对路径
     String isRelatePath =
         config.getInitParameter(this.UPLOAD_FILE_PATH_RELATE_FLAG_PARAM) == null
             ? "true"
             : config.getInitParameter(this.UPLOAD_FILE_PATH_RELATE_FLAG_PARAM);
     if (isRelatePath.equalsIgnoreCase("true"))
       uploadPath = prefix + (uploadPath.indexOf("/") == 0 ? "" : "/") + uploadPath;
     if ((uploadPath.lastIndexOf("/") + 1) != uploadPath.length()) uploadPath += "/";
     logger.info("file upload path=" + uploadPath);
   } catch (Exception e) {
     logger.error("fileDownload Servlet init Error:" + e.getMessage());
     e.printStackTrace();
   }
 }
Ejemplo n.º 2
0
  /*
   * (non-Javadoc)
   *
   * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
   *      javax.servlet.http.HttpServletResponse)
   */
  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    req.setCharacterEncoding("UTF-8");
    String downFileName = req.getParameter(this.DOWN_DISPLAY_FILENAME);
    String realFileName = req.getParameter(this.DOWN_REAL_FILENAME);
    String isNewWindow = null;
    try {
      isNewWindow = req.getParameter(this.DOWN_IS_NEW_WINDOW);
    } catch (Exception e) {
      logger.info("file downLoadServlet:本页面下载文件!");
      e.printStackTrace();
    }
    logger.info("下载文件为:" + uploadPath + realFileName);
    File downFile = new File(uploadPath + realFileName);

    resp.setContentType("text/html;charset=UTF-8");
    // resp.setCharacterEncoding("UTF-8");
    if (!downFile.exists()) {
      System.out.println("下载文件为=" + downFileName);
      logger.info("下载文件为:" + new String(downFileName.getBytes(), "UTF-8") + "不存在!");
      String alarm =
          StringUtil.replaceStr(alarmMessage, "{0}", new String(downFileName.getBytes(), "UTF-8"));
      resp.getWriter().print("<script type=\"text/javascript\">");
      resp.getWriter().print("alert('" + alarm + "');");
      if (isNewWindow != null) {
        resp.getWriter().print("window.close();");
      }
      resp.getWriter().print("</script>");
      return;
    }
    try {
      resp.setContentType("application/x-download");
      resp.setHeader(
          "Content-Disposition", "attachment;filename=" + URLEncoder.encode(downFileName, "UTF-8"));
      ServletOutputStream out = resp.getOutputStream();
      InputStream inStream = new FileInputStream(downFile);
      // 循环取出流中的数据
      byte[] b = new byte[1024];
      int len;
      while ((len = inStream.read(b)) > 0) out.write(b, 0, len);
      out.flush();
      out.close();
      inStream.close();
    } catch (Exception e) {
      e.printStackTrace();
      logger.error(e.getMessage());
    }
  }
Ejemplo n.º 3
0
 /**
  * 解压资源到web应用指定的路径下 目前不采取比较版本文件方式,采用非覆盖解压模式,对于已经存在的文件不做修改
  *
  * @param pResources
  * @param pDestPath
  * @throws Exception
  */
 private static void loadResource(ResourceFile resModel, String webHome)
     throws LoadResourcesException {
   String exportDir =
       webHome
           + (resModel.getUnzipPath().indexOf(File.separator) != 0 ? File.separator : "")
           + resModel.getUnzipPath();
   try {
     InputStream resources = ResourcesLoader.class.getResourceAsStream(resModel.getResource());
     ZipInputStream zis = new ZipInputStream(resources);
     FileUtil.unzip(zis, exportDir, false);
   } catch (Exception e) {
     final String MSG =
         "解压缩资源文件:" + resModel.getResource() + " 到 " + resModel.getUnzipPath() + "失败!";
     logger.error(MSG);
     throw new LoadResourcesException(MSG, e);
   }
 }