Send file as REST service parameter from client?

My requirement is to send the file to the REST service through one client. This service will process the file. I am using the Jersey API to implement this. But I searched in many articles, there is no information on how to transfer the file from the client side and how the REST service will retrieve the file . to achieve this?

And I do not use servlets to create a REST service.

+4
source share
2 answers

Assuming you are using Jersey on the client and server side, here is some code you can extend:

Server side:

@POST @Path("/") @Produces(MediaType.TEXT_PLAIN) @Consumes(MediaType.MULTIPART_FORM_DATA) public Response uploadFile(final MimeMultipart file) { if (file == null) return Response.status(Status.BAD_REQUEST) .entity("Must supply a valid file").build(); try { for (int i = 0; i < file.getCount(); i++) { System.out.println("Body Part: " + file.getBodyPart(i)); } return Response.ok("Done").build(); } catch (final Exception e) { return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e) .build(); } } 

The above code implements a resource method that accepts POST from multipart (file) data. It also illustrates how you can iterate over all parts of the body in an incoming (multi-part) query.

Customer:

 final ClientConfig config = new DefaultClientConfig(); final Client client = Client.create(config); final WebResource resource = client.resource(ENDPOINT_URL); final MimeMultipart request = new MimeMultipart(); request.addBodyPart(new MimeBodyPart(new FileInputStream(new File( fileName)))); final String response = resource .entity(request, "multipart/form-data") .accept("text/plain") .post(String.class); 

The above code simply attaches the file to a multi-page request and sends the request to the server. For client and server code, there is a dependency on the Jersey and JavaMail libraries. If you use Maven, they can be easily torn down, with the following dependencies:

 <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-core</artifactId> <version>1.17</version> </dependency> <dependency> <!-- only on server side --> <groupId>com.sun.jersey</groupId> <artifactId>jersey-server</artifactId> <version>1.14</version> </dependency> <dependency> <!-- only on client side --> <groupId>com.sun.jersey</groupId> <artifactId>jersey-client</artifactId> <version>1.17</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-json</artifactId> <version>1.17</version> </dependency> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.6</version> </dependency> 

Adjust dependency versions as needed

+9
source

I am right in assuming with its MimeMultipart type that I couldn’t just send it, but a few files or additional information, like String or something else, making only one simple entry, just adding a few MimeBodyParts containing different files or something else? for example, for example:

 final MimeMultipart request = new MimeMultipart(); request.addBodyPart(new MimeBodyPart(new FileInputStream(new File( fileOne))), 0); request.addBodyPart(new MimeBodyPart(new FileInputStream(new File( fileTwo))), 1); 

and etc.

0
source

All Articles