2

I would like to implement a webservice with cxf to upload a file with some informations about it contained in the body.

What I've done but didn't work yet :

 @POST
 @Path("/")
 @Consumes(MediaType.MULTIPART_FORM_DATA)
 User addDocument(
     @Multipart(value="metadata", type="application/json") DocMeta metadata,
     @Multipart(value="inputstream", type="multipart/form-data") InputStream inputStream)
     throws ObjectAlreadyExistsException;

When I try to request my service with curl it doesn't work :

curl  http://localhost:9090/... 
      --X POST  
      -H"Content-Type:multipart/form-data" 
      -F inputstream=@myFile.txt 
      -d'{"info1":"info1","info2":"info2"}'

Is it really possible to have both multipart data and a json body with cxf ??

Thanks by advance

Manu

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Manu
  • 91
  • 2
  • 7

1 Answers1

4

Yes it's possible. But the problem is with your cURL request. You should add all parts as --form/-F. You are trying to send the JSON as a normal body. Attempting that I would get an error with cURL, it wouldn't even send out the request. Also you need to set the Content-Type for each part. For example

C:\>curl -v -H "Content-Type:multipart/form-data" 
            -H "Accept:application/json" 
            -F "stream=@android.png;type=application/octet-stream" 
            -F "person={\"name\":\"peeskillet\"};type=application/json"
            -X POST http://localhost:8080/rest/multipart`

(All on one line of course). Here's the resource method I used to test.

public static class Person {
    public String name;
}

@POST
@Produces(MediaType.APPLICATION_JSON)
public Response postMultiPart(
    @Multipart(value="stream", type="application/octet-stream") InputStream img,
    @Multipart(value="person", type="application/json") Person person) throws Exception {

    Image image = ImageIO.read(img);
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300, 300);
    frame.add(new JLabel(new ImageIcon(image)));
    frame.setVisible(true);

    return Response.ok(person).build();
}

It was an image that I sent as a file.

Alternatively, you can get an Attachment, which will give you more info on the file.

public Response postMultiPart(
    @Multipart(value="stream") Attachment img,
    @Multipart(value="person", type="application/json") Person person) throws Exception {

    Image image = ImageIO.read(img.getObject(InputStream.class));
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Thanks man, so you think it's really not possible to have both file inputstream and a classical json body ? I don't find it in the doc... – Manu Feb 13 '15 at 10:41
  • 2
    From what I understand you are saying is that you want to have to different entity bodies, one being multipart and other other being application/json. If that's the case, this is not a cxf issue. A request should only have one entity body. That's what multipart was made for. Send the file as a body part and the JSON as a body part. You method is already set up this way, you just didn't send the request correctly – Paul Samsotha Feb 13 '15 at 10:50