javaSpringboot實(shí)現(xiàn)多文件上傳功能

前端采用layui框架,講解多文件上傳的完整實(shí)現(xiàn)功能。

創(chuàng)新互聯(lián)專注于企業(yè)網(wǎng)絡(luò)營(yíng)銷推廣、網(wǎng)站重做改版、二七網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5網(wǎng)站設(shè)計(jì)、商城網(wǎng)站開(kāi)發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為二七等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。

前端html重點(diǎn)代碼如下:

<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>狀態(tài)</th>
 <th>操作</th>
 </tr></thead>
 <tbody id="demoList"></tbody>
 </table>
 </div>
 <button type="button" class="layui-btn" id="testListAction">開(kāi)始上傳</button>
 </div>
  </div>
</div>

相應(yīng)的,js代碼如下所示:

layui.use('upload', function(){
  var $ = layui.jquery,upload = layui.upload;
  //多文件列表示例
  var demoListView = $('#demoList')
    ,uploadListIns = upload.render({
    elem: '#testList'
    ,url: '/upload'
    ,accept: 'file'
    ,data:{} //可放擴(kuò)展數(shù)據(jù) key-value
    ,multiple: true
    ,auto: false
    ,bindAction: '#testListAction'
    ,choose: function(obj){
     var files = this.files = obj.pushFile(); //將每次選擇的文件追加到文件隊(duì)列
     //讀取本地文件
     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(''));
 
      //單個(gè)重傳
      tr.find('.demo-reload').on('click', function(){
       obj.upload(index, file);
      });
 
      //刪除
      tr.find('.demo-delete').on('click', function(){
       delete files[index]; //刪除對(duì)應(yīng)的文件
       tr.remove();
       uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值,以免刪除后出現(xiàn)同名文件不可選
      });
 
      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 >上傳成功</span>');
     tds.eq(3).html(''); //清空操作
     return delete this.files[index]; //刪除文件隊(duì)列已經(jīng)上傳成功的文件
 
    } //code為后臺(tái)傳回來(lái)的數(shù)據(jù),具體多少自己定,
 
    //后臺(tái)只能傳回json格式數(shù)據(jù),不然會(huì)走error函數(shù);
 
    ,error: function(index, upload){
 
   }
 })
});

以上即是前端功能的實(shí)現(xiàn),后端方面,在Service層Impl下創(chuàng)建文件上傳的函數(shù):

public String uploadNoticeFile(MultipartFile fileList) {
  try{
   String pathname = filepath;
   String timeMillis = Long.toString(System.currentTimeMillis());//時(shí)間戳
   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);
 
   //存入數(shù)據(jù)庫(kù)
   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是我個(gè)人在寫(xiě)項(xiàng)目時(shí)創(chuàng)建的類,讀者可根據(jù)實(shí)際情況自行運(yùn)用。

然后,在controller層中創(chuàng)建相應(yīng)的函數(shù):

@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;
 }

以上,即實(shí)現(xiàn)了多文件上傳的全部功能。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

新聞名稱:javaSpringboot實(shí)現(xiàn)多文件上傳功能
本文URL:http://muchs.cn/article46/gpjohg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、商城網(wǎng)站、定制網(wǎng)站域名注冊(cè)、微信小程序、網(wǎng)頁(yè)設(shè)計(jì)公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)