Esempio n. 1
0
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    request.setCharacterEncoding("utf-8");
    response.setContentType("text/html;charset=utf-8");
    boolean isAccessed = SessionFilter.sessionCheck(request, response);
    if (isAccessed == false) {
      response.sendRedirect(request.getContextPath() + "/login.jsp");
    }

    /*
     * 上传三步
     */
    // 工厂
    DiskFileItemFactory factory = new DiskFileItemFactory();
    // 解析器
    ServletFileUpload sfu = new ServletFileUpload(factory);
    System.out.println("......");
    // 解析,得到List
    try {
      List<FileItem> list = sfu.parseRequest(request);
      FileItem fixuncha = null;
      //			String name = item.getFieldName();  // 获得该字段名称
      //            String value = item.getString("utf-8"); //获得该字段值

      CompanyTime companyTime = new CompanyTime();
      Integer companyId = Integer.parseInt(list.get(0).getString("utf-8"));
      companyTime.setCompanyId(companyId);
      companyTime.setPassSituation(list.get(1).getString("utf-8"));
      fixuncha = list.get(2);
      writeFile(fixuncha, companyTime);
      new CompanyTimeDao().add(companyTime);
      //			request.getRequestDispatcher("/companyinfo.jsp").forward(request, response);
      Company company = new CompanyDao().findByCompanyId(companyId + "");
      String ss = request.getContextPath() + "/companyinfo.jsp?companyId=" + company.getId();
      //			ss = new String(ss.getBytes("ISO-8859-1"), "utf-8");
      System.out.println(ss + ".......");
      response.sendRedirect(ss);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Esempio n. 2
0
  public void writeFile(FileItem fileItem, CompanyTime companyTime) throws Exception {
    if (fileItem != null) {
      /*
       * 1. 得到文件保存的路径
       */
      String root = this.getServletContext().getRealPath("/files/");
      /*
       * 2. 生成二层目录 1). 得到文件名称 2). 得到hashCode 3). 转发成16进制 4). 获取前二个字符用来生成目录
       */
      String filename = fileItem.getName(); // 获取上传的文件名称
      /*
       * 处理文件名的绝对路径问题
       */
      int index = filename.lastIndexOf(".");
      if (index != -1) {
        filename = filename.substring(index);
      }
      /*
       * 给文件名称添加uuid前缀,处理文件同名问题
       */
      String savename = UuidUtils.uuid() + "_" + filename;

      /*
       * 2. 获取hex的前两个字母,与root连接在一起,生成一个完整的路径
       */
      File dirFile = new File(root);

      /*
       * 3. 创建目录链
       */
      if (!dirFile.exists()) {
        dirFile.mkdirs();
      }

      /*
       * 4. 创建目录文件
       */
      File destFile = new File(dirFile, savename);
      /*
       * 5. 保存
       */
      fileItem.write(destFile);
      companyTime.setXunChaJiLu(savename);
    }
  }