First of all, when using Spring, make sure that you have proper MultiPartFile
resolver defined in your servlet context:
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="52428800"/>
<property name="maxInMemorySize" value="52428800"/>
</bean>
If you're using maven, this resolver is located in spring-web artifact:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${your.spring.version}</version>
</dependency>
Then, create form and make sure you're using proper enctype
:
<form method="post" action="upload.form" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit"/>
</form>
Finally, handle file upload in your controller
@RequestMapping(value="/path", method= RequestMethod.POST)
public StringscaleImage(@RequestParam("file") MultipartFile file) {
//....
}
Remember that asynch file upload is supported only with HTML5, with others you'd need to use some workarounds (like flash or iframes).
If you're still facing 400
Error, add to your logging service this logger (or similar, depending on logging framework):
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out"/>
<param name="threshold" value="TRACE"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{HH:mm:ss,SSS} %-5p [%c] %m%n"/>
</layout>
</appender>
<logger name="org.springframework.web.method.HandlerMethod">
<level value="TRACE"/>
</logger>
<root>
<priority value="info"/>
<appender-ref ref="console"/>
</root>
It should output exception thrown during request handling