java - Spring WebFlux文件上传: Unsupported Media Type 415 with Multipart upload

标签 java spring spring-boot spring-webflux project-reactor

我在使用 Spring 的响应式(Reactive)框架处理文件上传时遇到了一些问题。我想我正在遵循文档,但无法摆脱此 415/不支持的媒体类型 问题。

我的 Controller 如下所示(按照此处的示例:https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html#webflux-multipart-forms)

package com.test.controllers;

import reactor.core.publisher.Flux;

import org.springframework.http.MediaType;
import org.springframework.http.codec.multipart.FilePart;
import org.springframework.http.codec.multipart.Part;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    @RequestMapping(value = "/upload", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public Flux<String> uploadHandler(@RequestBody Flux<Part> parts) {
        return parts
                .filter(part -> part instanceof FilePart)
                .ofType(FilePart.class)
                .log()
                .flatMap(p -> Flux.just(p.filename()));
    }

}

尽管发布到此端点,总是给出相同的输出:

curl -X POST -F "data=@basic.ppt" http://localhost:8080/upload
---
"Unsupported Media Type","message":"Content type 'multipart/form-data;boundary=------------------------537139718d79303c;charset=UTF-8' not supported"

我也尝试过使用@RequestPart("data"),但得到了类似的不支持的媒体类型错误,尽管是文件的内容类型。

Spring 在将这些转换为 Part 时似乎遇到问题..?我被困住了 - 感谢任何帮助!

最佳答案

嗯,这不是您问题的直接答案,因为我使用功能端点,但我希望它能以某种方式帮助您。

import org.springframework.context.annotation.Bean;
import org.springframework.http.codec.multipart.FilePart;
import org.springframework.http.codec.multipart.Part;
import org.springframework.stereotype.Controller;
import org.springframework.web.reactive.function.BodyExtractors;
import org.springframework.web.reactive.function.server.HandlerFunction;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;

import java.io.File;
import java.util.Map;

import static org.springframework.web.reactive.function.BodyInserters.fromObject;
import static org.springframework.web.reactive.function.server.RequestPredicates.POST;
import static org.springframework.web.reactive.function.server.RequestPredicates.path;
import static org.springframework.web.reactive.function.server.RouterFunctions.nest;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;

@Controller
public class FileUploadController {

    @Bean
    RouterFunction<ServerResponse> apiRoutes() {
        return nest(path("/api"),
                route(POST("/upload"), fileUpload()));
    }

    private HandlerFunction<ServerResponse> fileUpload() {
        return request -> {
            return request.body(BodyExtractors.toMultipartData()).flatMap(parts -> {
                        Map<String, Part> map = parts.toSingleValueMap();
                        final FilePart filePart = (FilePart) map.get("file");
                        final String dir = "C:\\JDeveloper\\mywork\\Spring\\SpringTest\\webflux-file-upload\\uploaded";
                        filePart.transferTo(new File(dir + "/" + filePart.filename()));

                        return ServerResponse.ok().body(fromObject("ok, file uploaded"));
                    }
            );
        };
    }

}

您可以像这样使用curl上传文件:

curl -F "file=@C:\Users\Wojtek\Desktop\img-5081775796112008742.jpg" localhost:8080/api/fileupload

关于java - Spring WebFlux文件上传: Unsupported Media Type 415 with Multipart upload,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56236662/

相关文章:

java - IllegalArgumentException : Navigation action/destination packagename/action_xxx cannot be found from the current destination

java - 数据完整性违规异常 : could not insert

java.lang.IllegalStateException : LifecycleProcessor not initialized . ...上下文 - 在我的聊天应用程序上

maven - 构建 Spring Boot 应用程序的 Docker 镜像文件

spring-boot - GCP PubSub Spring Boot 重复提取消息

java - 无法将 BufferedImage 绘制到另一个具有比例的 BufferedImage 中

java - 文本在我的多客户端聊天 GUI 应用程序中不可见?

Java 分隔符读取器

java - 如何从curl生成正确的RestTemplate用法?

java - Spring Boot 模糊映射。无法映射方法