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 Wed, 21 Jun 2006 18:14:03 GMT
Hi, Kevin,

Sorry about this bossy email!

Use resp.setContentType() rather than writing it in yourself.  This way
the content-type will be available to your client as a standard HTTP
header.

Also, your only call to the Servlet outputstream should be
"part.writeTo( out )".  Any other writes to that outputstream will cause
the data in it to no longer be parseable as mime/multipart.

Only the server code you provided needs some fixes (your client code is
fine).  Here are the fixes:
======================================

//DataOutputStream out = new DataOutputStream(res.getOutputStream());

Multipart part = new MimeMultipart();
MimeBodyPart body = new MimeBodyPart();

//body.addHeader("Content-Type", "multipart/mixed");
//out.writeBytes("Content-type: "+part.getContentType());
//out.writeBytes("\r\n");
//out.writeBytes("\r\n");

body.setText("Attach 1");
part.addBodyPart(body);

body = new MimeBodyPart();
body.setText("Attach 2");
part.addBodyPart(body);

body = new MimeBodyPart();
DataSource source = new FileDataSource("C:\test.zip");
body.setDataHandler(new DataHandler(source));
body.setFileName("test.zip");
part.addBodyPart(body);

resp.setContentType( part.getContentType() );
OutputStream out = resp.getOutputStream();
part.writeTo( out );
out.flush();
out.close();


-- 
Julius Davies
Senior Application Developer, Technology Services
Credit Union Central of British Columbia
http://www.cucbc.com/
Tel: 604-730-6385
Cel: 604-868-7571
Fax: 604-737-5910

1441 Creekside Drive
Vancouver, BC
Canada
V6J 4S7


On Wed, 2006-21-06 at 09:56 -0500, Kevin Cummings wrote:
> Hi Julius,
>  
> I've finally gotten around to this component again and I think I'm
> doing everything properly but I can't seem to get it this to work. I'm
> getting an exception about missing a start boundary.
>  
> Here's the code I'm using to setup and send the multipart data:
>  
>   try {
>    DataOutputStream out = new
> DataOutputStream(resp.getOutputStream());
>  
>    Multipart part = new MimeMultipart();
>    MimeBodyPart body = new MimeBodyPart();
>  
>    body.addHeader("Content-Type", "multipart/mixed");
>    out.writeBytes("Content-type: "+part.getContentType());
>    out.writeBytes("\r\n");
>    out.writeBytes("\r\n");
>  
>    body.setText("Attach 1");
>    part.addBodyPart(body);
>  
>    body = new MimeBodyPart();
>    body.setText("Attach 2");
>    part.addBodyPart(body);
>  
>    body = new MimeBodyPart();
>    DataSource source = new FileDataSource("c:\\test.zip");
>    body.setDataHandler(new DataHandler(source));
>    body.setFileName("test.zip");
>    part.addBodyPart(body);
>  
>    part.writeTo(out);
>    out.flush();
>    out.close();
>   }
>   catch(MessagingException me) {
>    me.printStackTrace();
>   }
> 
> The received data on the other end looks like:
>  
> Content-type: multipart/mixed; 
>  boundary="----=_Part_0_1620876828.1150901410798"
>  
> ------=_Part_0_1620876828.1150901410798
>  
> Attach 1
> ------=_Part_0_1620876828.1150901410798
>  
> Attach 2
> ------=_Part_0_1620876828.1150901410798
> Content-Disposition: attachment; filename=test.zip
>  
> <file-data removed for size>
> ------=_Part_0_1620876828.1150901410798--
> 
> Now, the code that I am using to try and trocess the above data that
> fails:
>  
> final InputStream iStream = post.getResponseBodyAsStream();
> final String contentType = post.getResponseHeader("Content-Type");
> 
> MimeMultipart msg = null;
> DataSource ds = new DataSource() {
>  public InputStream getInputStream() { return iStream; }
>  public OutputStream getOutputStream() { return null; }
>  public String getContentType() { return contentType; }
>  public String getName() { return null; }
> };
>  
> try {
>  msg = new MimeMultipart(ds);
> //This line is causing the Exception!
>  int partCount = msg.getCount();
>  
> System.out.println("The posted data contains " + partCount + "
> parts.");
> }
> catch(MessagingException me) {
>  System.out.println("Messaging Exception occurred during
> processing.");
>  me.printStackTrace();
> }
> 
> I just don't see what I'm missing here.. Can a DataSource object
> handle the MimeMultipart data, or do I have to write something to
> parse that?
>  
> Thanks for any help you can offer!
>  
>  
> -------------------------------------------------------------------
> 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
> -------------------------------------------------------------------


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message