亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時間:2017年03月23日 11:03:10   作者:y0yO011  
本文通過實例代碼給大家講解了SpringMvc+Angularjs 實現(xiàn)多文件批量上傳功能,非常不錯,具有參考借鑒價值,需要的朋友一起學習吧

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

相關文章

  • SpringBoot整合Mybatis與druid實現(xiàn)流程詳解

    SpringBoot整合Mybatis與druid實現(xiàn)流程詳解

    這篇文章主要介紹了springboot整合mybatis plus與druid詳情,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的下伙伴可以參考一下
    2022-10-10
  • java serialVersionUID解決序列化類版本不一致問題面試精講

    java serialVersionUID解決序列化類版本不一致問題面試精講

    這篇文章主要為大家介紹了serialVersionUID解決序列化類版本不一致問題的面試精講,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-10-10
  • Springboot網(wǎng)站第三方登錄 微信登錄

    Springboot網(wǎng)站第三方登錄 微信登錄

    這篇文章主要為大家詳細介紹了Springboot網(wǎng)站第三方登錄 ,微信登錄,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • 使用java基礎類實現(xiàn)zip壓縮和zip解壓工具類分享

    使用java基礎類實現(xiàn)zip壓縮和zip解壓工具類分享

    使用java基礎類寫的一個簡單的zip壓縮解壓工具類,實現(xiàn)了指定目錄壓縮到和該目錄同名的zip文件和將zip文件解壓到指定的目錄的功能
    2014-03-03
  • Idea 搭建Spring源碼環(huán)境的超詳細教程

    Idea 搭建Spring源碼環(huán)境的超詳細教程

    這篇文章主要介紹了Idea 搭建Spring源碼環(huán)境,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-10-10
  • 五個很實用的IDEA使用技巧分享

    五個很實用的IDEA使用技巧分享

    IntelliJ IDEA 是一款優(yōu)秀的 Java 集成開發(fā)環(huán)境,它提供了許多強大的功能和快捷鍵,可以幫助開發(fā)者提高編碼效率和質量,本文就在為你介紹博主常用的五個IntelliJ IDEA使用技巧,希望能夠給你帶來一些工作效率上的提升
    2023-10-10
  • mybatis分頁絕對路徑寫法過程詳解

    mybatis分頁絕對路徑寫法過程詳解

    這篇文章主要介紹了mybatis分頁絕對路徑寫法過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-12-12
  • 簡單談談Java垃圾回收

    簡單談談Java垃圾回收

    本文是看了James Gosling的<<Java程序設計語言>>后結合自己的一些項目經(jīng)驗,簡單總結下關于java的垃圾回收問題的看法,有需要的小伙伴可以參考下
    2016-05-05
  • 基于Rest的API解決方案(jersey與swagger集成)

    基于Rest的API解決方案(jersey與swagger集成)

    下面小編就為大家?guī)硪黄赗est的API解決方案(jersey與swagger集成)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • Java添加事件監(jiān)聽的四種方法代碼實例

    Java添加事件監(jiān)聽的四種方法代碼實例

    這篇文章主要介紹了Java添加事件監(jiān)聽的四種方法代碼實例,本文直接給出代碼示例,并用注釋說明,需要的朋友可以參考下
    2014-09-09

最新評論