gpt4 book ai didi

Spring Boot + Multipart 文件上传产生格式错误的多部分请求

转载 作者:行者123 更新时间:2023-12-04 15:42:28 27 4
gpt4 key购买 nike

我目前正在使用 Spring Boot v1.3.0.BUILD-SNAPSHOT 和 Spring v4.2.2.BUILD-SNAPSHOT。

如果我尝试执行多文件上传(通过 angular):

myService.upload = function(name ,content) {
var fd = new FormData();
fd.append('name', name);
fd.append('file', content);

return $http({
method: 'POST',
url: SERVER_BASE_URL + 'upload',
data: fd,
transformRequest: angular.identity,
headers: {
'Content-Type': 'multipart/form-data'
}}).then(function(res) {
return res.headers('Location');
}, function(reason) {
throw reason;
});
};

我收到错误:

[WARN] org.eclipse.jetty.server.Request - java.io.IOException: Missing initial multi part boundary at org.eclipse.jetty.util.MultiPartInputStreamParser.parse(MultiPartInputStreamParser.java:507) ~[jetty-util-9.2.13.v20150730.jar:9.2.13.v20150730] at org.eclipse.jetty.util.MultiPartInputStreamParser.getParts(MultiPartInputStreamParser.java:400) ~[jetty-util-9.2.13.v20150730.jar:9.2.13.v20150730] at org.eclipse.jetty.server.Request.getParts(Request.java:2139) [jetty-server-9.2.13.v20150730.jar:9.2.13.v20150730] at org.eclipse.jetty.server.Request.extractMultipartParameters(Request.java:385) [jetty-server-9.2.13.v20150730.jar:9.2.13.v20150730] at org.eclipse.jetty.server.Request.extractContentParameters(Request.java:308) [jetty-server-9.2.13.v20150730.jar:9.2.13.v20150730] at org.eclipse.jetty.server.Request.extractParameters(Request.java:256) [jetty-server-9.2.13.v20150730.jar:9.2.13.v20150730] at org.eclipse.jetty.server.Request.getParameter(Request.java:825) [jetty-server-9.2.13.v20150730.jar:9.2.13.v20150730] at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:70) [spring-web-4.2.2.BUILD-SNAPSHOT.jar:4.2.2.BUILD-SNAPSHOT] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.2.2.BUILD-SNAPSHOT.jar:4.2.2.BUILD-SNAPSHOT] at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652) [jetty-servlet-9.2.13.v20150730.jar:9.2.13.v20150730] at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:85) [spring-web-4.2.2.BUILD-SNAPSHOT.jar:4.2.2.BUILD-SNAPSHOT] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.2.2.BUILD-SNAPSHOT.jar:4.2.2.BUILD-SNAPSHOT] at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652) [jetty-servlet-9.2.13.v20150730.jar:9.2.13.v20150730] at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.ja [jetty-servlet-9.2.13.v20150730.jar:9.2.13.v20150730] at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143) [jetty-server-9.2.13.v20150730.jar:9.2.13.v20150730] at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:577) [jetty-security-9.2.13.v20150730.jar:9.2.13.v20150730] at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:223) [jetty-server-9.2.13.v20150730.jar:9.2.13.v20150730] at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1127) [jetty-server-9.2.13.v20150730.jar:9.2.13.v20150730] at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515) [jetty-servlet-9.2.13.v20150730.jar:9.2.13.v20150730] at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185) [jetty-server-9.2.13.v20150730.jar:9.2.13.v20150730] at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1061) [jetty-server-9.2.13.v20150730.jar:9.2.13.v20150730] at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) [jetty-server-9.2.13.v20150730.jar:9.2.13.v20150730] at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) [jetty-server-9.2.13.v20150730.jar:9.2.13.v20150730] at org.eclipse.jetty.server.Server.handle(Server.java:499) [jetty-server-9.2.13.v20150730.jar:9.2.13.v20150730] at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:310) [jetty-server-9.2.13.v20150730.jar:9.2.13.v20150730] at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:257) [jetty-server-9.2.13.v20150730.jar:9.2.13.v20150730] at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:540) [jetty-io-9.2.13.v20150730.jar:9.2.13.v20150730] at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635) [jetty-util-9.2.13.v20150730.jar:9.2.13.v20150730] at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555) [jetty-util-9.2.13.v20150730.jar:9.2.13.30]1507 at java.lang.Thread.run(Thread.java:745) [na:1.8.0_45]



我添加了一个多部分解析器 bean:
@Bean
public FilterRegistrationBean openEntityManagerFilterRegistrationBean() {
// Set upload filter
final MultipartFilter multipartFilter = new MultipartFilter();
final FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(multipartFilter);
filterRegistrationBean.addInitParameter("multipartResolverBeanName", "commonsMultipartResolver");

return filterRegistrationBean;
}

@Bean
public CommonsMultipartResolver commonsMultipartResolver() {
final CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
commonsMultipartResolver.setMaxUploadSize(-1);

return commonsMultipartResolver;
}

然而,hiddenHttpMethodFilter 似乎在我的多部分解析器之前仍然被调用:

[DEBUG] org.eclipse.jetty.servlet.ServletHandler - call filter characterEncodingFilter [DEBUG] org.eclipse.jetty.servlet.ServletHandler - call filter hiddenHttpMethodFilter [WARN] org.eclipse.jetty.util.MultiPartInputStreamParser - Badly formatted multipart request



我尝试将 @Order(0) 添加到我的 Bean 中,但这没有效果。

我还缺少什么?

最佳答案

我不使用过滤器上传文件。

在此处尝试这种方法:

// in configs
  @Bean
     Public MultipartConfigElement multipartConfigElement () {
         End MultipartConfigFactory factory = new MultipartConfigFactory ();
         Factory.setMaxFileSize ( "50MB");
         Factory.setMaxRequestSize ( "50MB");
         Return factory.createMultipartConfig ();
     }


        // in controller
@RequestMapping (value = "/ sendfile", method = RequestMethod.POST)
     Public ModelAndView formSubmit (@RequestParam ( "file") MultipartFile file) {

}

注意:请记住删除您在配置中放置的过滤器

我希望有所帮助

关于Spring Boot + Multipart 文件上传产生格式错误的多部分请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32845211/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com