Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send File as a parameter to a REST Service, from a client?

Tags:

java

rest

jersey

My Requirement is to send the file to the REST Service through one client. That service is going to process the file. I am using Jersey API for implementing this. But I have searched in many articles, there is no any information of how to pass the file from client side and how the REST service will retrieve the file... How to achieve this?

And I am not using the Servlets for Creating REST Service.

like image 546
Anand Murugan Avatar asked Mar 07 '13 04:03

Anand Murugan


People also ask

Can you send a file via REST API?

Use the File transfer REST API to upload and download files using HTTP or HTTPS as the transport protocol and to list the contents of a directory. Uploads a file to any back-end application that supports REST APIs over HTTP or HTTPS protocol.

How do I upload a file to REST API?

To attach a file, you must include it with the Body as form-data. Once you are in the Body → form-data fields, you must enter a KEY . This should be “file” or whichever value you specified in the @RequestPart(“[value]”) . After doing so, a dropdown will appear that gives you the option of Text or File.

Can I send file as a parameter to a REST service?

- Stack Overflow Send File as a parameter to a REST Service, from a client? My Requirement is to send the file to the REST Service through one client. That service is going to process the file.

How to post file to web API/REST client API?

When you want to post file to Web API / REST client API through c#. You can use below code in C# to post file using Multipart Form Post in C#. Below is the HTML sample code: File : you can post... When you want to post file to Web API / REST client API through c#. You can use below code in C# to post file using Multipart Form Post in C#.

How to post file to REST API using stringfullresponse?

stringfullResponse=responseReader. ReadToEnd(); webResponse. Close(); Response. Write(fullResponse); That’s it. Now you can post file to REST API.

How to post file to REST API using streamreader?

// Process response StreamReader responseReader=newStreamReader(webResponse. GetResponseStream()); stringfullResponse=responseReader. ReadToEnd(); webResponse. Close(); Response. Write(fullResponse); That’s it. Now you can post file to REST API. Share on TwitterTweet


2 Answers

Assuming you are using Jersey on both the client and server side, here is some code that 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's of multipart (file) data. It also illustrates how you can iterate through all the individual body parts in the incoming (multipart) request.

Client:

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 a file to a multipart request, and fires the request off to the server. For both client and server side code there is a reliance on the Jersey and JavaMail libraries. If you are using Maven, these can be pulled down with ease, 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 the dependency versions as required

like image 111
Perception Avatar answered Nov 02 '22 11:11

Perception


Am I right by assuming, since its a MimeMultipart type, that I could not just send one, but multiple files or additional information maybe as String or whatever, on doing only one simple post, just by adding multiple MimeBodyParts containing the different files or whatever? for example like:

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);

etc.

like image 25
matze Avatar answered Nov 02 '22 09:11

matze