By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,650 Members | 1,368 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,650 IT Pros & Developers. It's quick & easy.

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

oll3i
100+
P: 679
Expand|Select|Wrap|Line Numbers
  1. List<FileItem> fields = upload.parseRequest(request);
Why fields.size() is 0 ?
When i upload the file.
Oct 12 '13 #1
Share this Question
Share on Google+
6 Replies


Nepomuk
Expert 2.5K+
P: 3,112
Judging from your other questions, I'm guessing this is the function ServletFileUpload#parseRequest(javax.servlet.http. HttpServletRequest)? 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.
Oct 12 '13 #2

oll3i
100+
P: 679
Expand|Select|Wrap|Line Numbers
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package myPackage;
  7.  
  8. import javax.servlet.ServletException;
  9. import javax.servlet.http.HttpServlet;
  10. import javax.servlet.http.HttpServletRequest;
  11. import javax.servlet.http.HttpServletResponse;
  12. import java.util.*;
  13. import javax.mail.*;
  14. import java.lang.Object;
  15. import javax.mail.internet.*;
  16. import javax.activation.*;
  17. import java.io.IOException;
  18. import java.io.PrintWriter;
  19. import javax.mail.MessagingException;
  20. import javax.servlet.http.HttpSession;
  21. import java.util.*;
  22. import java.net.InetAddress;
  23. import java.util.Properties;
  24. import java.util.Date;
  25. import javax.mail.*;
  26.  
  27. import javax.mail.internet.*;
  28. import java.io.File;
  29.  
  30. import com.sun.mail.smtp.*;
  31. import javazoom.upload.*;
  32. import org.apache.commons.fileupload.FileItem;
  33. import org.apache.commons.fileupload.disk.DiskFileItemFactory;
  34. import org.apache.commons.fileupload.servlet.ServletFileUpload;
  35. import org.apache.commons.fileupload.FileUploadException;
  36. import java.io.File;
  37. import java.io.IOException;
  38. import java.util.List;
  39. import java.util.Properties;
  40. import javax.servlet.ServletException;
  41. import javax.servlet.http.HttpServlet;
  42. import javax.servlet.http.HttpServletRequest;
  43. import javax.servlet.http.HttpServletResponse;
  44. import org.apache.commons.fileupload.FileItem;
  45. import org.apache.commons.fileupload.disk.DiskFileItemFactory;
  46. import org.apache.commons.fileupload.servlet.ServletFileUpload;
  47.  
  48. import org.apache.commons.fileupload.FileItemFactory;
  49. import org.apache.commons.fileupload.FileUploadException;
  50.  
  51.  
  52.  
  53. public class FileUploadHandler extends HttpServlet {
  54.     private final String UPLOAD_DIRECTORY = "/WebmailHRWorkingDirDocs";
  55.  
  56.     @Override
  57.     public void doPost(HttpServletRequest request, HttpServletResponse response)
  58.             throws ServletException, IOException {
  59.         String host = "mail.pl";
  60.         response.setContentType("text/html");
  61.         PrintWriter out = response.getWriter();
  62.  
  63.  
  64.  
  65.  
  66. boolean isMultipartContent = ServletFileUpload.isMultipartContent(request);
  67.  if (!isMultipartContent) {
  68. out.println("You are not trying to upload<br/>");
  69.             return;}
  70.  
  71.  
  72.  
  73.  try { MultipartFormDataRequest mrequest = new MultipartFormDataRequest(request);
  74.  ///mrequest = new MultipartFormDataRequest(request);
  75.  String to=mrequest.getParameter("to");
  76.  String from = mrequest.getParameter("from");                    
  77.  
  78.       String subject=mrequest.getParameter("subject");
  79.       String body=mrequest.getParameter("body");                      
  80.         Properties properties = System.getProperties();
  81.  
  82.       // Setup mail server
  83.  
  84. properties.put("mail.smtp.host", host);
  85. properties.put("mail.smtp.port", "587");
  86. properties.put("mail.smtp.user", from);
  87. properties.put("mail.smtp.auth", "true");
  88. properties.put("mail.smtp.password","p"); 
  89. properties.put("mail.smtp.starttls.enable", "true");
  90.  Session session = Session.getInstance(properties,new javax.mail.Authenticator()   
  91.                 {protected javax.mail.PasswordAuthentication    
  92.                     getPasswordAuthentication()    
  93.                 {return new javax.mail.PasswordAuthentication("u","p");}});   
  94.  
  95.  
  96.                  Message msg = new MimeMessage(session);
  97.  
  98.               msg.setFrom(new InternetAddress(from)); 
  99.               msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));   
  100.  
  101.               msg.setSubject(subject); 
  102.            // create the message part    
  103.               MimeBodyPart messageBodyPart =    
  104.                 new MimeBodyPart();   
  105.           //fill message   
  106.               messageBodyPart.setText(body);   
  107.               Multipart multipart = new MimeMultipart();   
  108.               multipart.addBodyPart(messageBodyPart);   
  109.            // Part two is attachment   
  110.               messageBodyPart = new MimeBodyPart();   
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.         out.println("You are trying to upload<br/>");
  121.  
  122.  
  123.  
  124.                FileItemFactory factory = new DiskFileItemFactory();
  125.         ServletFileUpload upload = new ServletFileUpload(factory);
  126.  
  127.  
  128.             List<FileItem> fields = upload.parseRequest(request);
  129.                 out.println("Number of fields: " + fields.size() + "<br/><br/>");
  130.             Iterator<FileItem> it = fields.iterator();
  131.                         out.println("Iterator<FileItem> it ");
  132.             if (!it.hasNext()) {
  133.                 out.println("No fields found");
  134.                 return;
  135.             }
  136.  
  137.             while (it.hasNext()) {
  138.                         FileItem fileItem = it.next();
  139.                 boolean isFormField = fileItem.isFormField();
  140.                 if (isFormField) {
  141.                     out.println("regular form field"
  142.                                                 + " FIELD NAME: " + fileItem.getFieldName() + 
  143.                             "<br/>STRING: " + fileItem.getString()
  144.                             );
  145.  
  146.                 } else {
  147.  
  148.                     out.println("FIELD NAME: " + fileItem.getFieldName() +
  149.                             "<br/>STRING: " + fileItem.getString() +
  150.                             "<br/>NAME: " + fileItem.getName() +
  151.                             "<br/>CONTENT TYPE: " + fileItem.getContentType() +
  152.                             "<br/>SIZE (BYTES): " + fileItem.getSize() +
  153.                             "<br/>TO STRING: " + fileItem.toString()
  154.                             );
  155.         String filename = UPLOAD_DIRECTORY + "/" +  fileItem.getName();
  156.          out.println("filename="+filename);
  157.          DataSource source = new FileDataSource(filename);
  158.          messageBodyPart.setDataHandler(new DataHandler(source));
  159.          messageBodyPart.setFileName(filename);
  160.          multipart.addBodyPart(messageBodyPart);
  161.          msg.setContent(multipart);
  162.  
  163.                 }
  164.                 }
  165.  
  166.  
  167.               Transport.send(msg);
  168.               System.out.println("success....................................");   
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175. } catch (FileUploadException e) {
  176. e.printStackTrace();
  177. } catch (MessagingException e) {
  178.   e.printStackTrace();
  179.   } catch (UploadException e) {
  180.   e.printStackTrace();
  181.   }
  182.     }
  183. }
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  

