Multipart Mime types from a client

Discussion in 'Java' started by Matthew Zimmer, Jun 30, 2004.

  1. Hey all,
    I'm trying to figure out some client/server code and am having a bit of
    a rough go. What I have is a servlet running that I want to be able to
    upload files to. Using the apache FileUpload code seems to make the
    servlet handle web forms with file upload just fine. However, I want to
    send the files up from a client application programatically rather then
    through a web browser which is where my problem arises.

    I know that I need to set up a Multipart Mime message that has separate
    portions for the files and any data I'm sending. I had found some
    examples where people were using the MimeMultiPart objects from
    javax.mail and thought that might solve my problem. However, the
    servlet isn't able to parse my message properly using the mail code.
    I'm curious if anybody knows of some good resources that might help me
    solve this issue. I'll show the code at the end of this message in case
    somebody might see some glaring issue as to what's wrong.

    I've looked into the proper format for Multipart Mime messages and could
    probably write my own class to do it properly (at least, properly in
    terms of how I think it's supposed to work), but was wondering if
    anybody might know of some way to use the mail package (or any other
    package) to accomplish this.

    Thanks,
    Matthew Zimmer

    CLIENT CODE
    -----------
    URLConnection urlConn = null;
    try
    {
    URL url = new URL(myservlet);
    urlConn = url.openConnection();
    HttpURLConnection hurl = (HttpURLConnection)urlConn;
    hurl.setRequestMethod("POST");
    hurl.setDoInput(true);
    hurl.setDoOutput(true);
    hurl.setUseCaches(false);

    FileInputStream fis = new FileInputStream("c:/temp/file.txt");
    MimeBodyPart bodyPart = new MimeBodyPart(fis);
    MimeMultipart multiPart = new MimeMultipart();
    multiPart.addBodyPart(bodyPart);

    MimeBodyPart root = new MimeBodyPart();
    root.setContent(multiPart);

    String mpct = root.getContentType();
    int bidx = mpct.indexOf("boundary=");
    if (bidx > -1)
    {
    mpct = mpct.substring(bidx+10, mpct.length()-1);
    }

    String contentType = "multipart/form-data; boundary=" + mpct;
    hurl.setRequestProperty("Content-type", contentType);
    DataOutputStream out = new
    DataOutputStream(hurl.getOutputStream());
    root.writeTo(System.out); // see output below
    root.writeTo(out);
    out.flush();
    out.close();
    hurl.getInputStream();
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }



    CLIENT OUTPUT (this is what gets sent to the server from
    "root.writeTo(out)"....doesn't seem to be right to me)
    -------------
    ------=_Part_0_31843011.1088621202155
    This is a text file
    that should go to the server


    ------=_Part_0_31843011.1088621202155--




    SERVER CODE
    -----------
    try
    {
    FileUpload base = new FileUpload();
    base.setFileItemFactory(new DefaultFileItemFactory());
    List list = base.parseRequest(request);
    // this always returns a list of nothing as it fails on parsing
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    Matthew Zimmer, Jun 30, 2004
    #1
    1. Advertising

  2. Matthew Zimmer wrote:

    >
    > I've looked into the proper format for Multipart Mime messages and could
    > probably write my own class to do it properly (at least, properly in
    > terms of how I think it's supposed to work), but was wondering if
    > anybody might know of some way to use the mail package (or any other
    > package) to accomplish this.
    >


    I ended up just manually creating the message and that worked fine.
    Matthew
    Matthew Zimmer, Jun 30, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. c# newbie
    Replies:
    3
    Views:
    5,864
  2. Guoqi Zheng

    mime multipart message

    Guoqi Zheng, May 22, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    459
    Guoqi Zheng
    May 22, 2004
  3. Xeth  Waxman
    Replies:
    0
    Views:
    9,114
    Xeth Waxman
    Dec 9, 2004
  4. cyberco

    Reading multipart mime headers

    cyberco, Feb 14, 2006, in forum: Java
    Replies:
    1
    Views:
    530
    Timo Stamm
    Feb 14, 2006
  5. Replies:
    1
    Views:
    383
Loading...

Share This Page