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

所属分类: 软件编程 / java 阅读数: 48
收藏 0 赞 0 分享

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://zhanzhang360.qulang.net/imgupload/000233/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 实现多文件批量上,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

更多精彩内容其他人还在看

spring boot 静态资源处理方法

本篇文章主要介绍了spring boot 静态资源处理方法。小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解spring boot 使用application.properties 进行外部配置

这篇文章主要介绍了详解spring boot 使用application.properties 进行外部配置,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Java实现分页的前台页面和后台代码

这篇文章主要为大家详细介绍了Java实现分页的前台页面和后台代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

详解spring面向切面aop拦截器

spring中有很多概念和名词,比如过滤器、拦截器、aop等。这篇文章主要介绍了详解spring面向切面aop拦截器,有兴趣的可以了解一下。
收藏 0 赞 0 分享

Java easyui树形表格TreeGrid的实现代码

这篇文章主要为大家详细介绍了Java easyui树形表格TreeGrid的实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Java的NIO与IO的详解及对比

这篇文章主要介绍了Java的NIO与IO的详解及对比的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Java中网络IO的实现方式(BIO、NIO、AIO)介绍

这篇文章主要介绍了Java中网络IO的实现方式(BIO、NIO、AIO)介绍的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Java 敏感信息加密处理

本文主要介绍了Java 敏感信息加密处理的相关知识:1)敏感信息加密处理我们要实现什么;2)敏感信息加密处理我做了些什么;3)敏感信息加密实现方法。具有很好的参考价值,下面跟着小编一起来看下吧
收藏 0 赞 0 分享

详解使用Spring3 实现用户登录以及权限认证

这篇文章主要介绍了详解使用Spring3 实现用户登录以及权限认证,这里整理了详细的代码,有需要的小伙伴可以参考下。
收藏 0 赞 0 分享

Java的内存机制详解

本文主要介绍了Java的内存机制的相关知识,具有很好的参考价值,下面跟着小编一起来看下吧
收藏 0 赞 0 分享
查看更多