commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Julius Davies" <juliusdav...@cucbc.com>
Subject RE: Need help processing multipart data in client application.
Date Sat, 06 May 2006 02:18:48 GMT
Hi, Kevin,

If you have the J2EE "mail.jar" and "activation.jar" in your classpath, you can do something
like this:

import javax.activation.DataSource;
import javax.mail.BodyPart;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMultipart;

final InputStream stream = somehowGetStream();
final String contentType = somehowGetMimeContentType();
DataSource ds = new DataSource()
{
  public InputStream getInputStream() { return stream; }
  public OutputStream getOutputStream() { return null; }
  public String getContentType() { return contentType; }
  public String getName() { return null; }
};

MimeMultipart multi = null;
try
{
  multi = new MimeMultipart( ds );
  int count = multi.getCount();
  for ( int i = 0; i < count; i++ )
  {
    BodyPart part = multi.getBodyPart( i );
    String type = part.getContentType();
    String header = part.getHeader( "interesting-header" )[ 0 ];
    InputStream partStream = part.getInputStream();

    // process part's content...
    // processPart( partStream );  // maybe something like this?
  }
}
catch ( MessagingException me )
{
  // do something interesting
}
catch ( IOException ioe )
{
  // do something interesting
}


The trick is implementing javax.activation.DataSource - it's not obvious from the Javadocs
that you can just do it quick and dirty as an anonymous inner class.  I originally spent days
trying to find a concrete implementation in the J2EE javadocs!

Good luck.  Hope this helps.


yours,

Julius Davies
http://juliusdavies.ca/




-----Original Message-----
From:	mfncooper@gmail.com on behalf of Martin Cooper
Sent:	Tue 5/2/2006 12:13 PM
To:	Jakarta Commons Users List
Cc:	
Subject:	Re: Need help processing multipart data in client application.

You might be able to do this will Commons FileUpload by implementing your
own flavour of the RequestContext interface that isolates the environment.
If you take a look at how the servlet and portlet implementations are done
in FileUpload, you'll find that they provide a very thin layer over an
environment-independent core. All they do is provide a custom RequestContext
and a convenience wrapper for it.

(I haven't tried anything like this, so I can't guarantee that it'll work,
but it seems like it should.)

--
Martin Cooper


On 5/2/06, Kevin Cummings <Kevin.Cummings@cs.oag.state.tx.us> wrote:
>
> I'm hoping someone can help me with a problem I'm having. I have a
> servlet that I've written that processes POST requests and responds with
> a multipart response. My response data looks like:
>
> Content-type: multipart/form-data;
> boundary=--------------------10af66014d5
>
> ----------------------10af66014d5
> Content-Disposition: form-data; name="FileID"
>
> 127709384793
> ----------------------10af66014d5
> Content-Disposition: form-data; name="FileSize"
>
> 10731
> ----------------------10af66014d5
> Content-Disposition: form-data; name="test.zip";
> filename="C:\test.zip"
> Content-Type: application/zip
>
> <zip-file contents>
> ----------------------10af66014d5--
>
> Now, my problem is how can I process this response in my client that
> makes the request to the servlet. All of the examples I've seen only
> work with a ServletRequest object which I don't have because my servlet
> is only responsible for responding to the request and sending the data
> back. Are there any existing libraries that will let me process a
> multipart response as opposed to a multipart request?
>
>
> Thanks!
>
>
> -------------------------------------------------------------------
> Kevin Cummings
> IT/Systems Development
> Child Support Division
> Texas Office of the Attorney General
> Email: kevin.cummings@cs.oag.state.tx.us
> Phone: (512) 460-6421
> Fax: (512) 460-6800
> -------------------------------------------------------------------
>
>






Mime
View raw message