SpringMVC利用MultipartFile进行文件上传

相关文章

  • SpringMVC基础配置

Spring 配置


<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  
  <property name="defaultEncoding" value="UTF-8"/>
  
  <property name="maxUploadSize" value="31457280"/>
  
  <property name="maxInMemorySize" value="40960"/>
bean>

依赖的组件

<dependency>
  <groupId>commons-fileuploadgroupId>
  <artifactId>commons-fileuploadartifactId>
  <version>1.3.1version>
dependency>

Java源码


package com.devefx.website.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class FileUploadController {

    @RequestMapping(value="/uploadFile", method=RequestMethod.POST)
    public String uploadFile(@RequestParam MultipartFile file) {
        // 获取文件流
        file.getInputStream();
        // 获取文件名
        file.getOriginalFilename();
        // 获取文件大小
        file.getSize();
    }

}

你可能感兴趣的:(Java,杂记)