SPRING REST:请求被拒绝,因为没有找到多部分边界

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17462642/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 06:03:54  来源:igfitidea点击:

SPRING REST: The request was rejected because no multipart boundary was found

springrestspring-mvcfile-upload

提问by Cherry

I did a POC for spring 3 rest multipart file upload. Its working fine. But when i tried integrating with my application i am facing issues.

我为 spring 3 rest 多部分文件上传做了一个 POC。它的工作正常。但是当我尝试与我的应用程序集成时,我遇到了问题。

It throws following exception:

它抛出以下异常:

org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request;
nested exception is org.apache.commons.fileupload.FileUploadException:
the request was rejected because no multipart boundary was found**"

Please let me know if I am wrong in any part of my code.

如果我的代码的任何部分有错,请告诉我。

Beans:

豆子:

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
 <property name="order" value="1" />
 <property name="mediaTypes">
 <map>
   <entry key="json" value="application/json" />
   <entry key="xml" value="application/xml" />
   <entry key="file" value="multipart/mixed" />
 </map>
</property>
</bean>
<!-- multipart resolver -->
 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  <!-- one of the properties available; the maximum file size in bytes -->
  <property name="maxUploadSize" value="50000000" />
 </bean>

Controller:

控制器:

@Controller
public class MultipleFilesRecieve {
    @RequestMapping ( value = "/saveMultiple", method = RequestMethod.POST )
        public String save( FileUploadForm uploadForm ) {
        List<MultipartFile> files = uploadForm.getFiles( );
        List<String> fileNames = new ArrayList<String>( );
        if ( null != files && files.size( ) > 0 ) {
            for ( MultipartFile multipartFile : files ) {
                String fileName = multipartFile.getOriginalFilename( );
                fileNames.add( fileName );
            }
        }
        return "multifileSuccess";
    }
}

回答by sermolaev

The problem isn't in your code - it's in your request. You're missing boundary in your multipart request. As it said in specification:

问题不在您的代码中 - 而在您的请求中。您在多部分请求中缺少边界。正如它在规范中所说:

The Content-Type field for multipart entities requires one parameter, "boundary", which is used to specify the encapsulation boundary. The encapsulation boundary is defined as a line consisting entirely of two hyphen characters ("-", decimal code 45) followed by the boundary parameter value from the Content-Type header field.

多部分实体的 Content-Type 字段需要一个参数“边界”,用于指定封装边界。封装边界定义为完全由两个连字符(“-”,十进制代码 45)组成的行,后跟来自 Content-Type 头字段的边界参数值。

This and thisposts should also be helpful.

这个这个帖子也应该有帮助。

回答by naXa

@sermolaev is right in his answer.

@sermolaev 的回答是对的。

I want to share my experience related to this problem. I've encountered this problem in Postman, but I could not understand the root cause for it for a long time. My request template seemed to be correct cause Postman included boundaryin it...

我想分享我与这个问题相关的经验。我在 Postman 中遇到过这个问题,但是我很长时间都无法理解它的根本原因。我的请求模板似乎是正确的,因为其中包含邮递员boundary...

Eventually I've discovered that when you're specifying Content-Type=multipart/formheader by yourself, it overrides the one added automatically by Postman. And this leads to the same error as yours. My solution was as simple as removing Content-Typeheader.

最终我发现当您Content-Type=multipart/form自己指定标题时,它会覆盖邮递员自动添加的标题。这会导致与您相同的错误。我的解决方案就像删除Content-Type标题一样简单。

回答by Ankit Rai

Don't supply Content-Typeheader in the request. It will work.

不要Content-Type在请求中提供标头。它会起作用。

回答by unify

Are you using any security filters? My problem was solved by removing the Security Filter Chain. From this:

您是否使用任何安全过滤器?通过删除安全过滤器链解决了我的问题。由此:

this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).addFilters(this.springSecurityFilterChain).build();

to this:

对此:

this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();

I opened an issue where I explain the details: https://jira.spring.io/browse/SPR-12114

我打开了一个问题,我解释了细节:https: //jira.spring.io/browse/SPR-12114