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

作者:y0yO011 时间:2023-12-08 23:25:24 

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 实现多文件批量上网站的支持!

来源:http://blog.csdn.net/u013836363/article/details/65437141

标签:spring,mvc,angularjs
0
投稿

猜你喜欢

  • 基于spring boot 的配置参考大全(推荐)

    2021-11-28 23:32:41
  • SpringCloud @RefreshScope刷新机制浅析

    2022-12-13 11:38:15
  • Java简单高效实现分页功能

    2022-05-24 13:56:02
  • Centos中安装jdk案例讲解

    2023-04-30 00:37:50
  • java反射技术与类使用示例

    2021-09-14 06:34:34
  • Java如何实现压缩文件与解压缩zip文件

    2022-01-28 09:14:00
  • Mybatis如何传入多个参数的实现代码

    2022-02-26 02:52:24
  • android ToolBar的简单使用

    2023-03-05 10:44:33
  • 关于Future机制原理及解析

    2022-01-23 03:57:48
  • Android仿QQ、微信聊天界面长按提示框效果

    2023-06-11 14:04:32
  • java多线程中执行多个程序的实例分析

    2023-03-11 18:21:58
  • Unity实现换装系统

    2021-08-11 15:27:15
  • 详解Java实现单例的五种方式

    2021-06-11 03:54:03
  • Intellij Idea中进行Mybatis逆向工程的实现

    2021-06-05 00:08:53
  • 基于Java实现Json文件转换为Excel文件

    2022-08-04 23:53:15
  • Java中一个for语句导致无穷大死循环的例子

    2022-12-17 08:13:33
  • 关于Springboot的日志配置

    2022-12-16 10:32:33
  • winform绑定快捷键的方法

    2023-12-10 22:16:04
  • JDK14性能管理工具之jstack使用介绍

    2022-10-11 18:48:01
  • springboot更新配置Swagger3的一些小技巧

    2023-08-28 06:31:43
  • asp之家 软件编程 m.aspxhome.com