SpingMVC文件上传

实现文件上传的步骤(form表单实现文件上传)

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

Servlet3.0规范已经提供方法来处理文件上传,但这种上传需要在Servlet中完成。而Spring MVC则提供了更简单的封装。

1.引入依赖 commons

        <!--文件上传的jar包-->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.4</version>
        </dependency>

2.书写控制器方法(单文件上传和多文件上传)

单文件上传

/**
 *文件上传控制器 单文件上传
 */
@Controller
public class FileController  {
    @RequestMapping("/firstone")
    public String doFirst(MultipartFile multipartFile, HttpSession session){
        //获取客户端上传的浏览器对象 .jpg .avi .txt
        String originalFilename = multipartFile.getOriginalFilename();
        //左半部分路径
        String realPath = session.getServletContext().getRealPath("/upload");
        //将左半部分路径和浏览器对象拼接起来
        File file=new File(realPath,originalFilename);
        try {
            multipartFile.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
            return "upload";
        }
        return "suecssful";
    }
}

多文件上传

@Controller
public class FileMutliController {
    @RequestMapping("/first")
    public String doFirst(@RequestParam MultipartFile[] multipartFile, HttpSession session){
        for (MultipartFile item:multipartFile) {
            if(item.getSize()>0){
                //获取客户端上传的浏览器对象 .jpg .avi .txt
                String originalFilename = item.getOriginalFilename();
                //左半部分路径
                String realPath = session.getServletContext().getRealPath("/upload");
                //将左半部分路径和浏览器对象拼接起来
                File file=new File(realPath,originalFilename);
                try {
                   item.transferTo(file);
                } catch (IOException e) {
                    e.printStackTrace();
                    return "upload";
                }
            }
        }
        return "suecssful";
    }
}

3.applicationContextFile.xml配置

        <!--包扫描器-->
<context:component-scan base-package="day16"></context:component-scan>
        <!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
        <!--文件上传  id必须为multipartResolver  不是会报错-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"/>
</bean>
        <!--注解驱动-->
<mvc:annotation-driven></mvc:annotation-driven>

4.前台页面

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form action="/first" method="post" enctype="multipart/form-data">
   文件1: <input type="file" name="multipartFile"><br>
    文件2: <input type="file" name="multipartFile"><br>
    文件3: <input type="file" name="multipartFile"><br>
    <input type="submit" value="提交">
</form>
</body>
</html>

这里的name属性值必须和后台控制器 MultipartFile multipartFile 的参数名字一样

原文地址:https://www.cnblogs.com/1234AAA/p/8694604.html