【问题标题】:Spring-boot Thymeleaf Multipart file uploadSpring-boot Thymeleaf 多部分文件上传
【发布时间】:2014-05-01 04:39:05
【问题描述】:

我正在尝试创建一个页面,允许用户选择要上传到我的 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>

我也尝试过使用标准(非 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

【问题讨论】:

  • 您正在使用@EnableAutoConfiguration? “消息”来自哪里(看起来不像您在浏览器中看到的那样)?
  • 是的,我正在使用@EnableAutoConfiguration,并且在我的 build.gradle 中有以下内容: compile("org.springframework.boot:spring-boot-starter-web")..compile("org. thymeleaf:thymeleaf-spring4")。我更新了浏览器中显示的错误消息。
  • 您是从 main() 方法运行还是在已部署的 WAR 中运行?你能分享整个项目吗?
  • 我是从一个 main 运行的,但不幸的是该项目太大而无法共享。我将用我的主要方法更新问题。
  • 你的依赖还是有点乱。当然,您应该尝试使用 Spring Boot 快照(和 rc5)。

标签: java spring-boot thymeleaf


【解决方案1】:

解决方案是更新 Spring Boot(多部分自动配置中的一个错误已修复,因此可能就是这样)。

【讨论】:

    猜你喜欢
    • 2014-10-31
    • 1970-01-01
    • 2022-10-22
    • 1970-01-01
    • 2022-06-14
    • 2019-11-10
    • 2021-05-29
    • 2019-07-24
    • 2017-11-05
    相关资源
    最近更新 更多