Hi

On 06/12/11 10:14, dra wrote:
Hi folks.
I am developing a RESTful client to communicate with an existing service
using cxf 2.5.0. At first everything was quite straight foward and working,
but now I am struggling on sending a multipart request, containing one file
and one text field: On service site all data is received, but I cannot set
the content disposition for both elements, which is necessary as the service
can only detect multipart fields by their content disposition.

My code so far:
client = WebClient.create("http://my_server";);
client.header("Authorization", authorizationHeader);
client.path("/multipart_path");
client.accept(MediaType.APPLICATION_JSON);
client.type(MediaType.MULTIPART_FORM_DATA_TYPE);
ContentDisposition cd = new ContentDisposition("form-data; name=\"file\";
filename=\"image.png\"");
List<Attachment>  atts = new LinkedList<Attachment>();
atts.add(new Attachment("textfield", MediaType.TEXT_PLAIN,
"my_textfield_value"));
atts.add(new Attachment("file", new FileInputStream(uploadFile), cd));
MultipartBody body = new MultipartBody(atts);
client.post(body, String.class);

This is what the service is receiving:
MIME Multipart Media Encapsulation, Type: multipart/form-data, Boundary:
"uuid:28129548-3b28-4559-ac64-7d135c3c7705"
Type: multipart/form-data

Content-Type: text/plain
Content-Transfer-Encoding: binary
Content-ID:<textfield>
my_textfield_value

Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
Content-ID:<file>
Content-Disposition: form-data; name="file"; filename="image.png"
data_of_file

The problem is the first attachment, which is a simple text field, but is
missing the Content-Disposition. Setting the Content-Disposition of the
field manually leads to exceptions. The code changes:
ContentDisposition cd = new ContentDisposition("form-data;
name=\"textfield\"");
ContentDisposition cd2 = new ContentDisposition("form-data; name=\"file\";
filename=\"image.png\"");
List<Attachment>  atts = new LinkedList<Attachment>();
atts.add(new Attachment("textfield", "my_textfield_value", cd));

This line is wrong, it gets
public Attachment(String id, String mediaType, Object object) {}
invoked

atts.add(new Attachment("file", new FileInputStream(uploadFile), cd2));

This one is good, add another one like this:

new Attachment("textfield", new ByteArrayInputStream("my_textfield_value".getBytes()), cd2));

or if Content-Type of the individual part needs to be set too:

new Attachment("textfield", new InputStreamDataSource("my_textfield_value", "text/plain"), cd1);


let me know please if it works for you
Cheers, Sergey

The exception:
org.apache.cxf.interceptor.Fault: .Problem with writing the request message,
class : class org.apache.cxf.jaxrs.ext.multipart.MultipartBody, ContentType
: multipart/form-data.

Any ideas on how to solve this problem? The service is okay, and can be fed
with this data from a web form, and also with a small client developed using
jersey instead of cxf.

Thanks alot.
Bye, Daniel

--
View this message in context: 
http://cxf.547215.n5.nabble.com/JAX-RS-client-Content-disposition-for-multiple-items-on-multipart-requests-tp5051629p5051629.html
Sent from the cxf-user mailing list archive at Nabble.com.


--
Sergey Beryozkin

Talend Community Coders
http://coders.talend.com/

Blog: http://sberyozkin.blogspot.com

Reply via email to