Ich versuche, mehrere Datei-Upload-Funktionalität in meine Angular Web-Anwendung mit angular-file-upload zu integrieren. Momentan funktioniert die Frontend-Funktionalität, aber jeder Upload-Versuch löst eine Ausnahme aus.Spring-Datei-Upload - Getting Expected MultipartHttpServletRequest: Ist ein MultipartResolver konfiguriert? Fehler
Der Upload-Controller ist definiert als
@Controller
@PropertySource("classpath:application.properties")
public class FileUploadController {
@Resource
private Environment env;
@RequestMapping(value = "/fileupload", method = RequestMethod.POST)
@ResponseBody
public List<String> fileUpload(@RequestParam("file") MultipartFile[] uploadFiles) throws IllegalStateException, IOException {
//file processing logic
}
}
In meiner AppConfig.java
Klasse erkläre ich die Bohne
@Bean
public CommonsMultipartResolver commonsMultipartResolver(){
CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
commonsMultipartResolver.setDefaultEncoding("utf-8");
commonsMultipartResolver.setMaxUploadSize(50000000);
return commonsMultipartResolver;
}
und die Web-Anwendung starten mit
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(AppConfig.class);
servletContext.addListener(new ContextLoaderListener(ctx));
ctx.setServletContext(servletContext);
ctx.refresh();
Dynamic servlet = servletContext.addServlet(SERVLET_NAME, new DispatcherServlet(ctx));
servlet.addMapping("/");
servlet.setLoadOnStartup(1);
//servlet.setMultipartConfig(ctx.getBean(MultipartConfigElement.class));
}
Ich glaube nicht, Es ist aufgrund des Fehlens eines <form>
Elements in meinem Angula r Ansicht, weil ich sehe, dass Content-Tyle
multipart/form-data
ist und dass die Request Payload entsprechend eingestellt ist.
Remote Address:192.168.33.10:80
Request URL:http://dev.jason.com/rest/fileupload
Request Method:POST
Status Code:500 Internal Server Error
Request Headers
Accept:*/*
Accept-Encoding:gzip,deflate
Accept-Language:en-US,en;q=0.8
Cache-Control:no-cache
Connection:keep-alive
Content-Length:415235
Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryBHlsldPQysTVpvwZ
Host:dev.jason.com
Origin:http://dev.jason.com
Pragma:no-cache
Referer:http://dev.jason.com/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36
Request Payload
------WebKitFormBoundaryBHlsldPQysTVpvwZ
Content-Disposition: form-data; name="file"; filename="IMG_5072.jpg"
Content-Type: image/jpeg
------WebKitFormBoundaryBHlsldPQysTVpvwZ--
Beachten Sie, dass dieses Problem auftritt, wenn
@Bean
public MultipartConfigElement multipartConfigElement(){
MultipartConfigFactory multipartConfigFactory = new MultipartConfigFactory();
multipartConfigFactory.setMaxFileSize("10MB");
multipartConfigFactory.setMaxRequestSize("50MB");
return multipartConfigFactory.createMultipartConfig();
}
in AppConfig.java
einschließlich und uncommenting den Befehl
servlet.setMultipartConfig(ctx.getBean(MultipartConfigElement.class));
in meinem Servlet initializer Klasse.
Jede Hilfe wird geschätzt!
Zum einen nicht registrieren die 'ctx' Zusammenhang sowohl mit dem' ContextLoaderListener' und der 'DispatcherServlet'. –
Was ist der Grund dafür? – Jason
http://stackoverflow.com/questions/11708967/what-is-the-difference-between-applicationcontext-and-webapplicationcontext-in- –