473,322 Members | 1,522 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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

oll3i
679 512MB
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
0 3622

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

Similar topics

2
by: Citoyen du Monde | last post by:
Trying to get some ideas on a simple javascript project (to teach myself the language). I want to develop a client-side vocabulary practice application that would allow users to enter their own...
10
by: Gregory A Greenman | last post by:
I'm trying to write a program in vb.net to automate filling out a series of forms on a website. There are three forms I need to fill out in sequence. The first one is urlencoded. My program is...
8
by: Du | last post by:
I'm trying to automate the upload process to yousendit.com, but the file size doesn't add up and yousendit.com keep rejecting my upload (it accepts the upload until the very end) I don't know...
1
by: LD | last post by:
Hi, I'm pulling my hair out!! My problem is, I need to automatically upload a zip file along with 3 other pieces of text data to a web server and wait for it's xml response. Basically a...
4
by: Alex Sibilev | last post by:
Hello, I have a really weird problem I've been trying to solve it without any luck for the last couple of hours :( I'm writing a "conference board" application (quite similar to ASP.NET...
7
by: Mark Waser | last post by:
Hi all, I'm trying to post multipart/form-data to a web page but seem to have run into a wall. I'm familiar with RFC 1867 and have done this before (with AOLServer and Tcl) but just can't seem...
1
by: jjouett | last post by:
I'm trying to setup an ASPX web page such that an existing Java Client can post form data, and I'm running into a problem. The existing Java Client doesn't set any credentials, and when I try to...
4
by: Geethu03 | last post by:
Hi I have a Tomcat 5.5 version and Run many applications using this. One of My Application is upload a file to mysql database, its works fine. My new application is update a file with some...
1
by: starter08 | last post by:
Hi, I have a C++ routine(client-side) which uploads an xml file to a web server by making a socket connection and sending all the post request through that socket. On the server side I have a cgi...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.