Jersey Multipart客户端上传 [英] Jersey Multipart Client Upload

查看:323
本文介绍了Jersey Multipart客户端上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计了一个多部分Jersey REST服务,如下所示,接收多部分请求(文件上传)并将文件保存在磁盘位置:

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;

    }

我的测试客户是:

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

但我无法得到答复。

当我使用HTML网页作为客户端通过调用REST服务上传文件但是从REST客户端无法正常工作时,它工作正常。

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?

推荐答案

对此的解决方案,如果你没有文件,但有些字符串左右,就是做类似的事情这个:

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

这篇关于Jersey Multipart客户端上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