我目前正在运行Spring Boot v1.3.0.BUILD-SNAPSHOT和Spring v4.2.2.BUILD-SNAPSHOT.
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 –@H_502_17@ java.io.IOException: Missing initial multi part boundary@H_502_17@ at org.eclipse.jetty.util.MultiPartInputStreamParser.parse(MultiPartInputStreamParser.java:507) ~[jetty-util-9.2.13.v20150730.jar:9.2.13.v20150730]@H_502_17@ at org.eclipse.jetty.util.MultiPartInputStreamParser.getParts(MultiPartInputStreamParser.java:400) ~[jetty-util-9.2.13.v20150730.jar:9.2.13.v20150730]@H_502_17@ at org.eclipse.jetty.server.Request.getParts(Request.java:2139) [jetty-server-9.2.13.v20150730.jar:9.2.13.v20150730]@H_502_17@ at org.eclipse.jetty.server.Request.extractMultipartParameters(Request.java:385) [jetty-server-9.2.13.v20150730.jar:9.2.13.v20150730]@H_502_17@ at org.eclipse.jetty.server.Request.extractContentParameters(Request.java:308) [jetty-server-9.2.13.v20150730.jar:9.2.13.v20150730]@H_502_17@ at org.eclipse.jetty.server.Request.extractParameters(Request.java:256) [jetty-server-9.2.13.v20150730.jar:9.2.13.v20150730]@H_502_17@ at org.eclipse.jetty.server.Request.getParameter(Request.java:825) [jetty-server-9.2.13.v20150730.jar:9.2.13.v20150730]@H_502_17@ at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:70) [spring-web-4.2.2.BUILD-SNAPSHOT.jar:4.2.2.BUILD-SNAPSHOT]@H_502_17@ at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.2.2.BUILD-SNAPSHOT.jar:4.2.2.BUILD-SNAPSHOT]@H_502_17@ at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652) [jetty-servlet-9.2.13.v20150730.jar:9.2.13.v20150730]@H_502_17@ at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:85) [spring-web-4.2.2.BUILD-SNAPSHOT.jar:4.2.2.BUILD-SNAPSHOT]@H_502_17@ at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.2.2.BUILD-SNAPSHOT.jar:4.2.2.BUILD-SNAPSHOT]@H_502_17@ at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652) [jetty-servlet-9.2.13.v20150730.jar:9.2.13.v20150730]@H_502_17@ at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.ja [jetty-servlet-9.2.13.v20150730.jar:9.2.13.v20150730]@H_502_17@ at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143) [jetty-server-9.2.13.v20150730.jar:9.2.13.v20150730]@H_502_17@ at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:577) [jetty-security-9.2.13.v20150730.jar:9.2.13.v20150730]@H_502_17@ at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:223) [jetty-server-9.2.13.v20150730.jar:9.2.13.v20150730]@H_502_17@ at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1127) [jetty-server-9.2.13.v20150730.jar:9.2.13.v20150730]@H_502_17@ at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515) [jetty-servlet-9.2.13.v20150730.jar:9.2.13.v20150730]@H_502_17@ at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185) [jetty-server-9.2.13.v20150730.jar:9.2.13.v20150730]@H_502_17@ at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1061) [jetty-server-9.2.13.v20150730.jar:9.2.13.v20150730]@H_502_17@ at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) [jetty-server-9.2.13.v20150730.jar:9.2.13.v20150730]@H_502_17@ at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) [jetty-server-9.2.13.v20150730.jar:9.2.13.v20150730]@H_502_17@ at org.eclipse.jetty.server.Server.handle(Server.java:499) [jetty-server-9.2.13.v20150730.jar:9.2.13.v20150730]@H_502_17@ at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:310) [jetty-server-9.2.13.v20150730.jar:9.2.13.v20150730]@H_502_17@ at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:257) [jetty-server-9.2.13.v20150730.jar:9.2.13.v20150730]@H_502_17@ at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:540) [jetty-io-9.2.13.v20150730.jar:9.2.13.v20150730]@H_502_17@ at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635) [jetty-util-9.2.13.v20150730.jar:9.2.13.v20150730]@H_502_17@ at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555) [jetty-util-9.2.13.v20150730.jar:9.2.13.30]1507@H_502_17@ 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@H_502_17@ [DEBUG] org.eclipse.jetty.servlet.ServletHandler – call filter hiddenHttpMethodFilter@H_502_17@ [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) {
}
注意:请记住删除放在配置中的过滤器
我希望能帮到你