Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring-boot Thymeleaf Multipart file upload

I am trying to create a page that allows a user to select a file to be uploaded to my SpringMVC Controller.

Here is my 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 successfully uploaded " + name + " into " + name + "-uploaded !";
           catch ( Exception e ) {
                return "You failed to upload " + name + " => " + e.getMessage();
           }
      } else {
           return "The selected file was empty and could not be uploaded.";
      }
  }

And my 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 also tried using a 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 but I have the 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 the following in my 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 am running embedded Tomcat, launched via:

public static void main(String[] args) {
        ApplicationContext ofac = SpringApplication.run( OFAC.class, args );
}

When I click the submit button, I don't see a request in my controller but I get a the following in my browser:

HTTP Status 400 - Required MultipartFile parameter 'myFile' is not present

type Status report

message Required MultipartFile parameter 'myFile' is not present

description The request sent by the client was syntactically incorrect.
Apache Tomcat/7.0.52

Here is what Firebug tells me about the 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
like image 427
sonoerin Avatar asked Feb 10 '26 17:02

sonoerin


1 Answers

Resolution was to update Spring Boot (a bug in the multipart autoconfig had been fixed so that was probably it).

like image 193
Dave Syer Avatar answered Feb 12 '26 06:02

Dave Syer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!