1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
   |  
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
		resp.setContentType("text/html");
		pw = resp.getWriter();
 
		boolean b = false;
		java.util.Enumeration en = req.getSession().getAttributeNames();
		while(en.hasMoreElements()){
			String key = en.nextElement().toString();
			if(key.compareTo("authenticated")==0){
				b = true;
			}
		}
 
		if(!b){
			req.setAttribute("error", "You are not autorized !");
			RequestDispatcher rd = req.getRequestDispatcher("index.jsp");
			rd.forward(req, resp);
			return;
		}
		else{
			Boolean bool = (Boolean)req.getSession().getAttribute("authenticated");
			if(bool.booleanValue()!=true){
				req.setAttribute("error", "You are not autorized !");
				RequestDispatcher rd = req.getRequestDispatcher("index.jsp");
				rd.forward(req, resp);
				return;
			}
		}
 
		String destination = new String();
 
		//Create a factory for disk-based file items
		DiskFileItemFactory factory = new DiskFileItemFactory();
		//factory.setSizeThreshold(maxSize);
		//Set factory constraints
		factory.setRepository(new File(path));
		//Create a new file upload handler
		ServletFileUpload upload = new ServletFileUpload(factory);
		List items;
		try{
			items = upload.parseRequest(req);
			if(items.isEmpty()){
				req.setAttribute("error", "Your form was not well completed !");
				RequestDispatcher rd = req.getRequestDispatcher("send.jsp");
				rd.forward(req, resp);
				return;
			}
			Iterator iter = items.iterator();
			while (iter.hasNext()) {
			    FileItem item = (FileItem) iter.next();
			    if (item.isFormField()) {
			    	if(item.getFieldName().trim().compareTo("repos")==0)
			    		destination = item.getString();
			    }
			    else{
			    	if(item.getName().trim().length()>0){
			    		if(item.getSize()<maxSize)
			    			processUploadedFile(item, destination);
			    		else{
			    			returnError("Your file is too fat (<5 Mo) !", req, resp, "send.jsp");
			    			return;
			    		}
			    	}
			    	else{
			    		returnError("Your form was not well completed !", req, resp, "send.jsp");
			    		return;
			    	}
			    }
			}
			returnMessage("Upload completed successfully !", req, resp, "send.jsp");
			return;
		}
		catch(FileUploadException fue){
			ExceptionDisplayer ed = new ExceptionDisplayer(fue, pw, this.getServletName());
			ed.printException();
		}
		catch(Exception e){
			ExceptionDisplayer ed = new ExceptionDisplayer(e, pw, this.getServletName());
			ed.printException();
		}
	}
 
private void processUploadedFile(FileItem item, String destination) throws Exception{
		//Process a file upload
		String final_destination = path+destination+"/"+item.getName();
		File uploadedFile = new File(final_destination);
		//getMime(uploadedFile);
		item.write(uploadedFile);
	} | 
Partager