i still can not send attachment
Please help
Oct 12 '13 #3

Nepomuk
Expert 2.5K+
P: 3,112
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:
Expand|Select|Wrap|Line Numbers
  1. package myPackage;
  2. // ...
  3. import javax.servlet.ServletException;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import org.apache.commons.fileupload.FileItem;
  8. import org.apache.commons.fileupload.FileItemFactory;
  9. import org.apache.commons.fileupload.FileUploadException;
  10. import org.apache.commons.fileupload.disk.DiskFileItemFactory;
  11. import org.apache.commons.fileupload.servlet.ServletFileUpload;
  12.  
  13. public class FileUploadHandlerShort extends HttpServlet {
  14.  
  15.     @Override
  16.     public void doPost(HttpServletRequest request, HttpServletResponse response)
  17.             throws ServletException, IOException {
  18.         response.setContentType("text/html");
  19.         PrintWriter out = response.getWriter();
  20.  
  21.         boolean isMultipartContent = ServletFileUpload
  22.                 .isMultipartContent(request);
  23.         if (!isMultipartContent) {
  24.             out.println("You are not trying to upload<br/>");
  25.             return;
  26.         }
  27.         // ...
  28.         try {
  29.             FileItemFactory factory = new DiskFileItemFactory();
  30.             ServletFileUpload upload = new ServletFileUpload(factory);
  31.  
  32.             List<FileItem> fields = upload.parseRequest(request);
  33.             out.println("Number of fields: " + fields.size() + "<br/><br/>");
  34.             // ...
  35.         } catch (FileUploadException e) {
  36.             e.printStackTrace();
  37.         }
  38.     }
  39. }
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 HttpServletRequest 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 HttpServletRequest.html#getParts() 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.
Oct 12 '13 #4

oll3i
100+
P: 679
a question, is MultipartFormDataRequest needed to send attachments?
Expand|Select|Wrap|Line Numbers
  1. try{         
  2. if (MultipartFormDataRequest.isMultipartFormData(request)) 
  3.     {
  4.  
  5.     MultipartFormDataRequest mrequest = new MultipartFormDataRequest(request);
  6.  
  7.  String to=mrequest.getParameter("to");
Oct 12 '13 #5

oll3i
100+
P: 679
i got through it now i know that it must be List<FileItem> items = upload.parseRequest(request);

Thank You
Oct 13 '13 #6

Nepomuk
Expert 2.5K+
P: 3,112
Glad you've solved your problem, though I don't quite understand what you did differently.
Oct 14 '13 #7

Post your reply

Sign in to post your reply or Sign up for a free account.