【问题标题】:Curl post multipart file to spring boot rest endpoint将多部分文件卷曲到 Spring Boot 休息端点
【发布时间】: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


【解决方案1】:

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

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

【讨论】:

  • 我试过这个,但不幸的是它不起作用。多部分边界有些奇怪
  • @dopatraman 奇怪,我使用了您提供的相同代码,并且对我来说工作正常
【解决方案2】:

我认为这是因为这里的file 不是参数。根据Spring web,一个参数是一个'查询'参数,例如/api?foo=barfoo是这个请求中的参数。

因此,在您的情况下,Spring 抱怨缺少参数 file,因为它希望请求看起来像 http://localhost:8081/qas/uploadCsv?file=something

更改方法签名

handleFileUpload(@RequestParam("file") MultipartFile file)

handleFileUpload(@RequestPart("file") MultipartFile file)

应该解决你的问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-29
    • 1970-01-01
    • 2013-02-19
    • 2018-04-10
    • 2016-05-06
    • 2015-01-31
    • 2014-10-31
    相关资源
    最近更新 更多