List<FileItem> fields = upload.parseRequest(request);

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • oll3i
    Contributor
    • Mar 2007
    • 679

    List<FileItem> fields = upload.parseRequest(request);

    Code:
    List<FileItem> fields = upload.parseRequest(request);
    Why fields.size() is 0 ?
    When i upload the file.
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Judging from your other questions, I'm guessing this is the function ServletFileUplo ad#parseRequest (javax.servlet. http.HttpServle tRequest)? If so then the result should be "[a] list of FileItem instances parsed from the request". So if the list is empty I'm guessing that nothing is being uploaded. Have you checked the request? How does that come to be?

    Oh, and a request from me: Next time, please give us more information. Guessing what you're using, especially as it's not part of the standard library, is more work than we who are trying to help should be required to do.

    Comment

    • oll3i
      Contributor
      • Mar 2007
      • 679

      #3
      Code:
      /*
       * To change this template, choose Tools | Templates
       * and open the template in the editor.
       */
      
      package myPackage;
      
      import javax.servlet.ServletException;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      import java.util.*;
      import javax.mail.*;
      import java.lang.Object;
      import javax.mail.internet.*;
      import javax.activation.*;
      import java.io.IOException;
      import java.io.PrintWriter;
      import javax.mail.MessagingException;
      import javax.servlet.http.HttpSession;
      import java.util.*;
      import java.net.InetAddress;
      import java.util.Properties;
      import java.util.Date;
      import javax.mail.*;
      
      import javax.mail.internet.*;
      import java.io.File;
      
      import com.sun.mail.smtp.*;
      import javazoom.upload.*;
      import org.apache.commons.fileupload.FileItem;
      import org.apache.commons.fileupload.disk.DiskFileItemFactory;
      import org.apache.commons.fileupload.servlet.ServletFileUpload;
      import org.apache.commons.fileupload.FileUploadException;
      import java.io.File;
      import java.io.IOException;
      import java.util.List;
      import java.util.Properties;
      import javax.servlet.ServletException;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      import org.apache.commons.fileupload.FileItem;
      import org.apache.commons.fileupload.disk.DiskFileItemFactory;
      import org.apache.commons.fileupload.servlet.ServletFileUpload;
      
      import org.apache.commons.fileupload.FileItemFactory;
      import org.apache.commons.fileupload.FileUploadException;
      
      
      
      public class FileUploadHandler extends HttpServlet {
          private final String UPLOAD_DIRECTORY = "/WebmailHRWorkingDirDocs";
         
          @Override
          public void doPost(HttpServletRequest request, HttpServletResponse response)
                  throws ServletException, IOException {
              String host = "mail.pl";
              response.setContentType("text/html");
      		PrintWriter out = response.getWriter();
      
      		
      
      
      boolean isMultipartContent = ServletFileUpload.isMultipartContent(request);
       if (!isMultipartContent) {
      out.println("You are not trying to upload<br/>");
      			return;}
      
       
       
       try { MultipartFormDataRequest mrequest = new MultipartFormDataRequest(request);
       ///mrequest = new MultipartFormDataRequest(request);
       String to=mrequest.getParameter("to");
       String from = mrequest.getParameter("from");                    
          
            String subject=mrequest.getParameter("subject");
            String body=mrequest.getParameter("body");                      
              Properties properties = System.getProperties();
      
            // Setup mail server
            
      properties.put("mail.smtp.host", host);
      properties.put("mail.smtp.port", "587");
      properties.put("mail.smtp.user", from);
      properties.put("mail.smtp.auth", "true");
      properties.put("mail.smtp.password","p"); 
      properties.put("mail.smtp.starttls.enable", "true");
       Session session = Session.getInstance(properties,new javax.mail.Authenticator()   
                      {protected javax.mail.PasswordAuthentication    
                          getPasswordAuthentication()    
                      {return new javax.mail.PasswordAuthentication("u","p");}});   
                
                   
                       Message msg = new MimeMessage(session);
                   
                    msg.setFrom(new InternetAddress(from)); 
                    msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));   
        
                    msg.setSubject(subject); 
                 // create the message part    
                    MimeBodyPart messageBodyPart =    
                      new MimeBodyPart();   
                //fill message   
                    messageBodyPart.setText(body);   
                    Multipart multipart = new MimeMultipart();   
                    multipart.addBodyPart(messageBodyPart);   
                 // Part two is attachment   
                    messageBodyPart = new MimeBodyPart();   
        
                    
                          
         
        
       
      
      
      		
      		out.println("You are trying to upload<br/>");
      
      
      
                     FileItemFactory factory = new DiskFileItemFactory();
      		ServletFileUpload upload = new ServletFileUpload(factory);
       
                     
      			List<FileItem> fields = upload.parseRequest(request);
      		        out.println("Number of fields: " + fields.size() + "<br/><br/>");
      			Iterator<FileItem> it = fields.iterator();
                              out.println("Iterator<FileItem> it ");
      			if (!it.hasNext()) {
      				out.println("No fields found");
      				return;
      			}
                              
      			while (it.hasNext()) {
      						FileItem fileItem = it.next();
      				boolean isFormField = fileItem.isFormField();
      				if (isFormField) {
      					out.println("regular form field"
                                                      + " FIELD NAME: " + fileItem.getFieldName() + 
      							"<br/>STRING: " + fileItem.getString()
      							);
      					
      				} else {
                                         
      					out.println("FIELD NAME: " + fileItem.getFieldName() +
      							"<br/>STRING: " + fileItem.getString() +
      							"<br/>NAME: " + fileItem.getName() +
      							"<br/>CONTENT TYPE: " + fileItem.getContentType() +
      							"<br/>SIZE (BYTES): " + fileItem.getSize() +
      							"<br/>TO STRING: " + fileItem.toString()
      							);
              String filename = UPLOAD_DIRECTORY + "/" +  fileItem.getName();
               out.println("filename="+filename);
               DataSource source = new FileDataSource(filename);
               messageBodyPart.setDataHandler(new DataHandler(source));
               messageBodyPart.setFileName(filename);
               multipart.addBodyPart(messageBodyPart);
               msg.setContent(multipart);
      					
      				}
      				}
      
      
                    Transport.send(msg);
                    System.out.println("success....................................");   
            
          
      			
          
      
      
      } catch (FileUploadException e) {
      e.printStackTrace();
      } catch (MessagingException e) {
        e.printStackTrace();
        } catch (UploadException e) {
        e.printStackTrace();
        }
          }
      }

      i still can not send attachment
      Please help

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        OK... First of all, please post only relevant code. Some of the imports can be useful to identify which classes you're using and the definition of those objects that are being problematic. So, even when being very thorough the code could be reduced to something like this:
        [code=java]package myPackage;
        // ...
        import javax.servlet.S ervletException ;
        import javax.servlet.h ttp.HttpServlet ;
        import javax.servlet.h ttp.HttpServlet Request;
        import javax.servlet.h ttp.HttpServlet Response;
        import org.apache.comm ons.fileupload. FileItem;
        import org.apache.comm ons.fileupload. FileItemFactory ;
        import org.apache.comm ons.fileupload. FileUploadExcep tion;
        import org.apache.comm ons.fileupload. disk.DiskFileIt emFactory;
        import org.apache.comm ons.fileupload. servlet.Servlet FileUpload;

        public class FileUploadHandl erShort extends HttpServlet {

        @Override
        public void doPost(HttpServ letRequest request, HttpServletResp onse response)
        throws ServletExceptio n, IOException {
        response.setCon tentType("text/html");
        PrintWriter out = response.getWri ter();

        boolean isMultipartCont ent = ServletFileUplo ad
        .isMultipartCon tent(request);
        if (!isMultipartCo ntent) {
        out.println("Yo u are not trying to upload<br/>");
        return;
        }
        // ...
        try {
        FileItemFactory factory = new DiskFileItemFac tory();
        ServletFileUplo ad upload = new ServletFileUplo ad(factory);

        List<FileItem> fields = upload.parseReq uest(request);
        out.println("Nu mber of fields: " + fields.size() + "<br/><br/>");
        // ...
        } catch (FileUploadExce ption e) {
        e.printStackTra ce();
        }
        }
        }[/code] This makes it much easier to understand what the situation is and what to focus on.

        OK, next: I asked whether you have checked the request. The request is of the type HttpServletRequ est and you can check the contents of that request. Not having used anything of the kind myself so far I can't be sure but I'd guess that the HttpServletRequ est.html#getPar ts() function is used, so check the length of that Collection. If that is 0 too, there's something wrong with the request. If it's not there's probably something wrong later in the code.

        Comment

        • oll3i
          Contributor
          • Mar 2007
          • 679

          #5
          a question, is MultipartFormDa taRequest needed to send attachments?
          Code:
          try{         
          if (MultipartFormDataRequest.isMultipartFormData(request)) 
              {
          
              MultipartFormDataRequest mrequest = new MultipartFormDataRequest(request);
          
           String to=mrequest.getParameter("to");

          Comment

          • oll3i
            Contributor
            • Mar 2007
            • 679

            #6
            i got through it now i know that it must be List<FileItem> items = upload.parseReq uest(request);

            Thank You

            Comment

            • Nepomuk
              Recognized Expert Specialist
              • Aug 2007
              • 3111

              #7
              Glad you've solved your problem, though I don't quite understand what you did differently.

              Comment

              Working...