java 实现文件夹上传

@AutoLog(value = "上传文件夹")
@ApiOperation(value = "上传文件夹", notes = "上传文件夹")
@PostMapping(value = "/upFolder")
public Result<?> uploadFolder(MultipartFile file, String id, Integer level, String path, String title) {
    synchronized ("") { #同步锁
        String originalFilename = file.getOriginalFilename();
        Result<ArrayList<Document>> result = new Result<>();
        ArrayList<Document> documents = new ArrayList<>();
        Document pDocument = new Document();
        String[] strings = originalFilename.split("/");
        String p = "";
        String pid = id;
        int level_ = 0;
        for (int i = 0; i < strings.length - 1; i++) {
            p = p + "/" + strings[i];
            File file1 = new File(ROOT_PATH + path + "/" + title + p);
            String title1 = p.substring(p.lastIndexOf("/") + 1);
            String path1 = null;
            if (id.equals("root")) {
                path1 = path + p.substring(0, p.lastIndexOf("/"));
            } else {
                path1 = path + "/" + title + p.substring(0, p.lastIndexOf("/"));
            }
            if (!file1.exists()) {
                file1.mkdir();
                Document newFolder = new Document()
                        .setKey(UUID.randomUUID().toString().replaceAll("-", "").substring(18))
                        .setTitle(title1)
                        .setCreateTime(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now()))
                        .setLevel(level + i + 1)
                        .setParentId(pid)
                        .setIsFile(0)
                        .setPath(path1)
                        .setDocumentNum(0)
                        .setFolderNum(0);
                documentService.save(newFolder);
                documentService.increaseFolderNum(pid);
                pid = newFolder.getId();
            } else {
                try {
                    pid = documentService.queryByTitleAndPath(title1, path1).getId();
                } catch (NullPointerException e) {
                    result.error500(originalFilename + " 上传失败");
                }
            }
            level_ = level + i + 2;
            if (id.equals("root") && i == 0) {
                pDocument.setId(pid);
            }
        }
        Document document = new Document();
        String title_ = strings[strings.length - 1];
        String fileType = title_.substring(title_.lastIndexOf(".") + 1);
        String documentType = FileTypeConstant.FILE_TYPE_MAP.get(fileType);
        String mode = "";
        if (fileType.equals("pdf")) {
            mode = "view";
        }
        float tempSize = (float) file.getSize() / 1024 / 1024;
        String size = (tempSize + "").substring(0, 5) + " Mb";
        String localTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now());
        String path_ = null;
        if (id.equals("root")) {
            path_ = path + p;
        } else {
            path_ = path + "/" + title + p;
        }
        /**
         * 保存数据库
         */
        document.setKey(UUID.randomUUID().toString().replaceAll("-", "").substring(18))
                .setTitle(title_)
                .setFileType(fileType)
                .setDocumentType(documentType)
                .setMode(mode)
                .setSize(size)
                .setCreateTime(localTime)
                .setUpdateTime(localTime)
                .setLevel(level_)
                .setParentId(pid)
                .setIsFile(1)
                .setUrl(HTTP_PATH + path_ + "/" + title_)
                .setPath(path_);
        documentService.save(document);
        documentService.updateDocumentNum(1, pid);
        documents.add(document);
        documents.add(pDocument);
        File file1 = new File(ROOT_PATH + path_ + "/", title_);
        try {
            if (!file1.exists()) {
                file1.createNewFile();
            }
            file.transferTo(file1);
        } catch (IOException e) {
            e.printStackTrace();
        }
        /**
         * 调用es方法
         */
        EsFileResource net = new EsFileResource();
        net.setUuid(id);
        net.setFileName(title_);
        net.setLocalFileUrl(ROOT_PATH + path_ + "/" + title_);
        net.setNetFileUrl(HTTP_PATH + path_ + "/" + title_);
        net.setFileCreationDate(localTime);
        esIndexService.process(net);
        result.setResult(documents);
        return result;
    }
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值