zoukankan      html  css  js  c++  java
  • SpringMVC文件上传

    单文件上传

    第①步:引入jar包

    <!--文件上传的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>
    

      

    第②步:创建form表单,method必须为POST,并加入属性enctype="multipart/form-data"

    <%--单文件上传--%>
    <form action="/first" method="post" enctype="multipart/form-data">
        请选择上传文件:<input type="file" name="upload"/><br/>
        <input type="submit"/>
    </form>
    

      

    第③步:创建处理器

    @Controller
    public class FirstController {
    
    
        /**
         * upload   文件对象
         */
        @RequestMapping("/first")
        public String doFirst(MultipartFile upload, HttpSession session){
            //判断是否为空文件
            if(upload.getSize()>0){
                //获取文件名
                String filename = upload.getOriginalFilename();
                //指定上传文件格式
                if (filename.endsWith("jpg")||filename.endsWith("JPG")){
                    //获取全路径
                    String realPath = session.getServletContext().getRealPath("/upload");
                    //拼接全路径
                    File file=new File(realPath,filename);
                    //上传文件
                    try {
                        upload.transferTo(file);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
          //返回页面 return "index"; } }

      

    第④步:配置xml文件

     <!--:参数方法名解析器-->
       <bean id="methodNameResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
           <property name="prefix" value="/"/>
           <property name="suffix" value=".jsp"/>
       </bean>
        <!--扫描包下所有的被标注的类-->
        <context:component-scan base-package="day16Fileupload"/>
    
        <!--id必须为multipartResolver   它是DispatchServlet的常量值-->
        <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            <!--表单上传文件总大小,字符为单位-->
            <property name="maxUploadSize" value="20971520"/>
            <!--设置编码格式-->
            <property name="defaultEncoding" value="utf-8"/>
        </bean>
    
        <!--配置注解驱动-->
        <mvc:annotation-driven/>
    

      

    多文件上传

    和单文件上传差不多

    <form action="/second" method="post" enctype="multipart/form-data">
        请选择上传文件1:<input type="file" name="upload"/><br/>
        请选择上传文件2:<input type="file" name="upload"/><br/>
        请选择上传文件3:<input type="file" name="upload"/><br/>
        <input type="submit"/>
    </form>
    

      

    处理器方法

    /*多文件上传,使用MultipartFile数组接收*/
        @RequestMapping("/second")
        public String doSecond(@RequestParam MultipartFile[] upload, HttpSession session){
            //循环上传
            for (MultipartFile item:upload){
                //获取文件名
                String filename = item.getOriginalFilename();
                //获取全路径
                String realPath = session.getServletContext().getRealPath("/upload");
                //拼接全路径
                File file=new File(realPath,filename);
                //上传文件
                try {
                    item.transferTo(file);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            //返回视图
            return "index";
        }
  • 相关阅读:
    Scrum Meeting 6 -2014.11.12
    Scrum Meeting 5 -2014.11.11
    Bing词典vs有道词典比对测试报告——体验篇之成长性及用户控制权
    团队项目的用户需求及反馈
    Scrum Meeting 4 -2014.11.8
    Scrum Meeting 3 -2014.11.5
    bing词典vs有道词典对比测试报告——功能篇之细节与用户体验
    Bing词典vs有道词典比对测试报告——功能篇之辅助功能,差异化功能及软件的效能
    Bing词典vs有道词典比对测试报告
    hdu 5087 次长升序串的长度
  • 原文地址:https://www.cnblogs.com/xuchangqi1/p/8693553.html
Copyright © 2011-2022 走看看