简体   繁体   English

Spring 文件上传 - “所需的请求部分不存在”

[英]Spring File Upload - 'Required request part is not present'

I am trying to send POST request to my controller but cannot pass any parameter in any type unless I decide to use JSON.我正在尝试向我的 controller 发送 POST 请求,但除非我决定使用 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.我的目标是将字符串和文件传递给我的 controller 但我不断收到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:我通过 Postman 发送请求,如下面的截图所示:

邮递员请求

邮递员请求 2

The only header tag is for Authorization.唯一的 header 标签用于授权。 I do not have any Content-Type header, I tried to add multipart/form-data but did not help.我没有任何 Content-Type header,我尝试添加multipart/form-data但没有帮助。

Only way I could pass String parameter is by adding to URL.我可以传递字符串参数的唯一方法是添加到 URL。 So following http://localhost:8080/SearchBox/admin/config?name=test works but this is not what I want.因此,遵循http://localhost:8080/SearchBox/admin/config?name=test有效,但这不是我想要的。 I want String and File parameters in Body part.我想要正文部分中的字符串和文件参数。

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注意:我已经检查过类似的帖子,但没有帮助This , This , This

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);
    }
}
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public ResponseEntity<?> upload(@RequestParam(value = "name") String 
name,@RequestParam(value = "file") MultipartFile file){
    // TODO check file is not null and save 
    return new ResponseEntity<>(HttpStatus.valueOf(200));;
}

在此处输入图像描述

Add Bean to Config file.将 Bean 添加到配置文件。

@Bean(name = "multipartResolver")
public CommonsMultipartResolver multipartResolver() {
    CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
    multipartResolver.setMaxUploadSize(-1);
    return multipartResolver;

}

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;虽然这不是原始发布者的问题,但如果您发现此问题并且上述解决方案均不适合您,请检查您的 Postman 标头; if "Content-Type" header is set, this may cause the error above.如果设置了“Content-Type”header,这可能会导致上述错误。

Remove the "Content-Type" header and it may resolve the issue above.删除“Content-Type”header 可能会解决上述问题。

See this question for more information:有关更多信息,请参阅此问题:

Postman: Required request part 'file' is not present Postman:所需的请求部分“文件”不存在

If you still getting error, try to remove all headers from postman and drop type of file如果您仍然收到错误,请尝试从 postman 中删除所有标题并删除文件类型

In my case the issue was that my csv was not named as per the string set within在我的情况下,问题是我的csv 没有按照其中设置的字符串命名

@PostMapping("/upload")
    public ResponseEntity<ResponseMessage> uploadFile(@RequestParam("file") MultipartFile file) {

在此处输入图像描述

在此处输入图像描述

I suspect that main reason is @RequestParam("file") there should be @RequestBody instead.我怀疑主要原因是 @RequestParam("file") 应该是 @RequestBody 。

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.如果请求正文通过代理或任何其他可能不支持 multipart/form-data 甚至 octet-stream 内容类型的中介,请考虑您的请求正文可能无法到达您的服务器。 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.我还可以建议您配置一个请求拦截器,以便您可以在 controller 之前和之后记录您的请求,这可能有助于您了解请求参数、有效负载和标头。 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.就我而言,我意识到我的请求正文的大小为 0,这有助于我检测导致此错误的原因不是我的 spring 配置。 Here I leave a useful resource to help you with the logging interceptor which Spring has out of the box.在这里,我留下一个有用的资源来帮助您使用 Spring 开箱即用的日志拦截器。 enter link description here在此处输入链接描述

Really not sure what was going on but I removed the code真的不知道发生了什么,但我删除了代码

=======REMOVE BELOW======== =======删除下面========

@Bean
public MultipartConfigElement multipartConfigElement() {
    return new MultipartConfigElement("");
}

@Bean
public MultipartResolver multipartResolver() {

Added below:在下面添加:

spring.servlet.multipart.max-file-size=-1 
spring.servlet.multipart.max-request-size=-1

And, it starting working magically.而且,它开始神奇地工作。 Maybe thats how it was with my spring version也许这就是我的 spring 版本的情况

In my case the name attribute of the input type file was not set as "file" .在我的情况下,输入类型文件的name 属性未设置为"file"

<input type="file" class="file-input" id="fileUpload" name="file">

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
    }
}

