springmvc 上传文件步骤:

第一 拷贝所需jar

第二 需要在springmvc(springmvc-servlet.xml)中配置:SpringMVC上传文件时,需要配置MultipartResolver处理器

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="UTF-8"/>  
        <!-- 指定所上传文件的总大小不能超过200KB。注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 -->  
       <property name="maxUploadSize" value="200000"/>
    </bean>

第三 需要在上传的表单添加enctype="multipart/form-data"
  
  <form method="post" enctype="multipart/form-data" action="upload.do">
        <input type="file"  name="file" /> <br/>
        <input type="submit" /> <br/>
    </form>

第四 单个文件上传

@RequestMapping(value = "/upload.do")
    public String upload(@RequestParam(value = "file", required = false) MultipartFile file,
            HttpServletRequest request, ModelMap model) {
        // 上传地址
        String path = request.getSession().getServletContext()
                .getRealPath("\\upload\\");
        String fileName = file.getOriginalFilename();
        File targetFile = new File(path, new Random().nextInt(100) + fileName);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
 
        try {
            file.transferTo(targetFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "index";
    }

第五 多个文件上传

    // 多文件上传
    @RequestMapping(value = "/upload1.do")
    public String fileUpload(HttpServletRequest request, HttpServletResponse response) throws Exception  {
      MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
      Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
      String ctxPath = request.getSession().getServletContext()
                .getRealPath("\\upload\\");

File file = new File(ctxPath);
       if (!file.exists()) {
            file.mkdir();
        }
        System.out.println(fileMap.size());
        String fileName = null;
        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
            // 上传文件名
            System.out.println("key: " + entity.getKey());
            MultipartFile mf = entity.getValue();
            fileName = mf.getOriginalFilename();
            File uploadFile = new File(ctxPath + fileName);
            System.out.println(ctxPath + fileName);
            FileCopyUtils.copy(mf.getBytes(), uploadFile);
        }

return "index";
    }

  注意:多文件上传的时候页面的input标签name属性的值不能相同

    <form method="post" enctype="multipart/form-data" action="upload.do">
        <input type="file"  name="file1" /> <br/>
        <input type="file"  name="file2" /> <br/>
        <input type="submit" /> <br/>
    </form>

转载于:https://blog.51cto.com/liuyj/1566100

spring mvc 上传文件相关推荐

  1. Spring MVC上传文件原理和resolveLazily说明

    问题:使用Spring MVC上传大文件,发现从页面提交,到进入后台controller,时间很长.怀疑是文件上传完成后,才进入.由于在HTTP首部自定义了"Token"字段用于权 ...

  2. Spring MVC上传文件后重命名读取不显示,报错已解决(The origin server did not find a current representation for the targe)

    上传文件后,重命名保存在本地,再读取时报错:The origin server did not find a current representation for the target resourc ...

  3. java中上传文件有哪些框架,Spring MVC系列教材 (八)- SPRING MVC 上传文件

    HTTP Status 500 – Internal Server Error Type 异常报告 消息 Servlet.init() for servlet [springmvc] threw ex ...

  4. 利用 Spring MVC 上传多文件到指定目录 spring upload files

    本篇文章,我们会教你通过eclipse构建一个创建一个Java web项目并转为maven工程,实现用spring mvc将所选文件上传到指定目录 开发环境: 1.JDK 1.7 2.Maven 3. ...

  5. java spring mvc 上传_Java Spring MVC 上传下载文件配置及controller方法详解

    下载: 1.在spring-mvc中配置(用于100M以下的文件下载) 下载文件代码 @RequestMapping("/file/{name.rp}") public Respo ...

  6. Spring Boot 上传文件(spring boot upload file)

    本篇文章将说明在Spring Boot web程序中如何上传文件. 开发环境: 1. eclipse Oxygen Release (4.7.0) 2. Spring Boot 1.4.3 RELEA ...

  7. Spring CommonsMultipartResolver 上传文件

    http://yanglei008.iteye.com/blog/246920 Spring CommonsMultipartResolver 上传文件 博客分类: spring SpringMVCJ ...

  8. Spring Boot(十七):使用Spring Boot上传文件

    Spring Boot(十七):使用Spring Boot上传文件 环境:Spring Boot最新版本1.5.9.jdk使用1.8.tomcat8.0 一.pom包配置 <parent> ...

  9. (转)Spring Boot(十七):使用 Spring Boot 上传文件

    http://www.ityouknow.com/springboot/2018/01/12/spring-boot-upload-file.html 上传文件是互联网中常常应用的场景之一,最典型的情 ...

  10. springboot上传文件同时传参数_Spring Boot 系列:使用 Spring Boot 上传文件

    上传文件是互联网中常常应用的场景之一,最典型的情况就是上传头像等,今天就带着带着大家做一个 Spring Boot 上传文件的小案例. 1.pom 包配置 我们使用 Spring Boot 版本 2. ...

最新文章

  1. React文档(十四)深入JSX
  2. 点击跳转到QQ聊天界面
  3. 【图像处理】——图像内插法
  4. 【BIM入门实战】建筑墙体知识全攻略
  5. 开源文档管理系统LogicalDOC测试报告---安装篇
  6. mac虚拟机vm屏幕一直闪烁_VM虚拟机VMware Fusion Pro 11
  7. Dediprog EM100Pro-G2 CmdLine教程
  8. mysql事物超时回滚_查询超时和事务回滚的处理办法
  9. 搜索引擎优化系统知名乐云seo_北京网络优化知名乐云seo
  10. onedrive已停止工作_4、工作簿的基本操作
  11. Kafka_producer
  12. nrf52在未配对的情况下使用白名单广播,指定安卓手机允许连接
  13. python解锁电脑屏幕_使用Python解锁计算机屏幕
  14. RNN中BPTT的推导和可能的问题
  15. Hadoop实现数据清洗ETL
  16. ZZULIOJ 1788 小金刚的宝藏 (01背包)
  17. Gearbox变速器
  18. JAVA | 入口即化的JVM
  19. FluentNHibernate之基本映射详解
  20. C3游戏引擎资源打包格式支持(APK不释放资源的问题)

热门文章

  1. python的标准库——turtle
  2. day4--numpy
  3. IDL| 数据分割读取
  4. 教务管理系统C++全部代码
  5. Android 扩展ViewFlipper做导航页(一)
  6. Flink CDC 系列 - Flink MongoDB CDC 在 XTransfer 的生产实践
  7. 插件开发之360 DroidPlugin源码分析(四)Activity预注册占坑
  8. android package.xml,文件没问题的情况下not read packageName from xxx\AndroidManifest.xml?
  9. 线程 daemon java user,Java线程_守护线程和用户线程
  10. pytorch标签onehot编码_pytorch将标签转为onehot