简体   繁体   English

在 spring 应用程序中发送 zip 文件作为多部分表单请求的一部分

[英]Sending a zip file as a part of a multipart form request in a spring application

I've been running into an issue with sending a file as a part of a rest request.我在发送文件作为 rest 请求的一部分时遇到了问题。 Sending the request without the file attached has been fine, but I receive a variety of errors when the file is attached.在没有附加文件的情况下发送请求一直很好,但是在附加文件时我收到了各种错误。

I've tested sending the file in postman and have been able to do it successfully.我已经测试了在 postman 中发送文件并且能够成功发送。 The code that postman generates is: postman生成的代码是:

OkHttpClient client = new OkHttpClient().newBuilder()
  .build();
MediaType mediaType = MediaType.parse("text/plain");
RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
  .addFormDataPart("id", "123")
  .addFormDataPart("name", "test")
  .addFormDataPart("templateId", "123")
  .addFormDataPart("Files[0].file","/file/path.zip",
    RequestBody.create(MediaType.parse("application/octet-stream"),
    new File("/file/path.zip")))
  .build();
Request request = new Request.Builder()
  .url("url)
  .method("POST", body)
  .addHeader("Authorization", "Bearer token")
  .addHeader("Cookie", "JSESSIONID=sessionid")
  .build();
Response response = client.newCall(request).execute();

Where as I'm using:我在哪里使用:

        MultiValueMap<String, Object> body
                = new LinkedMultiValueMap<>();

        body.put("id", Collections.singletonList("123"));
        body.put("name", Collections.singletonList("test"));
        body.put("templateId", Collections.singletonList("123"));
        body.put("Files[0].file", Collections.singletonList(new File(filePath)));

        HttpHeaders headers = new HttpHeaders();
        headers.set(AUTHORIZATION, BEARER + token);
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);

        HttpEntity<MultiValueMap<String, Object>> requestEntity
                = new HttpEntity<>(body, headers);

        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<ResponseObject> responseObject;

        responseObject= restTemplate.exchange(url, HttpMethod.POST, requestEntity, ResponseObject.class);       

Using the above code has been successful as long as the file isn't attached, however I've tried sending it in a variety of different ways.只要文件未附加,使用上面的代码就成功了,但是我尝试以各种不同的方式发送它。 When I'm making the request the file is being captured from s3 and stored on our eks cluster and I've verified that the file is there.当我发出请求时,该文件正在从 s3 捕获并存储在我们的 eks 集群中,并且我已验证该文件在那里。 The main difference between the two requests from what I can see is that Postman is setting a media type of application/octet-stream just for the file.从我所见,这两个请求之间的主要区别在于 Postman 仅为文件设置了应用程序/八位字节流的媒体类型。

The request appears to want a string, so I've tried sending the file as a byte array and input stream to no avail...although based off the postman code, it seems a File object would be fine.该请求似乎需要一个字符串,因此我尝试将文件作为字节数组发送并输入 stream 无济于事......尽管基于 postman 代码,它似乎是一个文件 ZA8CFDE6331BD59EB2AC66F8911C4B6。

Edit: The request also works sending it like this:编辑:该请求也可以像这样发送它:

MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addBinaryBody(FILE, new File(filePath));
        builder.addTextBody(FILE_NAME, fileName);
        builder.addTextBody(CUSTOMER_ID, String.valueOf(id));
        builder.addTextBody(name, name);
        builder.addTextBody(TEMPLATE_ID, String.valueOf(templateId));
        

How can I add the file as a binary in the multivalue map?如何将文件作为二进制文件添加到多值 map 中?

I needed to be sending the file as a binary in the multivalue map, based on how the other requests were working.根据其他请求的工作方式,我需要将文件作为多值 map 中的二进制文件发送。

I found this link: RestTemplate send file as bytes from one controller to another我找到了这个链接: RestTemplate send file as bytes from one controller to another

Which mentioned the ByteArrayResource.其中提到了ByteArrayResource。 Having implemented this into my code (below) I can now send the request successfully.在我的代码(如下)中实现了这一点后,我现在可以成功发送请求。

File file = new File(filePath);

        ByteArrayResource resource = new ByteArrayResource(FileUtils.readFileToByteArray(file)) {
            @Override
            public String getFilename() {
                return file.getName();
            }
        };

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React Spring 使用多部分表单数据启动应用程序 - 所需的请求部分“文件”不存在 - React Spring Boot App Using Multipart Form Data - Required request part 'file' is not present 带有@ModelAttribute的Spring Mvc应用程序PUT请求与多部分请求表单未绑定 - Spring Mvc application PUT request with @ModelAttribute with multipart request form not binding Spring 测试:发送数据文件的多部分 - Spring Tests: sending multipart for data file Spring org.springframework.web.multipart.support.MissingServletRequestPartException,所需的请求部分“文件”不存在 - Spring org.springframework.web.multipart.support.MissingServletRequestPartException, Required request part 'file' is not present 多部分表单请求未到达 SPRING Controller - MultiPart Form Request is not reaching the SPRING Controller GAE使用SSL从blobstore发送包含多部分/表单数据和文件的发布请求 - GAE sending post request with multipart/form-data, file from blobstore with SSL 春季多个分段请求文件上传-分段大小为0 - spring multiple multipart request file upload - multipart size is 0 如何修复 Spring 引导分段上传中的“所需请求部分不存在” - How to fix "Required request part is not present" in a Spring Boot multipart upload Spring WebClient 多部分/表单数据请求,无法发送文件 - Spring WebClient multipart/form-data request, Could not able to send file 如何从Spring MVC中的角度多部分/表单数据请求中检索文件 - How to retrieve file from angular multipart/form data request in spring mvc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM