Spring-boot Thymeleaf Multipart file upload -
i trying create page allows user select file uploaded springmvc controller.
here controller:
@restcontroller public class customerdatacontroller { @requestmapping(value = "/customerfile", method = requestmethod.post) public @responsebody string handlefileupload(@requestparam("myfile") multipartfile file) { if ( !file.isempty() ) { string name = file.getname(); try { byte[] bytes = file.getbytes(); bufferedoutputstream stream = new bufferedoutputstream( new fileoutputstream( new file( name + "-uploaded" ) ) ); stream.write( bytes ); stream.close(); return "you uploaded " + name + " " + name + "-uploaded !"; catch ( exception e ) { return "you failed upload " + name + " => " + e.getmessage(); } } else { return "the selected file empty , not uploaded."; } }
and upload.html form has:
<form action="upload" th:action="@{/customerfile}" method="post" enctype="multipart/form-data"> <input type="file" name="myfile" /> <input type="submit" /> </form>
i have tried using standard (non thymeleaf form):
<form method="post" action="/customerfile" enctype="multipart/form-data"> <input type="file" name="file"/> <input type="submit"/> </form>
not sure if it's relevant have following configuration:
@override public void addviewcontrollers(viewcontrollerregistry registry) { ... registry.addviewcontroller( "/upload" ).setviewname( "upload" ); } @bean multipartconfigelement multipartconfigelement() { multipartconfigfactory factory = new multipartconfigfactory(); factory.setmaxfilesize("512kb"); factory.setmaxrequestsize("512kb"); return factory.createmultipartconfig(); }
i have following in build.gradle: apply plugin: 'java'
apply plugin: 'groovy' apply plugin: 'idea' apply plugin: 'spring-boot' apply plugin: 'jacoco' apply plugin: 'war' repositories { mavencentral() maven { url "http://repo.spring.io/libs-snapshot" } } dependencies { compile("org.springframework.boot:spring-boot-starter-web") compile("org.springframework.boot:spring-boot-starter-data-jpa:1.0.0.rc4") compile("org.springframework:spring-orm:4.0.0.rc1") compile("org.hibernate:hibernate-entitymanager:4.2.1.final") compile("com.h2database:h2:1.3.172") compile("joda-time:joda-time:2.3") compile("org.thymeleaf:thymeleaf-spring4") compile("org.codehaus.groovy.modules.http-builder:http-builder:0.7.1") compile('org.codehaus.groovy:groovy-all:2.2.1') compile('org.jadira.usertype:usertype.jodatime:2.0.1') testcompile('org.spockframework:spock-core:0.7-groovy-2.0') { exclude group: 'org.codehaus.groovy', module: 'groovy-all' } testcompile('org.codehaus.groovy.modules.http-builder:http-builder:0.7+') testcompile("junit:junit") }
i running embedded tomcat, launched via:
public static void main(string[] args) { applicationcontext ofac = springapplication.run( ofac.class, args ); }
when click submit button, don't see request in controller following in browser:
http status 400 - required multipartfile parameter 'myfile' not present type status report message required multipartfile parameter 'myfile' not present description request sent client syntactically incorrect. apache tomcat/7.0.52
here firebug tells me request:
connection close content-language en content-length 1080 content-type text/html;charset=utf-8 date mon, 24 mar 2014 17:09:55 gmt server apache-coyote/1.1 request headersview source accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 accept-encoding gzip, deflate accept-language en-us,en;q=0.5 connection keep-alive cookie jsessionid=86768954cd2877a7d78535e26cffb8da dnt 1 host localhost:9001 referer http://localhost:9001/upload
resolution update spring boot (a bug in multipart autoconfig had been fixed it).
Comments
Post a Comment