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

java.io.IOException: Corrupt form data: premature ending

oll3i
100+
P: 679
WARNING: StandardWrapperValve[FileUploadHandler]: Servlet.service() for servlet FileUploadHandler threw exception
java.io.IOException: Corrupt form data: premature ending
on line 74

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 = "d:/WebmailHRWorkingDir";
  55.  
  56.     @Override
  57.     public void doPost(HttpServletRequest request, HttpServletResponse response)
  58.             throws ServletException, IOException {
  59.         String host = "mail.purcon.pl";
  60.         response.setContentType("text/html");
  61.         PrintWriter out = response.getWriter();
  62.  
  63.         out.println("Hello<br/>");
  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.  try{
  73.      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","m1ssy0u"); 
  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("kasia@purcon.pl","m1ssy0u");}});   
  94.  
  95.  
  96.  
  97.   ///if (MultipartFormDataRequest.isMultipartFormData(request)) 
  98.   //{
  99.   //try{
  100.     /// 
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.               Message msg = new MimeMessage(session);   
  114.  
  115.               msg.setFrom(new InternetAddress(from)); 
  116.               msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));   
  117.  
  118.               msg.setSubject(subject); 
  119.            // create the message part    
  120.               MimeBodyPart messageBodyPart =    
  121.                 new MimeBodyPart();   
  122.           //fill message   
  123.               messageBodyPart.setText(body);   
  124.               Multipart multipart = new MimeMultipart();   
  125.               multipart.addBodyPart(messageBodyPart);   
  126.            // Part two is attachment   
  127.               messageBodyPart = new MimeBodyPart();   
  128.  
  129.  
  130.                    messageBodyPart = new MimeBodyPart();   
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.         out.println("You are trying to upload<br/>");
  138.  
  139.  
  140.  
  141.                FileItemFactory factory = new DiskFileItemFactory();
  142.         ServletFileUpload upload = new ServletFileUpload(factory);
  143.  
  144.  
  145.             List<FileItem> fields = upload.parseRequest(request);
  146.             out.println("Number of fields: " + fields.size() + "<br/><br/>");
  147.             Iterator<FileItem> it = fields.iterator();
  148.             if (!it.hasNext()) {
  149.                 out.println("No fields found");
  150.                 return;
  151.             }
  152.                         out.println("<table border=\"1\">");
  153.             while (it.hasNext()) {
  154.                 out.println("<tr>");
  155.                 FileItem fileItem = it.next();
  156.                 boolean isFormField = fileItem.isFormField();
  157.                 if (isFormField) {
  158.                     out.println("<td>regular form field</td><td>FIELD NAME: " + fileItem.getFieldName() + 
  159.                             "<br/>STRING: " + fileItem.getString()
  160.                             );
  161.                     out.println("</td>");
  162.                 } else {
  163.  
  164.                DataSource source =    
  165.                 new FileDataSource(new File(UPLOAD_DIRECTORY + File.separator +  fileItem.getString()));   
  166.               System.out.println("source="+source.getName());
  167.               messageBodyPart.setDataHandler(   
  168.                 new DataHandler(source));   
  169.               messageBodyPart.setFileName(fileItem.getName());   
  170.               multipart.addBodyPart(messageBodyPart); 
  171.                     out.println("<td>file form field</td><td>FIELD NAME: " + fileItem.getFieldName() +
  172.                             "<br/>STRING: " + fileItem.getString() +
  173.                             "<br/>NAME: " + fileItem.getName() +
  174.                             "<br/>CONTENT TYPE: " + fileItem.getContentType() +
  175.                             "<br/>SIZE (BYTES): " + fileItem.getSize() +
  176.                             "<br/>TO STRING: " + fileItem.toString()
  177.                             );
  178.                     out.println("</td>");
  179.  
  180.  
  181.                 }
  182.                 out.println("</tr>");
  183.             }
  184.             out.println("</table>");
  185.         } catch (FileUploadException e) {
  186.             e.printStackTrace();
  187.  
  188.   } catch (MessagingException e) {
  189.   e.printStackTrace();
  190.   } catch (UploadException e) {
  191.   e.printStackTrace();
  192.   }
  193.  }
  194.  }    
  195.  
Oct 12 '13 #1
Share this question for a faster answer!
Share on Google+

Post your reply

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