Jersey 2 Multipart Download Client

I want to write a simple jersey 2 client to download a file. I am using Jersey 2.10.1 and wrote the following server code:

@POST @Consumes(MediaType.MULTIPART_FORM_DATA) @Produces(MediaType.APPLICATION_JSON) public Response uploadFile( @FormDataParam("file") InputStream aUploadedInputStream, @FormDataParam("file") FormDataContentDisposition aFileDetail) { UploadedFile uploadedFile = new UploadedFile(); uploadedFile.setOriginalFileName(aFileDetail.getFileName()); uploadedFile.setFileSize(aFileDetail.getSize()); saveToFile(aUploadedInputStream, aFileDetail.getType(), uploadedFile); databaseHelper.saveInDatabase(uploadedFile); return Response.status(200).build(); } 

("UploadedFile" is a custom class for storing file information in a database)

And this is my client code:

 private static final String TARGET_URL = "http://localhost:49158/rest/service/upload"; public Slimclient() { Client client = ClientBuilder.newBuilder() .register(MultiPartFeature.class).build(); WebTarget webTarget = client.target(TARGET_URL); MultiPart multiPart = new MultiPart(); FileDataBodyPart fileDataBodyPart = new FileDataBodyPart("file", new File("C:/Users/Nicklas2751/Desktop/test.txt"), MediaType.APPLICATION_OCTET_STREAM_TYPE); multiPart.bodyPart(fileDataBodyPart); Response response = webTarget.request( MediaType.MULTIPART_FORM_DATA).post( Entity.entity(multiPart, multiPart.getMediaType())); System.out.println(response.getStatus()+" "+response.getStatusInfo()+" "+response); } public static void main(String[] args) { new Slimclient(); } 

The server code works without problems, but when I start the client, I get the following error:

 415 Unsupported Media Type InboundJaxrsResponse{ClientResponse{method=POST, uri=http://localhost:49158/rest/service/upload, status=415, reason=Unsupported Media Type}} 

I searched the web for a good tutorial for jersey 2 and multipart fileupload, but I can only find tutorials and examples for jersey 1 or with an HTML form like β€œClient”. I hope someone can help me :)

+8
java rest file-upload multipartform-data
source share
1 answer

I found my problem. I skipped the installation of MediaType MultiPart , and using .request(MediaType.MULTIPART_FORM_DATA) I set the expected MediaType response to MULTIPART_FORM_DATA . Here is the working code:

 public class Slimclient { private static final String TARGET_URL = "http://localhost:49158/rest/service/upload"; public Slimclient() { Client client = ClientBuilder.newBuilder() .register(MultiPartFeature.class).build(); WebTarget webTarget = client.target(TARGET_URL); MultiPart multiPart = new MultiPart(); multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE); FileDataBodyPart fileDataBodyPart = new FileDataBodyPart("file", new File("C:/Users/Nicklas/Desktop/aab.txt"), MediaType.APPLICATION_OCTET_STREAM_TYPE); multiPart.bodyPart(fileDataBodyPart); Response response = webTarget.request(MediaType.APPLICATION_JSON_TYPE) .post(Entity.entity(multiPart, multiPart.getMediaType())); System.out.println(response.getStatus() + " " + response.getStatusInfo() + " " + response); } public static void main(String[] args) { new Slimclient(); } } 
+25
source share

All Articles