gpt4 book ai didi

spring - Curl 将多部分文件发布到 Spring Boot Rest 端点

转载 作者:行者123 更新时间:2023-12-03 17:36:27 35 4
gpt4 key购买 nike

我正在尝试使用 Curl 将文件发布到在 Spring Boot 中实现的 Restful 端点,它抛出以下错误:

    $ curl -v http://localhost:8081/qas/uploadCsv -X POST -F "file=@test.csv"
Note: Unnecessary use of -X or --request, POST is already inferred.
* timeout on name lookup is not supported
* Trying ::1...
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Connected to localhost (::1) port 8081 (#0)
> POST /qas/uploadCsv HTTP/1.1
> Host: localhost:8081
> User-Agent: curl/7.46.0
> Accept: */*
> Content-Length: 4257762
> Expect: 100-continue
> Content-Type: multipart/form-data; boundary=------------------------e16823418f4c8f54
>
< HTTP/1.1 100 Continue
} [155 bytes data]
< HTTP/1.1 400 Bad Request
< Server: Apache-Coyote/1.1
< X-Application-Context: application:8081
< Content-Type: application/json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Mon, 11 Apr 2016 14:27:42 GMT
< Connection: close
<
{ [247 bytes data]
100 4158k 0 236 100 4157k 1888 32.4M --:--:-- --:--:-- --:--:-- 32.4M{"timestamp":1460384862046,"status":400,"error":"Bad Request","exception":"org.springframework.web.bind.MissingServletRequestParameterException","message":"Required MultipartFile parameter 'file' is not present","path":"/qas/uploadCsv"}
* Closing connection 0

我一定遗漏了一些基本的东西,但看不到它是什么。它正在寻找请求参数"file",但不确定如何通过 Curl 发送它。

Spring java 配置具有以下 bean:
@Bean
public MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
factory.setMaxFileSize("1000MB");
factory.setMaxRequestSize("1000MB");
return factory.createMultipartConfig();
}

@Bean
public MultipartResolver multipartResolver() {
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
multipartResolver.setMaxUploadSizePerFile(10000000);
return multipartResolver;
}

我的spring boot web服务如下:
@RequestMapping(value = "/uploadCsv", method = RequestMethod.POST, consumes = {"multipart/*"})
public @ResponseBody result handleFileUpload(@RequestParam("file") MultipartFile file) {

在上面的签名中,它正在寻找与 cUrl 错误相关的 requestParam 文件。
    "status":400,"error":"Bad Request",
"exception":"org.springframework.web.bind.MissingServletRequestParameterException",
"message":"Required MultipartFile parameter 'file' is not present","path":"/qas/uploadCsv"}

此请求适用于提交以下请求的 postman :
POST /qas/uploadCsv HTTP/1.1
Host: localhost:8081
Cache-Control: no-cache
Postman-Token: d62a469b-16c8-b30a-4168-7622d9695c57
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file"; filename=""
Content-Type:


----WebKitFormBoundary7MA4YWxkTrZu0gW

我还有一个可以正常工作的 mockMvc 集成测试。比较 cUrl 中的两个(cUrl 与 postman ),没有包含文件参数的内容配置。在谷歌上环顾四周,看不到发送此请求?任何指针

提前致谢

更新:

我尝试了以下链接中的解决方案

File upload working under Jetty but not under Tomcat

在文件上方使用相同的 cURL 命令是 null 抛出以下错误
$ curl -v http://localhost:8081/qas/uploadCsv -X POST -F "file=@test.csv" -H "Content-Type: multipart/form-data"
Note: Unnecessary use of -X or --request, POST is already inferred.
* timeout on name lookup is not supported
* Trying ::1...
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Connected to localhost (::1) port 8081 (#0)
> POST /qas/uploadCsv HTTP/1.1
> Host: localhost:8081
> User-Agent: curl/7.46.0
> Accept: */*
> Content-Length: 4257762
> Expect: 100-continue
> Content-Type: multipart/form-data; boundary=------------------------e6cd31736e52dfa2
>
< HTTP/1.1 100 Continue
} [155 bytes data]
100 4157k 0 0 100 4157k 0 681k 0:00:06 0:00:06 --:--:-- 0< HTTP/1.1 500 Internal Server Error
< Server: Apache-Coyote/1.1
< X-Application-Context: application:8081
< Content-Type: application/json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Tue, 12 Apr 2016 07:23:18 GMT
< Connection: close
<
100 4157k 0 0 100 4157k 0 622k 0:00:06 0:00:06 --:--:-- 0{ [4 bytes data]
100 4158k 0 174 100 4157k 26 622k 0:00:06 0:00:06 --:--:-- 0{"timestamp":1460445797961,"status":500,"error":"Internal Server Error","exception":"java.lang.NullPointerException","message":"No message available","path":"/qas/uploadCsv"}
* Closing connection 0

最佳答案

卷发 POST + 多部分
要使用文件 POST,请添加此 -F 文件=@"path/to/data.txt "

curl -F file=@"test.csv" http://localhost:8081/qas/uploadCsv

关于spring - Curl 将多部分文件发布到 Spring Boot Rest 端点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36551963/

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