SpringMvc+Angularjs 实现多文件批量上传

 更新时间:2017年03月23日 11:03:10   作者:y0yO011  
本文通过实例代码给大家讲解了SpringMvc+Angularjs 实现多文件批量上传功能,非常不错,具有参考借鉴价值,需要的朋友一起学习吧

SpringMvc代码

jar包

commons-fileupload

commons-io

spring-mvc.xml配置

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  <property name="defaultEncoding" value="UTF-8" />
</bean>

Controller

@RequestMapping(value = "api/v1/upload", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Map upload (@RequestParam(value = "files") MultipartFile [] files,
                 @RequestParam(value = "id") String id,
                 HttpServletRequest request, HttpServletResponse response) {
  Map res = new HashMap();
  try {
    log.info("upload>>>>>id:{}", id);
    if (files!=null) {
      for (MultipartFile file:files) {
        log.info("filename:{}", file.getOriginalFilename());
      }
    }
  } catch (Exception e) {
    log.error("upload>>>>异常:{}", e.toString());
  }
  log.info("upload>>>>返回结果:{}", res);
  return res;
}

保存到本地

// copy File
 public boolean copyFile (MultipartFile tempFile, String filePath) {
   Boolean res = false;
   try {
     File file = new File(filePath);
     if (!file.getParentFile().exists()) {
       file.getParentFile().mkdirs();
     }
     // 将文件拷贝到当前目录下
     tempFile.transferTo(file);
     res = true;
   } catch (Exception e) {
     log.info("copyFile>>>>异常:{}", e.toString());
   }
   return res;
 }

AngularJs代码

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <script src="https://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="uploadCtrl">
  <p><input type="file" multiple="multiple" name="files"></p>
  <p><input type="text" name="id" ng-model="id"></p>
  <p><input type="button" value="提交" ng-click="submit()"></p>
</div>
<script>
  var app = angular.module('myApp', []);
  app.controller('uploadCtrl', ["$scope", "$http", function($scope, $http) {
    $scope.submit = function () {
      var fd = new FormData();
      var files = document.querySelector('input[name="files"]').files;
      for (var i=0; i<files.length; i++) {
        fd.append("files", files[i]);
      }
      fd.append("id", $scope.id);
      $http({
        method:'POST',
        url  : '/Project/api/v1/upload',
        data: fd,
        headers: {'Content-Type':undefined},
        transformRequest: angular.identity
      }).success(function (response) {
        console.log(response.data);
      }).error(function () {
      });
    }
  }]);
</script>
</body>
</html>

Form表单提交

<form action="/Project/api/v1/upload" method="POST" enctype="multipart/form-data">
  <p><input type="text" name="id" /></p>
  <p><input type="file" multiple="multiple" id="files" name="files" /></p>
  <p><input type="submit" value="Submit" /></p>
</form>

以上所述是小编给大家介绍的SpringMvc+Angularjs 实现多文件批量上,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

相关文章

  • 学习在一台新电脑上配置JAVA开发环境

    学习在一台新电脑上配置JAVA开发环境

    本文主要介绍了如何在一台新电脑上配置JAVA开发环境,每一个步骤都有对应的截图和文字说明,需要的朋友可以参考下
    2015-07-07
  • SpringBoot自定义注解使用读写分离Mysql数据库的实例教程

    SpringBoot自定义注解使用读写分离Mysql数据库的实例教程

    这篇文章主要给大家介绍了关于SpringBoot自定义注解使用读写分离Mysql数据库的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11
  • 图文详解mybatis+postgresql平台搭建步骤

    图文详解mybatis+postgresql平台搭建步骤

    从头开始搭建一个mybatis+postgresql平台,这篇文章主要介绍了图文详解mybatis+postgresql平台搭建步骤,感兴趣的小伙伴们可以参考一下
    2016-07-07
  • Java实现批量修改txt文件名称的方法示例

    Java实现批量修改txt文件名称的方法示例

    这篇文章主要介绍了Java实现批量修改txt文件名称的方法,结合实例形式分析了Java针对目录文件遍历及文件读写、属性操作等相关实现技巧,需要的朋友可以参考下
    2019-03-03
  • SpringBoot配置ShedLock分布式定时任务

    SpringBoot配置ShedLock分布式定时任务

    ShedLock是一个在分布式环境中使用的定时任务框架,这篇文章主要介绍了SpringBoot配置ShedLock分布式定时任务,需要的朋友们下面随着小编来一起学习学习吧
    2021-05-05
  • Mybatis Trim标签用法简单介绍

    Mybatis Trim标签用法简单介绍

    这篇文章主要介绍了Mybatis Trim标签用法,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2017-05-05
  • SpringBoot监控模块Actuator的用法详解

    SpringBoot监控模块Actuator的用法详解

    Spring Boot Actuator 是 Spring Boot 自带的一个功能模块,提供了一组已经开箱即用的生产环境下常用的特性和服务,比如应用程序的健康检查、信息暴露、度量收集、日志记录等,本文将给大家详细SpringBoot监控模块Actuator的用法
    2023-06-06
  • 手把手教你使用IDEA创建多模块(maven)项目

    手把手教你使用IDEA创建多模块(maven)项目

    这篇文章主要给大家介绍了关于如何使用IDEA创建多模块(maven)项目的相关资料,文中通过图文以及实例代码介绍的非常详细,需要的朋友可以参考下
    2023-07-07
  • JAVA时间存储类Period和Duration使用详解

    JAVA时间存储类Period和Duration使用详解

    这篇文章主要为大家介绍了JAVA时间存储类Period和Duration使用详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-09-09
  • eclipse老是自动跳到console解决办法

    eclipse老是自动跳到console解决办法

    eclipse启动服务后,想看一些properties信息或者别的,但老是自动跳转到console页面,本文给大家介绍了解决办法,对大家的学习或工作有一定的帮助,需要的朋友可以参考下
    2024-03-03

最新评论