Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jersey Multipart Client Upload

Tags:

java

rest

jersey

I've designed a multipart Jersey REST service as below to receive a multipart request (file uploads) and save the file in a disk location:

@POST
    @Path("/Upload")
    @Produces(MediaType.TEXT_HTML)
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public String uploadFile(@FormDataParam("file") InputStream inputStream,
            @FormDataParam("file") FormDataContentDisposition contentDisposition) {

        System.out.println("Method Entry");
        System.out.println(contentDisposition.getFileName());


        String result = "not Success";
        File file = null;
        if (contentDisposition != null
                && contentDisposition.getFileName() != null
                && contentDisposition.getFileName().trim().length() > 0) {
            try {
                file = new File("xx"
                        + contentDisposition.getFileName());
                new File("yy").mkdirs();
                file.createNewFile();
                OutputStream outputStream = new FileOutputStream(file);
                int read = 0;
                byte[] bytes = new byte[1024];

                while ((read = inputStream.read(bytes)) != -1) {
                    outputStream.write(bytes, 0, read);
                }
                outputStream.flush();
                outputStream.close();
                result = "success";

            } catch (Exception e) {

                System.out.println(e.toString());
            }
        }
        System.out.println("Method Exit");
        return result;

    }

and my test client is:

    Client client = Client.create();
    WebResource resource = client
            .resource("xyz");
    String conString = "This is the content";

    FormDataMultiPart formDataMultiPart = new FormDataMultiPart();
    formDataMultiPart.field("file", "Testing.txt");

    FormDataBodyPart bodyPart = new FormDataBodyPart("file",
            new ByteArrayInputStream(conString.getBytes()),
            MediaType.APPLICATION_OCTET_STREAM_TYPE);
    formDataMultiPart.bodyPart(bodyPart);

    String reString = resource.type(MediaType.MULTIPART_FORM_DATA)
            .accept(MediaType.TEXT_HTML)
            .post(String.class, formDataMultiPart);
    System.out.println(reString);

But I am not able to get the response.

It is working perfectly when I am using the HTML web page as the client to upload the files by calling the REST service but from the REST client it is not working.

Is there anything that has to be changed in the client?

like image 553
vvekselva Avatar asked Aug 23 '12 12:08

vvekselva


People also ask

What is a multipart file upload?

Multipart upload allows you to upload a single object as a set of parts. Each part is a contiguous portion of the object's data. You can upload these object parts independently and in any order. If transmission of any part fails, you can retransmit that part without affecting other parts.

How do I upload a file to REST API?

The rules are the same as those applied to the set metadata values REST API. Use Content-Type: application/json to describe this information as a JSON object. File to upload. This must come after the jsonInputParameters parameter.

What is @FormDataParam?

The @FormDataParam("file") annotation is used to mention file parameter in the service class. The @Consumes(MediaType. MULTIPART_FORM_DATA) is used to provide information of the file upload. To upload file using JAX-RS API, we are using jersey implementation.


1 Answers

The solution to this, if you don't have a file, but some String or so, is to do something like this:

final FormDataMultiPart formDataMultiPart = new FormDataMultiPart();
final String value = "Hello World";
final FormDataContentDisposition dispo = FormDataContentDisposition//
        .name("file")//
        .fileName("test.txt")//
        .size(value.getBytes().length)//
        .build();
final FormDataBodyPart bodyPart = new FormDataBodyPart(dispo, value);
formDataMultiPart.bodyPart(bodyPart);
like image 163
Karsten Becker Avatar answered Sep 21 '22 17:09

Karsten Becker