Example of a multipart/form-data JMeter POST: HTTP Header Manager: Accept / Content-Type multipart/form-data;多部分/表单数据 JMeter POST 示例:HTTP Header 管理器:接受/内容类型多部分/表单数据; boundary=------------------------jm888 Expect 100-continue User-Agent jmeter/3.3 HTTP Request: uncheck the following: Redirect Automatically Follow Redirects User KeepAlive Use multipart/form-data for POST Browser-compatible headers Select Body Data tab sample of Body Data: ------------------------jm888 Content-Disposition: form-data;边界=------------jm888 期望 100 次继续用户代理 jmeter/3.3 HTTP 请求:取消选中以下:重定向自动跟随重定向用户 KeepAlive将 multipart/form-data 用于 POST 浏览器兼容的标头 Select 主体数据的主体数据选项卡示例:------------jm888 内容处置:表格数据; name="microsvc" Content-Type: application/json name="microsvc" 内容类型:应用程序/json

{ "orgId": 1, "ResourceId": "0032ade8-cd59-45b6-8fff-299a63442474", "StatusId": "00d8ff46-c688-47cb-85a2-8edfaa1db9a2", "TypeId": "a972697e-735e-11e7-8cf7-a6006ad3dba0", "Id": "2a1773fe-dbb8-4510-b051-32b2ad823ea1" } ------------------------jm888 Content-Disposition: form-data; { “orgId”:1,“ResourceId”:“0032ade8-cd59-45b6-8fff-299a63442474”,“StatusId”:“00d8ff46-c688-47cb-85a2-8edfaa1db9a2”,“TypeId”:“a972697e-735e-11e7- 8cf7-a6006ad3dba0", "Id": "2a1773fe-dbb8-4510-b051-32b2ad823ea1" } ------------------------jm888 内容处置:表格数据; name="payload";名称="有效载荷"; filename="1349.xml" Content-Type: application/xml文件名="1349.xml" 内容类型:应用程序/xml

${__FileToString(C:\jmeterData\sourceData\1349.xml,,)} ------------------------jm888-- ${__FileToString(C:\jmeterData\sourceData\1349.xml,,)} ------------------------jm888--

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Thymeleaf所需的请求部分“文件”不存在 - Spring Thymeleaf Required request part 'file' is not present Spring Boot Required 请求部分“文件”不存在 - Spring Boot Required request part 'file' is not present AngularJS JSON Spring MVC 应用程序中的文件上传 400 Bad Request Required 请求部分不存在 - File Upload in AngularJS JSON Spring MVC application 400 Bad Request Required request part is not present 如何修复 Spring 引导分段上传中的“所需请求部分不存在” - How to fix "Required request part is not present" in a Spring Boot multipart upload 所需的请求部分“文件”不存在。 尝试上传图像,角度-&gt;春天 - Required request part 'file' is not present. Trying upload an image, angular -> spring 上传文件springboot所需的请求部分“文件”不存在 - upload file springboot Required request part 'file' is not present Tomcat:所需的请求部分“文件”不存在 - Tomcat : Required request part 'file' is not present 出现“不存在所需的请求部分&#39;文件&#39;”错误 - Getting “Required request part 'file' is not present” error MissingServletRequestPartException:所需的请求部分“文件”不存在 Springboot - MissingServletRequestPartException: Required request part 'file' is not present Springboot React Spring 使用多部分表单数据启动应用程序 - 所需的请求部分“文件”不存在 - React Spring Boot App Using Multipart Form Data - Required request part 'file' is not present
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM