springmvc+layui的多文件上传

话不多说直接上代码:

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 iter = multiRequest.getFileNames();
         while (iter.hasNext()) {
            // 取得上传文件
            List 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 = $([''
                        , '' + file.name + ''
                        , '' + (file.size / 1014).toFixed(1) + 'kb'
                        , '等待上传'
                        , ''
                        , ''
                        , ''
                        , ''
                        , ''].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('上传成功');
                    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('上传失败');
                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);
        }
        });
    });

你可能感兴趣的:(java,layui)