-----------------------------
Please read the FAQ!
<http://java.apache.org/faq/>
-----------------------------


You can use the JavaMail package to parse the response.  It works fairly
well.

-ch

//
// File:   FileUploadServlet.java
// Author: Christopher Hoover <[EMAIL PROTECTED]>
// $Id$
//
import java.io.*;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;

import javax.mail.*;
import javax.mail.internet.*;

public class FileUploadServlet extends HttpServlet
{
        final static int maximumUploadLength = 10000000;

        public void init(ServletConfig config) throws ServletException {
                super.init(config);
        }

        public void doPost(HttpServletRequest request, HttpServletResponse
response)
                throws ServletException, IOException
        {
                if (request.getContentLength() > maximumUploadLength) {
                        System.err.println("Content too long: " + 
request.getContentLength());
                        response.sendError(response.SC_BAD_REQUEST, "Content too 
long.");
                        return;
                }

                if 
(request.getContentType().toLowerCase().equals("multipart/form-data"))
{
                        System.err.println("Bad content type: " + 
request.getContentType());
                        response.sendError(response.SC_BAD_REQUEST, "Bad content 
type.");
                        return;
                }

                try {
                        Properties props = System.getProperties();
                        Session session = Session.getDefaultInstance(props, null);

                        String headers
                                = "Content-Type: " + request.getContentType() + 
"\r\n\r\n";

                        InputStream messageIS = new SequenceInputStream(new
StringBufferInputStream(headers),
                                                                                       
                                 request.getInputStream());

                        MimeMessage message = new MimeMessage(session, messageIS);

                        Object content = message.getContent();

                        if (content instanceof MimeMultipart) {
                                MimeMultipart mm = (MimeMultipart) content;

                                int nBodyParts = mm.getCount();
                                for (int i = 0; i < nBodyParts; i++) {
                                        BodyPart part = mm.getBodyPart(i);

                                        System.out.println("Body part #" + i);
                                        System.out.println("content type   :" + 
part.getContentType());
                                        System.out.println("description    :" + 
part.getDescription());
                                        System.out.println("file name      :" + 
part.getFileName());
                                        System.out.println("size           :" + 
part.getSize());
                                        System.out.println("line count     :" + 
part.getLineCount());
                                }
                        } else
                                System.err.println("Content is not MimeMultipart");
                } catch (MessagingException e) {
                        System.err.println("Failed to parse content: " + e);
                        response.sendError(response.SC_BAD_REQUEST, "Failed to parse 
content.");
                }

                response.setContentType("text/plain");
                PrintWriter out = response.getWriter();
                out.println("You won big!");
        }
}


------
Christopher Hoover
mailto:[EMAIL PROTECTED]
http://www.murgatroid.com
+1-408-348-0304, +1-209-315-6378 fax


> -----Original Message-----
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED]]On Behalf Of Durga Panda
> Sent: Wednesday, September 01, 1999 5:42 AM
> To: [EMAIL PROTECTED]
> Subject: need a sample servlet that process "multipart/form-data"
>
>
> -----------------------------
> Please read the FAQ!
> <http://java.apache.org/faq/>
> -----------------------------
>
> Does anybody have a sample servlet which processes
> "multipart/form-data" or
> any pointer to any site which have this;
> actually I need it for uploadung a file from html using
> <input name="myfile" type="file"> kind of tag;
> Thanks a lot...
>
> ______________________________________________________
> Get Your Private, Free Email at http://www.hotmail.com
>
>
> --
> --------------------------------------------------------------
> Please read the FAQ! <http://java.apache.org/faq/>
> To subscribe:        [EMAIL PROTECTED]
> To unsubscribe:      [EMAIL PROTECTED]
> Archives and Other:  <http://java.apache.org/main/mail.html>
> Problems?:           [EMAIL PROTECTED]
>



--
--------------------------------------------------------------
Please read the FAQ! <http://java.apache.org/faq/>
To subscribe:        [EMAIL PROTECTED]
To unsubscribe:      [EMAIL PROTECTED]
Archives and Other:  <http://java.apache.org/main/mail.html>
Problems?:           [EMAIL PROTECTED]

Reply via email to