激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - SpringMvc+Angularjs 實現(xiàn)多文件批量上傳

SpringMvc+Angularjs 實現(xiàn)多文件批量上傳

2020-09-01 09:38y0yO011 Java教程

本文通過實例代碼給大家講解了SpringMvc+Angularjs 實現(xiàn)多文件批量上傳功能,非常不錯,具有參考借鑒價值,需要的朋友一起學(xué)習(xí)吧

SpringMvc代碼

jar包

commons-fileupload

commons-io

spring-mvc.xml配置

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

Controller

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@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>>>>返回結(jié)果:{}", res);
  return res;
}

保存到本地

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 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();
     }
     // 將文件拷貝到當(dāng)前目錄下
     tempFile.transferTo(file);
     res = true;
   } catch (Exception e) {
     log.info("copyFile>>>>異常:{}", e.toString());
   }
   return res;
 }

AngularJs代碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<!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表單提交

?
1
2
3
4
5
<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 實現(xiàn)多文件批量上,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對服務(wù)器之家網(wǎng)站的支持!

原文鏈接:http://blog.csdn.net/u013836363/article/details/65437141

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 在线天堂中文在线资源网 | 色诱亚洲精品久久久久久 | 国产精品久久久久久久久久大牛 | 黄色高清av | h视频在线播放 | 欧美韩国一区 | 成人午夜影院 | 青草视频在线观看视频 | 成年人网站国产 | 国产外围在线 | 免费黄色在线观看网站 | 777zyz色资源站在线观看 | 久久久久久久久久久高潮一区二区 | v11av在线视频成人 | 国产一国产一级毛片视频在线 | 欧美精品久久久久久久久老牛影院 | 91青青| 麻豆视频网 | 国产免费让你躁在线视频 | 成人免费毛片一 | 日本黄色一级毛片 | 成人福利视频在线 | 成人做爰高潮片免费视频韩国 | 成人黄色小视频网站 | 欧美日韩亚洲成人 | 毛片在线免费观看完整版 | 国产寡妇xxxxxxxx性开放 | 亚洲性综合网 | h视频免费在线 | 亚洲人成在线播放 | 密室逃脱第一季免费观看完整在线 | 欧美14一15sex性hd | 欧美一级黑人 | 亚洲成人国产综合 | 99精品国产一区二区三区 | 国产精品成人一区二区三区吃奶 | 美国一级黄色毛片 | 免费在线观看毛片 | 日韩视频不卡 | 九九精品视频免费 | 免费观看国产视频 |