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

查看:22
本文介绍了用于附加文件的 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 具有特殊的格式.如果服务器需要 multipart/form-data 格式,我们就不能将它作为普通请求发送.可以在 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>

<小时>

  • 查看多部分输出的更多信息/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天全站免登陆