将多部分文件卷曲到弹簧引导休息端点

时间:2016-04-11 14:45:23

标签: spring rest curl spring-boot

我正在尝试将文件发布到使用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;
    }

我的春季启动网络服务如下:

@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 vs 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

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

我认为这是由于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)

应该解决您的问题