예제 #1
0
@SuppressWarnings("serial")
public class UploadServlet extends HttpServlet {
  private BllFacadeFactory bllFacadeFactory = BllFacadeFactory.getBllFactoryInstance();
  private BllInterface bllInterface = bllFacadeFactory.getInterface();
  /** Constructor of the object. */
  public UploadServlet() {
    super();
  }

  /** Destruction of the servlet. <br> */
  @Override
  public void destroy() {
    super.destroy(); // Just puts "destroy" string in log
    // Put your code here
  }

  /**
   * The doGet method of the servlet. <br>
   * This method is called when a form has its tag value method equals to get.
   *
   * @param request the request send by the client to the server
   * @param response the response send by the server to the client
   * @throws ServletException if an error occurred
   * @throws IOException if an error occurred
   */
  @SuppressWarnings({"rawtypes", "unchecked"})
  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    System.out.println("-------------------UpLoadify-doGet");
    System.out.println("-------------------QueryString::::" + request.getQueryString());
    this.doPost(request, response);
  }

  /**
   * The doPost method of the servlet. <br>
   * This method is called when a form has its tag value method equals to post.
   *
   * @param request the request send by the client to the server
   * @param response the response send by the server to the client
   * @throws ServletException if an error occurred
   * @throws IOException if an error occurred
   */
  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    String originalName = "";
    String savePath = this.getServletConfig().getServletContext().getRealPath("/");
    savePath = savePath + "/Accessory/";
    File f1 = new File(savePath);
    if (!f1.exists()) {
      f1.mkdirs();
    }
    DiskFileItemFactory fac = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(fac);
    upload.setHeaderEncoding("utf-8");
    List fileList = null;
    try {
      fileList = upload.parseRequest(request);
    } catch (FileUploadException ex) {
      return;
    }

    Iterator<FileItem> it = fileList.iterator();
    String name = "";
    String extName = "";
    while (it.hasNext()) {
      FileItem item = it.next();
      if (!item.isFormField()) {
        name = item.getName();
        long size = item.getSize();
        String type = item.getContentType();
        System.out.println(size + " " + type);
        if (name == null || name.trim().equals("")) {
          continue;
        }

        // 扩展名格式:
        if (name.lastIndexOf(".") >= 0) {
          extName = name.substring(name.lastIndexOf("."));
        }
        originalName = name;
        File file = null;
        do {
          // 生成文件名:
          name = UUID.randomUUID().toString();
          file = new File(savePath + name + extName);
        } while (file.exists());
        File saveFile = new File(savePath + name + extName);
        try {
          item.write(saveFile);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }
    JSONObject json = new JSONObject();
    json.put("address", "Accessory/" + name + extName);
    json.put("filename", originalName);
    out.print(name + extName);
    out.flush();
    out.close();
  }

  /**
   * Initialization of the servlet. <br>
   *
   * @throws ServletException if an error occurs
   */
  @Override
  public void init() throws ServletException {
    // Put your code here
  }
}
public class isPollVoteServlet extends HttpServlet {
  private BllFacadeFactory bllFacadeFactory = BllFacadeFactory.getBllFactoryInstance();
  private BllInterface bllInterface = bllFacadeFactory.getInterface();
  /** Constructor of the object. */
  public isPollVoteServlet() {
    super();
  }

  /** Destruction of the servlet. <br> */
  @Override
  public void destroy() {
    super.destroy(); // Just puts "destroy" string in log
    // Put your code here
  }

  /**
   * The doGet method of the servlet. <br>
   * This method is called when a form has its tag value method equals to get.
   *
   * @param request the request send by the client to the server
   * @param response the response send by the server to the client
   * @throws ServletException if an error occurred
   * @throws IOException if an error occurred
   */
  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    this.doPost(request, response);
  }

  /**
   * The doPost method of the servlet. <br>
   * This method is called when a form has its tag value method equals to post.
   *
   * @param request the request send by the client to the server
   * @param response the response send by the server to the client
   * @throws ServletException if an error occurred
   * @throws IOException if an error occurred
   */
  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    response.setCharacterEncoding("utf-8");
    response.setContentType("text/html;charset=utf-8");
    PrintWriter out = response.getWriter();

    String userNum = request.getParameter("UserNum");
    String courseNum = request.getParameter("CourseNum");
    String matchNum = request.getParameter("MatchNum");
    Integer matchTemp = Integer.parseInt(request.getParameter("MatchTemp"));
    PollVoteEntity pv = new PollVoteEntity();
    pv.setCourseNum(courseNum);
    pv.setMatchNum(matchNum);
    pv.setStudentNum(userNum);
    pv.setMatchTemp(matchTemp);
    JSONObject json = new JSONObject();
    if (bllInterface.IsPollVote(pv)) {
      json.put("result", true);
    } else {
      json.put("result", false);
    }
    out.print(json);
    out.flush();
    out.close();
  }

  /**
   * Initialization of the servlet. <br>
   *
   * @throws ServletException if an error occurs
   */
  @Override
  public void init() throws ServletException {
    // Put your code here
  }
}