一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - Java教程 - java Springboot實現多文件上傳功能

java Springboot實現多文件上傳功能

2021-05-28 12:10西郊巷外 Java教程

這篇文章主要為大家詳細介紹了java Springboot實現多文件上傳功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

前端采用layui框架,講解多文件上傳的完整實現功能。

前端html重點代碼如下:

java" id="highlighter_207918">
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<div class="layui-form-item">
  <label class="layui-form-label">上傳文件</label>
 <div class="layui-input-block">
 <div class="layui-upload">
 <button type="button" class="layui-btn layui-btn-normal" id="testlist">選擇多文件</button>
 <div class="layui-upload-list">
  <table class="layui-table">
  <thead>
  <tr><th>文件名</th>
  <th>大小</th>
  <th>狀態</th>
  <th>操作</th>
  </tr></thead>
  <tbody id="demolist"></tbody>
  </table>
 </div>
 <button type="button" class="layui-btn" id="testlistaction">開始上傳</button>
 </div>
   </div>
</div>

相應的,js代碼如下所示:

?
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
layui.use('upload', function(){
   var $ = layui.jquery,upload = layui.upload;
   //多文件列表示例
   var demolistview = $('#demolist')
        ,uploadlistins = upload.render({
        elem: '#testlist'
        ,url: '/upload'
        ,accept: 'file'
        ,data:{}  //可放擴展數據 key-value
        ,multiple: true
        ,auto: false
        ,bindaction: '#testlistaction'
        ,choose: function(obj){
          var files = this.files = obj.pushfile(); //將每次選擇的文件追加到文件隊列
          //讀取本地文件
          obj.preview(function(index, file, result){
            var tr = $(['<tr id="upload-'+ index +'">'
              ,'<td>'+ file.name +'</td>'
              ,'<td>'+ (file.size/1014).tofixed(1) +'kb</td>'
              ,'<td>等待上傳</td>'
              ,'<td>'
              ,'<button class="layui-btn layui-btn-mini demo-reload layui-hide">重傳</button>'
              ,'<button class="layui-btn layui-btn-mini layui-btn-danger demo-delete">刪除</button>'
              ,'</td>'
              ,'</tr>'].join(''));
 
            //單個重傳
            tr.find('.demo-reload').on('click', function(){
              obj.upload(index, file);
            });
 
            //刪除
            tr.find('.demo-delete').on('click', function(){
              delete files[index]; //刪除對應的文件
              tr.remove();
              uploadlistins.config.elem.next()[0].value = ''; //清空 input file 值,以免刪除后出現同名文件不可選
            });
 
            demolistview.append(tr);
          });
        }
        ,done: function(res, index, upload){
          if(res.code == 0) //上傳成功
            var tr = demolistview.find('tr#upload-'+ index)
              ,tds = tr.children();
          tds.eq(2).html('<span style="color: #5fb878;">上傳成功</span>');
          tds.eq(3).html(''); //清空操作
          return delete this.files[index]; //刪除文件隊列已經上傳成功的文件
 
        } //code為后臺傳回來的數據,具體多少自己定,
 
        //后臺只能傳回json格式數據,不然會走error函數;
 
        ,error: function(index, upload){
 
     }
  })
});

以上即是前端功能的實現,后端方面,在service層impl下創建文件上傳的函數:

?
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
public string uploadnoticefile(multipartfile filelist) {
   try{
      string pathname = filepath;
      string timemillis = long.tostring(system.currenttimemillis());//時間戳
      string filename = timemillis + filelist.getoriginalfilename();
      file dir = new file(pathname);
      if (!dir.exists()) {
        dir.mkdirs();
      }
      string filepath = pathname + filename;
      file serverfile = new file(filepath);
      filelist.transferto(serverfile);
 
      //存入數據庫
      noticefile noticefile = new noticefile();
      noticefile.setnofilename(filename);
      noticefile.setnofilepath(filepath);
      noticefile.setnoid(0l);
      noticefilerepository.save(noticefile);
      return "1";
 
    }catch (exception e) {
      e.printstacktrace();
      return "0";
    }
 
  }

noticefile是我個人在寫項目時創建的類,讀者可根據實際情況自行運用。

然后,在controller層中創建相應的函數:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
@requestmapping(value = "/upload", method = requestmethod.post)
  @responsebody
  public map<string, object> noticefile(@requestparam(name = "file") multipartfile files) {
    string msg = noticefileservice.uploadnoticefile(files);
 
    map map = new hashmap();
    if (msg == "1") {
      map.put("code", "0");
    } else {
      map.put("code", "1");
    }
    return map;
  }

以上,即實現了多文件上傳的全部功能。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/weixin_42259631/article/details/80999415

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国内精品一区二区在线观看 | 成人免费一区二区三区在线观看 | 千金在线观看 | 门房秦大爷在线阅读 | 色吧欧美 | 女人被男人躁得好爽免费视频 | 国产小情侣自拍 | 国产91 最新 在线 | 五月婷婷在线播放 | 91夜色视频 | 日日夜夜撸影院 | 韩国伦理hd | 欧美一区二区三区免费看 | 好吊妞乱淫 | 欧美日韩一区二区三区免费不卡 | 久久99精品久久久久久园产越南 | 亚洲视频一区网站 | 久久成人永久免费播放 | 热99re久久精品国产首页 | 精品牛牛影视久久精品 | sao虎在线精品永久在线 | 久久嫩草影院网站 | 亚洲sss综合天堂久久久 | 99久久香蕉国产线看观香 | 男gay网站视频免费观看 | 女同性互吃奶乳免费视频 | 国产成人夜色91 | 国产青草亚洲香蕉精品久久 | 国产精品永久免费10000 | 亚洲欧美一级夜夜爽w | 日本免费观看的视频在线 | 91入口免费网站大全 | 国产成人免费高清激情视频 | 国产一区二区三区丶四区 | 日本美女动态图片 | 幻女free性摘花第一次 | 免费国产白棉袜踩踏区域 | 99久久精品免费看国产一区二区 | 日本中文字幕一区二区三区不卡 | 亚洲午夜久久久久影院 | 国内自拍网红在线综合 |