Thanks a lot.
I try to use XOP, the following is my code and configuration.
In my junit code, I got the following error message.
------------------------------------------------------
org.apache.cxf.interceptor.Fault: .Problem with writing the request message,
class : class example.cxf.Picture.
at
org.apache.cxf.jaxrs.client.WebClient$BodyWriter.handleMessage(WebClient.java:679)
at
org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:255)
at
org.apache.cxf.jaxrs.client.WebClient.doChainedInvocation(WebClient.java:615)
at org.apache.cxf.jaxrs.client.WebClient.doInvoke(WebClient.java:595)
at org.apache.cxf.jaxrs.client.WebClient.invoke(WebClient.java:240)
at org.apache.cxf.jaxrs.client.WebClient.post(WebClient.java:249)
at arden.example.cxf.CxfClientTest.postPicture(CxfClientTest.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at
org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at
org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at
org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
at
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.apache.cxf.jaxrs.client.ClientWebApplicationException: .Problem
with writing the request message, class : class example.cxf.Picture.
at
org.apache.cxf.jaxrs.client.AbstractClient.reportMessageHandlerProblem(AbstractClient.java:506)
at
org.apache.cxf.jaxrs.client.AbstractClient.writeBody(AbstractClient.java:406)
at
org.apache.cxf.jaxrs.client.WebClient$BodyWriter.handleMessage(WebClient.java:673)
... 28 more
Caused by: javax.ws.rs.WebApplicationException
at
org.apache.cxf.jaxrs.provider.MultipartProvider.getHandlerForObject(MultipartProvider.java:320)
at
org.apache.cxf.jaxrs.provider.MultipartProvider.createDataHandler(MultipartProvider.java:291)
at
org.apache.cxf.jaxrs.provider.MultipartProvider.convertToDataHandlers(MultipartProvider.java:246)
at
org.apache.cxf.jaxrs.provider.MultipartProvider.writeTo(MultipartProvider.java:212)
at
org.apache.cxf.jaxrs.client.AbstractClient.writeBody(AbstractClient.java:401)
... 29 more
-----------------------------------------
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.ws.rs.core.Response;
public class CxfClientTest {
@Test
public void postPicture() throws FileNotFoundException {
File uploadFile = new File("/tmp/Citi.pdf");
DataSource source = new FileDataSource(uploadFile);
DataHandler dataHandler = new DataHandler(source);
Picture picture = new Picture(new Date(), "picture");
picture.setData(dataHandler);
WebClient client =
WebClient.create("http://localhost:8080/spring_cxf_rest/rest/postPicture");
client.type("multipart/mixed").accept("multipart/mixed");
Response response = client.post(picture);
System.out.println(response.getStatus());
}
-----------------------------------------
<jaxrs:server id="restContainer" address="/">
<jaxrs:serviceBeans>
<ref bean="testService"/>
</jaxrs:serviceBeans>
<jaxrs:properties>
<entry key="mtom-enabled" value="true"/>
</jaxrs:properties>
</jaxrs:server>
-------------------------------------------
@XmlType
public class Picture {
private Date time;
private String name;
@XmlMimeType("application/octet-stream")
private DataHandler data;
===============================================================
--- 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, 12:39 PM
> Hi
>
> On Thu, Feb 3, 2011 at 5:25 PM, acec acec <[email protected]>
> wrote:
>
> > Thanks a lot.
> > When I try the following Book class, everything is
> ok.
> > ----------------------
> > Book b =
> multipartBody.getAttachment(partContentId).getObject(Book.class)
> >
> > @XmlRootElement(name = "book")
> > public class Book {
> > private Date time;
> > private String name;
> > ...
> > getter/setter
> > }
> >
>
> ok
>
>
> > ----------------------
> > But after I change Book class like the following, I
> got some error message.
> > public class Book {
> > private Date time;
> > private String name;
> > private InputStream
> inputStream;
> > ...
> > Seems I can not include inputStream into Book class,
> and I have to use
> > different contentId attach InputStream separately.
> >
> >
> If you XOP then you can include InputStream inside the
> Book...But you'd need
> to use WebClient for that to work :-)
>
> 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, 11:41 AM
> > > > 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);
> > > >> > > }
> > > >> > > }
> > > >> > >
> > > >> > >
> > > >> > >
> > > >> >
> > > >>
> > > >>
> > > >>
> > > >
> > >
> >
> >
> >
>