【发布时间】:2016-04-11 14:45:23
【问题描述】:
我正在尝试将文件发布到使用 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
我一定错过了一些基本的东西,但看不到它是什么。它正在寻找请求参数“文件”,但不确定如何通过 Curl 发送它。
Spring java config 有以下 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"}
此请求适用于提交以下请求的邮递员:
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 与邮递员),没有包含文件参数的内容处置。在谷歌上环顾四周,看不到发送此请求?任何指针
提前致谢
更新:
我尝试了以下链接中的解决方案
File upload working under Jetty but not under Tomcat
在文件上方使用相同的 cURL 命令为空,抛出下面的错误
$ 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
【问题讨论】:
-
@dopatraman - 我无法使用最新版本的 curl/spring boot 重新创建它。你能否通过更新的更新和可能的重现步骤来恢复这篇文章?
-
在我看来你的 curl 命令是正确的,可能是服务器代码有问题
-
我在spring boot 2.4.1上试过你的代码,没有问题。可以分享一下spring boot的版本吗?
-
我的curl版本是7.71.1,你试过升级curl版本吗?
-
您是否尝试过使用 Postman 生成的 curl 命令?鉴于 Postman 工作正常,看看它生成的 curl 会很有趣。
标签: spring rest curl spring-boot