Multipart Mime types from a client

M

Matthew Zimmer

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();
}
 
M

Matthew Zimmer

Matthew said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top