【问题标题】:400 BAD Request error on HttpMethod.PUT - File upload second timeHttpMethod.PUT 上的 400 BAD 请求错误 - 第二次上传文件
【发布时间】:2019-05-11 22:20:49
【问题描述】:

我在上传 zip 文件的控制器中有一个 PUT 请求方法,该方法将其作为输入流并处理流。它适用于 Kb 的文件大小。现在我上传了一个 10Mb 大小的 zip 文件,它第一次运行良好。第二次,它没有上传,我收到 BAD 请求错误。当我重新启动服务时,它第一次运行良好,第二次收到相同的 BAD Request 400 错误。佩斯建议

@RequestMapping(path = “/upload/{fileName}”, method = PUT,
    consumes = "multipart/form-data", produces = "application/json; charset=UTF-8")

 public void upload(@PathVariable(“fileName”) String fileName,
            @RequestBody MultipartFile[] multipartFile) throws IOException{ 

        //inputstream is processed here

    }

【问题讨论】:

    标签: spring-boot inputstream put multipart http-method


    【解决方案1】:

    要在 Spring Boot 中上传文件,我更喜欢这种方法:

    @RequestMapping(value = "/upload", method = RequestMethod.PUT) // Or POST
    @ResponseStatus(HttpStatus.OK)
    public void upload(@RequestParam("file") MultipartFile file) {
    
        System.out.println(String.format("File name %s", file.getName()));
        System.out.println(String.format("File original name %s", file.getOriginalFilename()));
        System.out.println(String.format("File size %s", file.getSize()));
    
        //do whatever you want with the MultipartFile
        file.getInputStream();
    }
    

    在 Spring Boot 中配置多部分文件上传

    最常用的属性是:

    1. spring.http.multipart.file-size-threshold: 文件写入磁盘的阈值。支持以 MB 或 KB 作为后缀,以兆字节或千字节表示大小

    2. spring.http.multipart.location:临时文件的位置

    3. spring.http.multipart.max-file-size: 上传支持的每个文件的最大大小;还支持MB或KB后缀; 默认为 1MB

    4. spring.http.multipart.max-request-size: 整个请求的最大大小;也支持MB或KB后缀

    您当然可以在 yml 的 application.properties 中更改这些配置。

    在你的情况下,我更喜欢你去你的 rest api 并检查堆栈错误以查看确切的错误。

    【讨论】:

    • 我收到此错误“org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'multipart/form-data;charset=UTF-8' not supported”
    • 你可以使用邮递员来测试你的休息端点吗?如果是,你选择二进制,你可以把你的文件上传并告诉我你得到了什么
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-20
    • 1970-01-01
    • 1970-01-01
    • 2019-11-25
    • 2017-02-11
    • 2019-12-02
    • 2016-03-23
    相关资源
    最近更新 更多