zoukankan      html  css  js  c++  java
  • 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 的参数名字一样

  • 相关阅读:
    SAP 移动类型 整理
    VB6及VS2005 相关的 树TREE控件,网格控件、电子表格控件、网络图及甘持图控件(项目进度)
    金蝶 PK 用友,第三方评论与自我评价(1)
    谁在开发“工作流”WORKFLOW 产品?
    协同及ERP开发平台,我们如何选择?
    关注“北京广联达软件公司”的项目成本管理系统 !
    一个免费提供的开发平台___"KCOM 商业工程"
    企业 ISO“质量、安全和环境” 三大体系认证的管理系统的开发者 !
    MAXWELL 万胜系统软件公司——为工程建设承包商提供优秀的软件套件!
    Contractor Anywhere (任何地方的承包商)也被 SAGE “赛捷”公司收购 !
  • 原文地址:https://www.cnblogs.com/1234AAA/p/8694604.html
Copyright © 2011-2022 走看看