springmvc+layui的多文件上传

5 篇文章 0 订阅

话不多说直接上代码:

public String uploadFile2(HttpServletRequest request, HttpSession session) throws Exception {
      // 创建一个通用的多部分解析器
      CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
      JSONObject resObj = new JSONObject();
      if (multipartResolver.isMultipart(request)) {
         // 转换成多部分request
         MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
         // get the parameter names of the multipart files contained in this request
         Iterator<String> iter = multiRequest.getFileNames();
         while (iter.hasNext()) {
            // 取得上传文件
            List<MultipartFile> multipartFiles = multiRequest.getFiles(iter.next());
//          String dirPath = "E:/Code/ideaCode/educational-admin/src/resources/Files/";
//          String dirPath = "E:/Code/ideaCode/educational-admin/src/resources/Files/";
            String dirPath = "/home/tomcat/apache-tomcat-8.5.42/webapps/educational-1.0-SNAPSHOT/resource/Files/";
            for (MultipartFile multipartFile : multipartFiles) {
               String originalFilename = multipartFile.getOriginalFilename();
               System.out.println("originalFilename------>" + originalFilename);
               if (StringUtils.isNotEmpty(originalFilename)) {
                  multipartFile.transferTo(new File(dirPath + originalFilename));

                  // 获取服务器路径
                  String serverUrl = "";
                  serverUrl = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/resource/Files/" + originalFilename;

                  UploadFile uploadFile = new UploadFile();
                  uploadFile.setFileName(originalFilename);
                  uploadFile.setUploadPerson(String.valueOf(session.getAttribute("stu_name")));
                  uploadFile.setUploadTime(CreateTimeUtils.createTime());
                  uploadFile.setFileUrl(dirPath);
                  // 判断文件类型
                  String fileType = originalFilename.substring(originalFilename.indexOf(".") + 1);
                  resObj.put("fileType", fileType);
                  // 进行插入
                  uploadFileService.addUploadFile(uploadFile);
                  DownloadFile df = new DownloadFile();
                  df.setFileName(originalFilename);
                  df.setFile(originalFilename);
                  df.setFileUrl(dirPath);
                  df.setOpreateType("下载");
                  df.setStatus(0);
                  df.setUploadTime(CreateTimeUtils.createTime());
                  downloadFileService.addDownloadFile(df);

                  // 读取数据
                  List lists = ImportCustUtils.ImportCust(dirPath, originalFilename);
                  // 判断为空
                  if (lists != null) {
                     String title = String.valueOf(lists.get(0));
                     // 截取数据
                     if (title.contains("[") || title.contains("]")) {
                        String realTitle = title.substring(title.indexOf("[") + 1, title.indexOf("]"));
                        boolean flag = false;
                        if (realTitle.contains("学生信息导入表")) {
                           flag = true;
                        }
                        resObj.put("flag", flag);
                     }
                  }
                  resObj.put("code", "0");
                  resObj.put("originalFilename", originalFilename);
                  resObj.put("dirPath", dirPath);
               }
            }
         }
      }
      return resObj.toJSONString();
   }

 

 

 

 

 

 

 

  layui.use('upload', function () {
        var $ = layui.jquery
            , upload = layui.upload;

        //多文件列表示例
        var demoListView = $('#demoList')
            , uploadListIns = upload.render({
            elem: '#testList'
            , url: '/upload/uploadFile2.do'
            , accept: 'file'
            , 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-xs demo-reload layui-hide">重传</button>'
                        , '<button class="layui-btn layui-btn-xs 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) {
                console.log(res);
                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]; //删除文件队列已经上传成功的文件
                }
                this.error(index, upload);
            }
            , error: function (index, upload) {
                var tr = demoListView.find('tr#upload-' + index)
                    , tds = tr.children();
                tds.eq(2).html('<span style="color: #FF5722;">上传失败</span>');
                tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传
            }
            ,allDone: function(obj){ //当文件全部被提交后,才触发
//            console.log(obj.total); //得到总文件数
//            console.log(obj.successful); //请求成功的文件数
//            console.log(obj.aborted); //请求失败的文件数
                console.log(obj);
        }
        });
    });

欢迎关注我的微信公众号,更多内容将在微信公众号中发布:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值