我正在尝试创建一个页面,允许用户选择要上传到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:&#39; java&#39;
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
答案 0 :(得分:2)
解决方案是更新Spring Boot(多部分autoconfig中的一个错误已得到修复,可能就是这样)。