繁体   English   中英

如何修复 Spring 引导分段上传中的“所需请求部分不存在”

[英]How to fix "Required request part is not present" in a Spring Boot multipart upload

虽然我的问题与类似,但我没有使用MockMultipartFile 这是我的用例。

在 Spring Boot 2.4.5 应用程序中,我编写了一个执行代码的单元测试,它应该发送一个multipart/form-data请求。 为了能够从测试中测试接收端,我启动了一个Controller作为multipart/form-data调用的接收器。 这是 controller(它目前没有对参数做任何事情)。

import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/v1/api")
public class VideoServiceStub {
    @PostMapping(value ="/videoservice/videos", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseEntity<String> upload(@RequestPart("name") MultipartFile name, @RequestPart("file") MultipartFile file) {
        return ResponseEntity.status(HttpStatus.OK).body("it works");
    }
}

我的用例的特别之处在于 POST 应由内存中的字节数组InputStream制成。 为了提供一些上下文,我的代码稍后将从 URL 下载一个文件,并使这个multipart/form-data中继数据并将其发送到其他地方。 单元测试将触发以下代码执行,该代码使用 Spring Rest 模板。

protected void sendToVideoService(String name, byte[] content) {
        var headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);

        var body = new LinkedMultiValueMap<>();
        body.add("name", name);
        body.add("file", new ByteArrayResource(content));

        var url = this.uploadUrl();  // the test will return the correct localhost URL here

        var requestEntity = new HttpEntity<>(body, headers);
        restTemplate.postForEntity(url, requestEntity, String.class);        
    }

它以HttpClientErrorException$BadRequest失败,我可以在标准输出日志中看到:

2021-06-01 07:23:33.027  WARN 27432 --- [o-auto-1-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : 
Resolved [org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'name' is not present]

为了验证问题出在接收端,我使用较新的java.net.http.HttpClient和出色的甲醇库测试了一个版本。

protected void sendToVideoServiceUsingJava11(String name, byte[] content) {
    var url = this.uploadUrl(); // the test will return the correct localhost URL here
    var methanol = Methanol.create();
    var multipartBody = MultipartBodyPublisher.newBuilder()
            .textPart("name", name)
            .formPart("file", HttpRequest.BodyPublishers.ofByteArray(content))
            .build();

    var request = MutableRequest.POST(url, multipartBody);
    methanol.send(request, HttpResponse.BodyHandlers.ofString());
}

我可以在标准输出中看到完全相同的警告:

2021-06-01 07:22:35.086  WARN 23840 --- [o-auto-1-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : 
Resolved [org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'name' is not present]
400

最后,我的application.yaml中有这个:

spring:
  servlet:
    multipart:
      max-file-size: 750MB
      max-request-size: 750MB

server:
  tomcat:
    max-swallow-size: 786432000
    max-http-form-post-size: 786432000

logging:
  level:
    org:
      springframework:
        web:
          filter:
            CommonsRequestLoggingFilter: DEBUG

我设法解决了这个问题。 VideoServiceStub中, name参数必须是String而不是MultipartFile

@PostMapping(value ="/videoservice/videos", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<String> upload(@RequestPart("name") String name, @RequestPart("file") MultipartFile file) {
    return ResponseEntity.status(HttpStatus.OK).body("it works");
}

此外,在sendToVideoServiceUsingJava11中,如果没有filename ,它就无法工作:

.formPart("file", "filename.mp4", HttpRequest.BodyPublishers.ofByteArray(content))

我从来没有让 Spring REST 模板示例工作。 这样做时无法传递文件名: body.add("file", new ByteArrayResource(content)) 也许有人有想法?

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM