java - Jersey 分段客户端上传

标签 java rest jersey

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

@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 客户端它不工作。

客户端有什么需要修改的吗?

最佳答案

解决这个问题的方法是,如果你没有文件,但有一些 String 左右的东西,那就是做这样的事情:

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

关于java - Jersey 分段客户端上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12091893/

相关文章:

java - 匹配相同的希伯来语单词总是返回 False

java - 在 JFrame 上排列 JPanel

mysql - 当数据库中的数据更新时,如何更新前端检索到的数据?

rest - Keycloak UMA授权REST API需要发送数千个请求

javascript - REST API - Node、Express、MySQL - 如何通过 URL 传递有限数量的对象来过滤 JSON(来自数据库)返回

java - Jersey + Guice 不能将非 Jersey 资源与 Jersey 资源混合

Jersey - JAX/RS - 如何使用不同的处理程序处理不同的内容类型

java - 在 Java 中发出带有 header 的 http get 请求

java - RESTful Jersey 请求的资源不可用

java - 使用 oAuth - fatsecret API 向 REST API 发送请求