gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:52:18 27 4
gpt4 key购买 nike

我在使用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com