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 的参数名字一样

  • 相关阅读:
    XAMPP Error: Apache shutdown unexpectedly. 解决思路
    [转]《我眼中的技术高手》——邯郸学步、创作与创新
    svn 提交错误 400 Bad Reqest MKACTIVITY 请求于XX失败 Conflict Unable to connect to a repository at URL
    try catch 怎么写?
    正则词典
    用B表更新A表
    3种PHP连接MYSQL数据库的常用方法
    PHP数据库连接失败--could not find driver 解决办法
    Php.ini 文件位置在哪里,怎么找到 php.ini
    检测到在集成的托管管道模式下不适用的ASP.NET设置的解决方法(非简单设置为【经典】模式)。
  • 原文地址:https://www.cnblogs.com/1234AAA/p/8694604.html
Copyright © 2011-2022 走看看