欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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>>>>返回結(jié)果:{}", 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();
     }
     // 將文件拷貝到當(dāng)前目錄下
     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 實(shí)現(xiàn)多文件批量上,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

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

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

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

    java serialVersionUID解決序列化類(lèi)版本不一致問(wèn)題面試精講

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

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

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

    使用java基礎(chǔ)類(lèi)實(shí)現(xiàn)zip壓縮和zip解壓工具類(lèi)分享

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

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

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

    五個(gè)很實(shí)用的IDEA使用技巧分享

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

    mybatis分頁(yè)絕對(duì)路徑寫(xiě)法過(guò)程詳解

    這篇文章主要介紹了mybatis分頁(yè)絕對(duì)路徑寫(xiě)法過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • 簡(jiǎn)單談?wù)凧ava垃圾回收

    簡(jiǎn)單談?wù)凧ava垃圾回收

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

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

    下面小編就為大家?guī)?lái)一篇基于Rest的API解決方案(jersey與swagger集成)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-08-08
  • Java添加事件監(jiān)聽(tīng)的四種方法代碼實(shí)例

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

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

最新評(píng)論