Jersey Multipart Client Upload

蓝咒 提交于 2019-12-12 08:37:54

问题


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?


回答1:


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



回答2:


When you say you're not able to get a response, what do you mean exactly ?

If you send a file, can you try with this ?

   FileDataBodyPart fdp = new  FileDataBodyPart("file",f,MediaType.APPLICATION_OCTET_STREAM_TYPE);

Also,

   formDataMultiPart.field("file", "Testing.txt");

Shouldn't be named "file", but "filename" for example.

For debugging, I would advise you to listen on your server using Wireshark.



来源:https://stackoverflow.com/questions/12091893/jersey-multipart-client-upload

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!