RestEasy client framework file upload

Fredrik L picture Fredrik L · May 30, 2012 · Viewed 10.7k times · Source

Does anyone know how to create the RestEasy client side calls to upload a file using the following two interface signatures? I'm not finding any information at all. I know how to do it using the HttpClient but I'd like to use the client proxy to keep it consistent.

@POST
@Path("/upload")
@Consumes("multipart/form-data")
public void uploadFile(MultipartFormDataInput input);

@POST
@Path("/upload2")
@Consumes("multipart/form-data")
public void uploadFile2(@MultipartForm FileUploadForm form);

Any help would be appreciated, Fredrik

Answer

Frank Rünagel picture Frank Rünagel · Nov 14, 2013

With RESTEasy 3.0.X a file upload via MultipartFormData could look like this:

ResteasyClient client = new ResteasyClientBuilder().build();

ResteasyWebTarget target = client.target("http://.../upload");

MultipartFormDataOutput mdo = new MultipartFormDataOutput();
mdo.addFormData("file", new FileInputStream(new File("....thermo.wav")),
    MediaType.APPLICATION_OCTET_STREAM_TYPE);
GenericEntity<MultipartFormDataOutput> entity = new GenericEntity<MultipartFormDataOutput>(mdo) {};

Response r = target.request().post( Entity.entity(entity, MediaType.MULTIPART_FORM_DATA_TYPE));