Java Spring文件上传 - '所需的请求部分不存在'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46563182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring File Upload - 'Required request part is not present'
提问by Gokhan Celikkaya
I am trying to send POST request to my controller but cannot pass any parameter in any type unless I decide to use JSON. My goal is to pass a String and a file to my controller but I keep getting Required request part 'xxx' is not present
error.
我正在尝试向我的控制器发送 POST 请求,但除非我决定使用 JSON,否则无法传递任何类型的任何参数。我的目标是将一个字符串和一个文件传递给我的控制器,但我不断收到Required request part 'xxx' is not present
错误消息。
@RestController
public class ConfigurationController {
@PostMapping(value = "/config")
public ResponseEntity<?> saveEnvironmentConfig(@RequestParam("file") MultipartFile uploadfile){
return ResponseEntity.ok().body(null);
}
}
I cannot have file here. Similarly if I try:
我这里不能有文件。同样,如果我尝试:
@RestController
public class ConfigurationController {
@PostMapping(value = "/config")
public ResponseEntity<?> saveEnvironmentConfig(@RequestParam("name") String name){
return ResponseEntity.ok().body(null);
}
}
same thing I cannot get name here.
同样的事情我在这里找不到名字。
I am sending request via Postman as given in following screenshot:
我通过邮递员发送请求,如下面的截图所示:
The only header tag is for Authorization. I do not have any Content-Type header, I tried to add multipart/form-data
but did not help.
唯一的标头标签用于授权。我没有任何 Content-Type 标头,我尝试添加multipart/form-data
但没有帮助。
Only way I could pass String parameter is by adding to URL. So following http://localhost:8080/SearchBox/admin/config?name=test
works but this is not what I want. I want String and File parameters in Body part.
我可以传递字符串参数的唯一方法是添加到 URL。所以下面的http://localhost:8080/SearchBox/admin/config?name=test
作品,但这不是我想要的。我想要正文部分中的字符串和文件参数。
I also tested via CURL:
我还通过 CURL 进行了测试:
curl -X POST -H "Authorization:Bearer myToken" -H "Content-Type:Multipart/form-data" http://localhost:8080/SearchBox/admin/config --data 'pwd=pwd'
curl -X POST -H "Authorization:Bearer myToken"http://localhost:8080/SearchBox/admin/config --data 'pwd=pwd'
curl -H "Authorization:Bearer myToken" -F file=@"/g123.conf" http://localhost:8080/SearchBox/admin/config
Note:I checked similar posts already but did not help This, This, This
采纳答案by Gokhan Celikkaya
I finally solved the issue and sharing my solution in case someone else may face the same problem.
我终于解决了这个问题并分享了我的解决方案,以防其他人可能面临同样的问题。
@RestController
@RequestMapping("/")
public class ConfigurationController {
@Bean
public MultipartConfigElement multipartConfigElement() {
return new MultipartConfigElement("");
}
@Bean
public MultipartResolver multipartResolver() {
org.springframework.web.multipart.commons.CommonsMultipartResolver multipartResolver = new org.springframework.web.multipart.commons.CommonsMultipartResolver();
multipartResolver.setMaxUploadSize(1000000);
return multipartResolver;
}
@PostMapping(value = "/config", consumes = "multipart/form-data")
public ResponseEntity<?> saveEnvironmentConfig(@RequestParam("password") String password, @RequestParam("file") MultipartFile submissions)
throws AdminAuthenticationException, ConfigurationException {
return ResponseEntity.ok().body(null);
}
}
回答by Michal Cholewiński
I suspect that main reason is @RequestParam("file") there should be @RequestBody instead.
我怀疑主要原因是 @RequestParam("file") 应该是 @RequestBody 。
回答by baba
回答by drew
While this was not the original poster's problem, if you found this question and none of the above solutions worked for you, check your Postman headers; if "Content-Type" header is set, this may cause the error above.
虽然这不是原始海报的问题,但如果您发现了这个问题并且上述解决方案都不适合您,请检查您的 Postman 标题;如果设置了“Content-Type”标头,则可能会导致上述错误。
Remove the "Content-Type" header and it may resolve the issue above.
删除“Content-Type”标头,它可能会解决上述问题。
See this question for more information:
有关更多信息,请参阅此问题:
回答by Mahendra Sri
Add Bean to Config file.
将 Bean 添加到配置文件。
@Bean(name = "multipartResolver")
public CommonsMultipartResolver multipartResolver() {
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
multipartResolver.setMaxUploadSize(-1);
return multipartResolver;
}
回答by darkconeja
Consider also the possibility of your request body not reaching your server if it's going through a proxy or any other intermediary that may not support multipart/form-data or even octet-stream content types. Hopefully, this can be solved with extra configuration to work this kind of requests. I can also recommend you to configure a request interceptor so you can log your request before and after your controller, this might help you getting a peek into the request parameters, payload and headers. In my case I realized that the size of my request body was 0, which help me to detect what was causing this error wasn't my spring configuration. HereI leave a useful resource to help you with the logging interceptor which Spring has out of the box. enter link description here
如果您的请求正文通过代理或任何其他可能不支持 multipart/form-data 甚至八位字节流内容类型的中介,请考虑您的请求正文无法到达您的服务器的可能性。希望这可以通过额外的配置来解决,以处理此类请求。我还可以建议您配置一个请求拦截器,以便您可以在控制器之前和之后记录您的请求,这可能有助于您了解请求参数、有效负载和标头。就我而言,我意识到我的请求正文的大小为 0,这有助于我检测导致此错误的原因不是我的 spring 配置。在这里,我留下了一个有用的资源来帮助您使用 Spring 开箱即用的日志拦截器。在此处输入链接描述
回答by Aik
In my case solution was missing '@EnableWebMvc' annotation on configuration
在我的情况下,解决方案在配置上缺少“@EnableWebMvc”注释
@EnableWebMvc
@Configuration
internal class BulkOffersUploadConfig {
@Bean
fun multipartResolver(): MultipartResolver {
val multipartResolver = CommonsMultipartResolver()
multipartResolver.setMaxUploadSize(1000)
multipartResolver.setMaxUploadSizePerFile(1000)
return multipartResolver
}
}