簡體   English   中英

Spring WebFlux文件上傳:不支持分段上傳的415類型媒體

[英]Spring WebFlux File Upload: Unsupported Media Type 415 with Multipart upload

我在使用Spring的反應式框架處理文件上傳時遇到一些問題。 我認為我正在關注文檔,但是無法擺脫415 / Unsupported Media Type問題。

我的控制器如下所示(按照此處的示例: 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") ,但是出現類似的Unsupported Media Type錯誤,盡管文件的內容類型也是如此。

看來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

感謝@kojot的回答,但是在這種情況下,我發現問題是除了spring-webflux之外,我spring-webmvc暫時包含了spring-webflux 您的解決方案可能也可以使用,但是我想堅持使用Controller樣式,因此最終從build.gradle強制排除了spring-webmvc build.gradle

configurations {
    implementation {
        exclude group: 'org.springframework', module: 'spring-webmvc'
    }
}

在那之后,它按照記錄工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM