> 2) According to cxf guide "When handling complex multipart/form-data
>> submissions (such as those containing files) MultipartBody (and Attachment)
>> need to be used directly."
>>
>> MultipartBody or List<Attachment>, provided it is a real
> multipart/form-data submission (with recursive parts)
>
>
>> When I handle the above form, I have to use MultipartBody, is there easy
>> way to parse text input and file input from MultipartBody?
>>
>>
> Assuming the JAX-RS provider is available which can handle the content of
> the file then yes. Example, if a given file represents a Book XML instance
> then you can do
>
> Book b = multipartBody.getAttachment(partContentId).getObject(Book.class)
>
> or
>
> Book b = multipartBody.getAttachmentObject(partContentId, Book.class)
>
>
or if all the attachment parts have the same content type then simply

for (Attachment att : listofAttachments) {
    Book b = att.getObject(Book.class);
}


> Cheers, Sergey
>
> Regards.
>>
>>
>> --- On Thu, 2/3/11, Sergey Beryozkin <[email protected]> wrote:
>>
>> > From: Sergey Beryozkin <[email protected]>
>> > Subject: Re: some questions about cxf rest uploading file
>> > To: [email protected]
>> > Received: Thursday, February 3, 2011, 9:54 AM
>> > Hi
>> >
>> > On Thu, Feb 3, 2011 at 2:29 PM, acec acec <[email protected]>
>> > wrote:
>> >
>> > > Hi, all
>> > > I am using the following code to upload a file. It
>> > works fine.
>> > >
>> > > But I have several questions:
>> > > 1) If I change body.getAllAttachments() ==>
>> > body.getAttachment(contentId),
>> > > What does mean "contentId"? In my junit code, How can
>> > I set that contentId?
>> > >
>> > > Content-Id the id of the individual multi-part; sure,
>> > if you know the
>> > contentId then you can use
>> >
>> > body.getAttachment(contentId)
>> >
>> >
>> > > 2)Can I get original file name on server side? I
>> > tried
>> > > dataHandler.getName(), but it return null.
>> > >
>> > >
>> > If it's a multipart/form-data then you can get a
>> > ContentDisposition header
>> > from the Attachment and get the name; otherwise you can
>> > just introduce a
>> > custom header and use it during the creation of the
>> > individual part and then
>> > retrieve it from Attachment.
>> >
>> >
>> > > 3)In my junit code, when I add Part using
>> > (reqEntity.addPart("file1",
>> > > fileBody)), how can I retrieve the name "file1" from
>> > my server side?
>> > >
>> > > You're using the the Apache Http Client, so I'm not
>> > sure what exactly it
>> > produces. If it's a multipart/form-data request then on the
>> > cxf side do
>> > attachment.getContentDisposition() and get the file name
>> >
>> >
>> > > 4)I never use MTOM, when I am using the following code
>> > to upload one PDF
>> > > file, does is use base64?
>> > >
>> > >
>> > Not sure what Apache HttpClient does there
>> >
>> > Cheers, Sergey
>> >
>> >
>> > > Thanks a lot.
>> > >
>> > > ===============Server side
>> > coe===============================
>> > > @POST
>> > > @Path("/upload/book")
>> > > public void postBook(MultipartBody body) {
>> > >  try {
>> > >  System.out.println("start post");
>> > >  List<Attachment> attachments =
>> > body.getAllAttachments();
>> > >  DataHandler dataHandler =
>> > attachments.get(0).getDataHandler();
>> > >  InputStream inputStream =
>> > dataHandler.getInputStream();
>> > >  OutputStream outputStream = new
>> > FileOutputStream(new
>> > > File("/tmp/new.pdf"));
>> > >  IOUtils.copy(inputStream, outputStream);
>> > >  inputStream.close();
>> > >  outputStream.close();
>> > >  System.out.println("end post");
>> > > } catch (Exception e) {
>> > >  System.out.println("Exception");
>> > > }
>> > > }
>> > >
>> > > ====================My
>> > Junit==========================
>> > > public class UploadClassTest {
>> > >  @Test
>> > >  public void uploadClass() throws
>> > ClientProtocolException, IOException {
>> > >   HttpClient httpClient = new
>> > DefaultHttpClient();
>> > >   HttpPost httpPost = new HttpPost("
>> > > http://localhost:8080/spring_cxf_rest/rest/upload/book";);
>> > >
>> > >   FileBody fileBody = new FileBody(new
>> > File("/tmp/Citi.pdf"));
>> > >
>> > >   MultipartEntity reqEntity = new
>> > MultipartEntity();
>> > >   reqEntity.addPart("file1",
>> > fileBody);
>> > >
>> > >   httpPost.setEntity(reqEntity);
>> > >
>> > >   httpClient.execute(httpPost);
>> > >  }
>> > > }
>> > >
>> > >
>> > >
>> >
>>
>>
>>
>

Reply via email to