一、单文件上传

  1.导入依赖

<dependency>
          <groupId>commons-io</groupId>
          <artifactId>commons-io</artifactId>
          <version>2.4</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
        <dependency>
          <groupId>commons-fileupload</groupId>
          <artifactId>commons-fileupload</artifactId>
          <version>1.3.1</version>
        </dependency>

  2.创建文件上传表单

<form action="/file/fileUpload" method="post" enctype="multipart/form-data">
            <input type="file" name="file"/>
            作者: <input type="text" name="author"/>
            <input type="submit" value="提交"/>
        </form>

  3.构建文件上传控制器

@RequestMapping("/fileUpload")
        /**
         * MultipartFile 选择的文件
         */
        public String fileupload(HttpSession session,MultipartFile file,String author) throws IOException {
            System.out.println("作者:"+author);
            System.out.println(file);
            /**
             * 如何处理文件
             */
            if(!file.isEmpty()){
                //获取文件名称
                String fileName=file..getOriginalFilename();
                //获取到需要上传的路径
                String realPath = session.getServletContext().getRealPath("/WEB-INF/upload");
                //创建文件对象
                File uploadfile=new File(realPath+"\\"+fileName);
                //如何上传文件
                file.transferTo(uploadfile);
            }
            return "index";
        }

  4.注册文件上传解析器

<!--文件上传解析器-->
        <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            <!--编码-->
            <property name="defaultEncoding" value="UTF-8"/>
            <property name="maxUploadSize" value="500000"/>
        </bean>

二、多文件上传

  文件上传表单:

<form action="/file/fileUploads" method="post" enctype="multipart/form-data">
        <input type="file" name="uploadFiles"/>
        <input type="file" name="uploadFiles"/>
        <input type="file" name="uploadFiles"/>
        作者: <input type="text" name="author"/>
        <input type="submit" value="提交"/>
    </form>

  控制器

@RequestMapping("/fileUploads")
    /**
     * MultipartFile 选择的文件
     */
    public String fileUploads(HttpSession session,MultipartFile[] uploadFiles,String author) throws IOException {
        System.out.println("作者:"+author);
        System.out.println(uploadFiles);
        /**
         * 如何处理文件
         */
        for (MultipartFile file:uploadFiles){
            if(!file.isEmpty()){
                //获取文件名称
                String fileName=file.getOriginalFilename();
                //获取到需要上传的路径
                String realPath = session.getServletContext().getRealPath("/WEB-INF/upload");
                //创建文件对象
                File uploadfile=new File(realPath+"\\"+fileName);
                //如何上传文件
                file.transferTo(uploadfile);
            }
        }
        return "index";
    }

  其余一致

三、文件下载

  后期补

最新文章

  1. 【源码】Word转PDF V1.0.1 小软件,供新手参考
  2. SQL Server编程(04)基本语法【转载】
  3. js的变量使用&lt;bean&gt;&lt;list:write&gt;赋值时需加&#39; &#39;
  4. javascript与服务器3
  5. WIN7下使用VC2010调试uCOS-II 2.91
  6. LeetCode-Repeated DNA Sequences (位图算法减少内存)
  7. Codeforces Round #325 (Div. 1) D. Lizard Era: Beginning
  8. u盘安装centos
  9. iOS获取手机相关信息
  10. hdu 3948(后缀数组+RMQ)
  11. stdio.h及cstdio的区别
  12. 待修改 nyoj 412 又是一个遗留问题
  13. [问题解决] ubuntu server12.04 按ctrl+alt+F1没用
  14. 本地环境下 WordPress 环境搭建与安装
  15. hibernate ——联合主键
  16. python!!!!惊了,这世上居然还有这么神奇的东西存在
  17. 全文检索-Elasticsearch (四) elasticsearch.net 客户端
  18. October 16th 2017 Week 42nd Monday
  19. spring Boot使用AOP统一处理Web请求日志记录
  20. jquery cdn加速注意事项

热门文章

  1. LeetCode 61——旋转链表(JAVA)
  2. Java多线程(十):BlockingQueue实现生产者消费者模型
  3. 按Excel的模板导出数据
  4. WPF DataGrid数据绑定
  5. c#如何使用MemoryStream和BinaryFormatter进行对象的序列化和返序列化
  6. js 扁平化输出数组
  7. 【python+beautifulsoup4】Python中安装bs4后,pycharm报错ModuleNotFoundError: No module named &#39;bs4&#39;
  8. Async 配置线程池
  9. es string 分词完整示例
  10. Django_06_项目完成