用于附加文件的RESTeasy客户端代码 [英] RESTeasy client code for attaching a file

查看:142
本文介绍了用于附加文件的RESTeasy客户端代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将文件附加到我的服务端点。
我通过POSTMAN测试了这个功能(chrome浏览器插件测试休息服务),它工作正常。

I need to attach a file to my service end-point . I tested the functionality via POSTMAN ( chrome browser plugin to test rest service ) , it is working fine.

但是我需要用JUNIT测试它。
对于这种情况,我使用的是RESTeasy客户端。

But I need to test the same with JUNIT . For that case I am using RESTeasy client .

我正在尝试使用此代码:

I was trying with this code :

    StringBuilder sb = new StringBuilder();

    BufferedReader br = new BufferedReader(new FileReader("C:/Temp/tempfile.txt"));
    try {
        String line = br.readLine();
        while (line != null) {
            sb.append(line);
            sb.append(System.lineSeparator());
            line = br.readLine();
        }
    }
    finally {
        br.close();
    }

    byte[] file = sb.toString().getBytes();

Client client = ClientBuilder.newClient();
        Invocation.Builder builder = client.target(webTarget.getUri()
                + "/attachment" ).request(MediaType.MULTIPART_FORM_DATA_TYPE);

        Response response = builder.post(Entity.entity(file, MediaType.MULTIPART_FORM_DATA), Response.class);

但我收到错误:


org.apache.commons.fileupload.FileUploadException:请求被拒绝,因为未找到多部分边界

org.apache.commons.fileupload.FileUploadException : the request was rejected because no multipart boundary was found

这有什么解决方案吗?

或者有人可以提供示例RESTeasy rest客户端代码来附加文件吗?

Or can anybody give a sample RESTeasy rest client code to attach a file ?

推荐答案

Multipart具有特殊格式。如果服务器需要多部分/表单数据格式,我们不能将其作为普通请求发送。您可以查看Postman中的预览窗口以查看格式

Multipart has a special format. If the server is expecting a multipart/form-data format, we can't just send it as a normal request. You can look at the preview window in Postman to see the format


您可以看到每个零件都有边界。我们不必担心手动设置它。 Resteasy有一个用于构建多形式输出的API。您可以使用 MultipartFormDataOutput 类来构建输出。只需使用 addFormData 方法添加部件即可。在你的情况下它只有一个部分,但请求仍然会按照服务器的预期格式进行格式化。

You can see that each part has a boundary. We don't really have to worry about setting this manually. Resteasy has an API for building multiform output. You can use the MultipartFormDataOutput class to build the output. Just use the addFormData method to add parts. In your case its only one part, but the request will still get formatted the way the server is expecting.

所以你的请求看起来应该更像是

So your request should look something more like

MultipartFormDataOutput output = new MultipartFormDataOutput();
                      // file (below) doesn't have to be a `byte[]`
                      // It can be a `File` object and work just the same
output.addFormData("file", file, MediaType.APPLICATION_OCTET_STREAM_TYPE);

Response response = target.request()
        .post(Entity.entity(output, MediaType.MULTIPART_FORM_DATA));

这假设您具有所需的依赖关系,因为我想成像,如果服务器正在接受多部分

This is assuming you have the required dependency, as I imaging you would, if the server is accepting multipart

<dependency>
   <groupId>org.jboss.resteasy</groupId>
   <artifactId>resteasy-multipart-provider</artifactId>
   <version>${resteasy.version}</version>
</dependency>







  • 查看有关使用multipart / form-data输出的更多信息


    • See more about Output with multipart/form-data
    • 对于任何对服务器端感到好奇的读者(因为你没有提供代码),这就是我用来测试的东西

      For any future readers who are curious about server side (since you haven't provided your code), this is what I used to test

      @Path("/multipart")
      public class MultipartResource {
      
          @POST
          @Consumes(MediaType.MULTIPART_FORM_DATA)
          public Response postData(MultipartFormDataInput input) throws Exception {
      
              byte[] bytes = input.getFormDataPart("file", byte[].class, null);
              JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(bytes)));
      
              return Response.ok("GOT IT").build();
          }
      }
      

      这篇关于用于附加文件的RESTeasy客户端代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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