如何使用 org.jboss.resteasy.client.ClientRequest 上传多部分文件?

How to upload a multipart file using org.jboss.resteasy.client.ClientRequest?

我需要维护旧版软件。我们如何使用org.jboss.resteasy.client.ClientRequest上传文件org.springframework.web.multipart.MultipartFile?

总之我想实现: curl -X POST http://mydomain/upload?sender=mr_abc -F file=@${FILE} 使用 ClientRequest${FILE} 可以是任何文件作为字符串路径示例:/Users/mr_abc/mytarfile.tar.

这是我的:

request = new ClientRequest("http://mydomain/upload");
request.header("Content-Type","multipart/form-data");
request.queryParameter("sender", "mr_abc");
request.queryParameter("file", new File("/Users/mr_abc/mytarfile.tar"));
ClientResponse<String> response = request.post(String.class);

导致错误 Required request part 'file' is not present

Part filePart = request.getPart("file"); // Retrieves <input type="file" name="file">
String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString(); // MSIE fix.
InputStream uploadedInputStream = filePart.getInputStream();

Try above code to parse request and extract file contents.

我们需要添加:

<dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-multipart-provider</artifactId>
            <version>3.0.19.Final</version>
            <scope>test</scope>
</dependency>

我之前尝试过 2.3.5.Final,但失败了,所以 3.0.19 似乎是更好的版本。 在 java 代码中需要使用 class MultiPartFormDataOutput

File file = new File("/Users/mr_abc/mytarfile.tar");
MultipartFormDataOutput upload = new MultipartFormDataOutput();
        upload.addFormData("file", targetStream, MediaType.MULTIPART_FORM_DATA_TYPE, "mytarfile.tar");

request = new ClientRequest("http://mydomain/upload");

request.queryParameter("sender", "mr_abc");
request.body(MediaType.MULTIPART_FORM_DATA_TYPE, upload);
ClientResponse<String> response = request.post(String.class);