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

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

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 黄绝一级 | 探花 在线 | 成人在线一区二区三区 | 操mm| 忘忧草在线社区WWW日本直播 | 啊啊啊好大视频 | 日韩国产成人资源精品视频 | 亚洲成人99 | 我的妹妹最近有点怪在线观看 | 10个免费货源网站 | 亚洲春色综合另类网蜜桃 | 白丝校花掀起短裙呻吟小说 | 被老头肉至怀孕小说 | 国产极品久久 | 日韩精选 | 范冰冰好紧好滑好湿 | 97久久久亚洲综合久久88 | 久久精视频| videos欧美肥婆| 小sao货水好多真紧h的视频 | 四虎永久免费地址在线网站 | 男人边吃奶边做好爽视频免费 | 精品国产一区二区三区久久久蜜臀 | 精品第一国产综合精品蜜芽 | 无限在线观看视频大全免费高清 | 国产里番 | www一区| 免费观看欧美成人禁片 | 亚洲国产综合自在线另类 | 国产成年人在线观看 | 波多野结衣不卡 | 99热在线精品播放 | 亚洲国产资源 | 国产欧美日韩精品一区二区三区 | 天天干天天日天天射天天操毛片 | 国产精品香蕉夜间视频免费播放 | 国产一区日韩二区欧美三区 | 午夜理论电影在线观看亚洲 | 日韩在线视频一区二区三区 | 欧美在线观看网址 | x8x8在线观看 |