我试图创建一个页面,允许用户选择要上传到我的SpringMVC 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."; } }
我的upload.html表单具有:
<form action="upload" th:action="@{/customerFile}" method="post" enctype="multipart/form-data"> <input type="file" name="myFile" /> <input type="submit" /> </form>
我也尝试过使用标准格式(非Thymeleaf格式):
<form method="post" action="/customerFile" enctype="multipart/form-data"> <input type="file" name="file"/> <input type="submit"/> </form>
不知道它是否相关,但是我有以下配置:
@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(); }
我的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") }
我正在运行嵌入式Tomcat,通过以下方式启动:
public static void main(String[] args) { ApplicationContext ofac = SpringApplication.run( OFAC.class, args ); }
单击提交按钮时,在控制器中没有看到请求,但在浏览器中显示以下内容:
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
这是Firebug告诉我有关请求的内容:
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
解决方法是更新Spring Boot(修复了multipart autoconfig中的一个错误,所以很可能就是这样)。