RESTeasy client code for attaching a file

I need to attach a file to my service endpoint. I tested the functionality through POSTMAN (chrome browser plugin for testing the leisure service), it works fine.

But I need to check the same with JUNIT. In this case, I use the RESTeasy client.

I tried using this code:

StringBuilder sb = new StringBuilder(); BufferedReader br = new BufferedReader(new FileReader("C:/Temp/tempfile.txt")); try { String line = br.readLine(); while (line != null) { sb.append(line); sb.append(System.lineSeparator()); line = br.readLine(); } } finally { br.close(); } byte[] file = sb.toString().getBytes(); Client client = ClientBuilder.newClient(); Invocation.Builder builder = client.target(webTarget.getUri() + "/attachment" ).request(MediaType.MULTIPART_FORM_DATA_TYPE); Response response = builder.post(Entity.entity(file, MediaType.MULTIPART_FORM_DATA), Response.class); 

But I get an error message:

org.apache.commons.fileupload.FileUploadException: the request was rejected because a multiline border was not found

Is there any solution for this?

Or can someone give a sample RESTeasy client client code for attaching a file?

+3
java rest junit jax-rs resteasy
Dec 23 '14 at 13:22
source share
1 answer

Multipart has a special format. If the server expects multipart / form-data format, we cannot just send it as a regular request. You can see the preview window in Postman to see the format

enter image description hereenter image description here




You can see that each part has a border. We do not need to worry about installing this manually. Resteasy has an API for building multiform output. You can use the MultipartFormDataOutput class to build the output. Just use the addFormData method to add details. In your case, this is only one part, but the request will still be formatted as the server expects.

So your query should look something like

 MultipartFormDataOutput output = new MultipartFormDataOutput(); // file (below) doesn't have to be a `byte[]` // It can be a `File` object and work just the same output.addFormData("file", file, MediaType.APPLICATION_OCTET_STREAM_TYPE); Response response = target.request() .post(Entity.entity(output, MediaType.MULTIPART_FORM_DATA)); 

It is assumed that you have the required dependency as I get the image if the server accepts multipart

 <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-multipart-provider</artifactId> <version>${resteasy.version}</version> </dependency> 






And just for completeness ...

For future readers interested in the server side (since you did not provide your code), this is what I used for testing

 @Path("/multipart") public class MultipartResource { @POST @Consumes(MediaType.MULTIPART_FORM_DATA) public Response postData(MultipartFormDataInput input) throws Exception { byte[] bytes = input.getFormDataPart("file", byte[].class, null); JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(bytes))); return Response.ok("GOT IT").build(); } } 
+4
Dec 23 '14 at 15:17
source share



All Articles