Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SpringBoot-EleAF多部分文件上载_Java_Spring Boot_Thymeleaf - Fatal编程技术网

Java SpringBoot-EleAF多部分文件上载

Java SpringBoot-EleAF多部分文件上载,java,spring-boot,thymeleaf,Java,Spring Boot,Thymeleaf,我正在尝试创建一个页面,允许用户选择要上载到我的SpringMVC控制器的文件 这是我的控制器: @RestController public class CustomerDataController { @RequestMapping(value = "/customerFile", method = RequestMethod.POST) public @ResponseBody String handleFileUpload(@RequestParam("myFile") Multi

我正在尝试创建一个页面,允许用户选择要上载到我的SpringMVC控制器的文件

这是我的控制器:

@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>
我的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中的一个bug已经修复,很可能就是这样)。

您正在使用
@EnableAutoConfiguration
?“消息”从何而来(看起来不像你在浏览器中看到的)?是的,我正在使用@EnableAutoConfiguration,下面是我的build.gradle:compile(“org.springframework.boot:spring boot starter web”)…compile(“org.thymeleaf:thymeleaf-spring4”)。我更新了浏览器中显示的错误消息。您是从main()方法运行还是在已部署的WAR中运行?你能分享整个项目吗?我是从一个主要的运行,但不幸的是,该项目太大,无法分享。我将用我的主要方法更新这个问题。你的依赖关系仍然有点混乱。当然,您应该尝试使用Spring启动快照(和rc5)。
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
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