SpringMVC——文件上传

一、SpringMVC文件上传说明

文件上传是项目开发中常用的功能。为了能上传文件,必须将表单的method设置为POST,并将enctype设置为multipart/form-data。只有在这种情况下,浏览器才会把用户选择的文件二进制数据发送给服务器。

Spring MVC为文件上传提供了直接的支持,这种支持是用即插即用的MultipartResolver实现的。Spring MVC使用Apache Commons fileupload技术实现了一个MultipartResolver实现类CommonsMultipartResolver.所以Spring MVC的文件上传依赖Apache Commons fileupload的组件

二、示例

1、注入MultipartResolver

pom.xml中导入commons-fileupload依赖

        <!--commons-io可以不用自己导入,fileupload已经依赖了io-->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.3</version>
        </dependency>

在springMVC配置文件中注入CommonsMultipartResolver

    <!--文件上传配置-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!--上传文件大小限制,单位字节(10MB)-->
        <property name="maxUploadSize" value="10485760"></property>
        <!--请求的编码格式,必须和jsp的pageEncoding属性一致,以便正确读取表单内容,默认为ISO-8859-1-->
        <property name="defaultEncoding" value="UTF-8"></property>
    </bean>

2、上传页面的简单编写

负责上传文件的表单的编码类型必须是"multipart/form-data"

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>文件上传下载测试</title>
</head>
<body>
    <h2>文件上传下载测试</h2>
    <form action="upload" enctype="multipart/form-data" method="post">
        <table>
            <tr>
                <td>文件描述:</td>
                <td><input type="text" name="description"></td>
            </tr>
            <tr>
                <td>请选择文件:</td>
                <td><input type="file" name="file"></td>
            </tr>
            <tr>
                <td><input type="submit" value="上传"></td>
            </tr>
        </table>
    </form>
</body>
</html>

3、controller的编写

springmvc中由MultipartFile接口来接收上传的文件

Spring MVC会将上传文件绑定到MultipartFile对象中,该对象提供了获取上传文件内容,文件名等方法。通过transferTo()方法还可以将文件存储到硬件中。

MultipartFile常用方法如下:

*bye[] getBytes()         :获取文件数据

*String getContentType()      :获取文件MIME类型

*InputStream getIputStream()    :获取文件流

*String getName()         :获取表单中文件组件的名字

*String getOriginalFilename()    :获取上传文件的原名

*boolean isEmpty()          :是否是空文件

*void transferTo(File dest)    :将文件保存到一个目标文件中

@Controller
public class FileController {
    @RequestMapping("upload")
    public String upload(HttpServletRequest request, @RequestParam("description") String description,
                         @RequestParam("file") MultipartFile file) throws IOException {
        System.out.println(description);
        //上传文件不为空
        if (!file.isEmpty()){
            //上传文件路径
            String path = request.getServletContext().getRealPath("/static/images");
            //上传文件名
            String filename = file.getOriginalFilename();
            File filepath = new File(path, filename);
            //判断路径是否存在,如果不存在就创建一个
            if (!filepath.getParentFile().exists()){
                filepath.getParentFile().mkdirs();
            }
            //将文件保存
            file.transferTo(new File(path+File.separator+filename));
            System.out.println("上传文件路径"+path+File.separator+filename);
            return "fileUpSuccess";
        }
        else {
            return "error";
        }
    }
}

4、测试

注意request.getServletContext().getRealPath("");获取的是target下的路径

 

原文地址:https://www.cnblogs.com/lyh233/p/14006207.html