Ejemplo n.º 1
0
  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    HttpSession session = request.getSession();
    String username = "";
    for (Part part : request.getParts()) {
      if (part.getName().equals("message")) {
        part.delete();
        break;
      }
      String type = part.getContentType();

      if (!type.startsWith("image/")) {
        request.setAttribute("invalidType", true);
        RequestDispatcher rd = request.getRequestDispatcher("upload.jsp");
        rd.forward(request, response);
      }
      String filename = part.getSubmittedFileName();

      InputStream is = request.getPart(part.getName()).getInputStream();
      int i = is.available();

      LoggedIn lg = (LoggedIn) session.getAttribute("LoggedIn");

      if (lg.getlogedin()) {
        username = lg.getUsername();
        if (i > 0) {
          byte[] b = new byte[i + 1];
          is.read(b);
          PicModel tm = new PicModel();
          tm.setCluster(cluster);
          String description = request.getParameter("message");
          if (session.getAttribute("Location").equals("profile")) {
            tm.insertPic(b, type, filename, username, description, true);
          } else {
            tm.insertPic(b, type, filename, username, description, false);
          }
          is.close();
        }
      }
    }
    if (session.getAttribute("Location").equals("profile")) {
      response.sendRedirect("UserProfile");
    } else {
      response.sendRedirect("/InstagrimXinyue/Images/" + username);
    }
  }