基于springboot仿雀语的文档管理系统

news2024/11/24 19:58:08

项目介绍

本项目借鉴了雀语的一些UI设计,实现了文档在线管理的功能,知识库可以对不同分类的文档建立不同的库,知识库里面左边可以维护菜单菜单目录,右边实现在线预览。该项目可以防止用户下载和复制文档,只支持在线预览。

技术实现

文档上传的时候通过aspose把图片word和execel类型的文件转为pdf文件存储到nas,前端预览使用pdf.js加载预览pdf文档

项目预览

开发介绍

目录维护

左侧菜单点击加号可以维护目录

前端代码

<body class="white-bg">
    <div class="ibox-content">
        <form class="form-horizontal m-t" id="form1">
        <input id="id" name="id" type="hidden" />
        <input id="parentid" name="parentid" type="hidden" />
        <input id="deletemark" name="deletemark" type="hidden" />
        <input id="createdate" name="createdate" type="hidden" />
        <input id="createuserId" name="createuserId" type="hidden" />
        <input id="createusername" name="createusername" type="hidden" />
        <input id="modifydate" name="modifydate" type="hidden" />
        <input id="modifyuserid" name="modifyuserid" type="hidden" />
        <input id="modifyusername" name="modifyusername" type="hidden" />
        <div class="row">
            <div class="col-xs-12">
                <input type="text" id="name" class="form-control" placeholder="名称" />
            </div>
            <div class="col-xs-12">
                <textarea id="remark" class="form-control" placeholder="描述" style="width:100%;height:100px;"></textarea>
            </div>
            <div class="col-xs-12">
                <input type="text" id="sortcode" class="form-control" placeholder="序号" />
            </div>
            <div class="col-xs-12">
                <textarea id="keywords" class="form-control" placeholder="关键词" style="width:100%;height:100px;"></textarea>
            </div>
        </div>
        </form>
    </div>
</body>

后端代码

    @ApiOperation(value = "保存", notes = "")
    @PostMapping(value="/Save")
    public Result Save(@RequestBody String json) {
        DocmMenu model =  (DocmMenu) JSON.parseObject(json, DocmMenu.class);
        if(StrUtils.isBlank(model.getId())){
            model.setCreateuserid("admin");
            model.setDeletemark(new BigDecimal(0));
            model.setCreateusername("admin");
            model.setCreatedate(DateUtils.getNow());
            model.setId(UUID.randomUUID().toString());
            docmMenuService.add(model);
        }else {
            docmMenuService.update(model);
        }

        return Result.success(model,"成功!");
    }

文档维护

文档维护使用Dropzone实现前端文件上传,支持历史版本管理。

前端代码

<!DOCTYPE html>
<html>
<head>
    <title>文档管理</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="shortcut icon" href="favicon.ico">
    <link href="../css/bootstrap.min.css" rel="stylesheet">
    <link href="../css/font-awesome.min.css" rel="stylesheet">
    <link href="../css/plugins/jsTree/style.min.css" rel="stylesheet">
    <link href="../css/plugins/bootstrap-table/bootstrap-table.min.css" rel="stylesheet">
    <link href="../css/plugins/dropzone/dropzone.css" rel="stylesheet" type="text/css" />
    <!-- 全局js -->
    <script src="../js/jquery.min.js"></script>
    <script src="../js/bootstrap.min.js"></script>
    <script src="../js/plugins/jsTree/jstree.min.js"></script>
    <script src="../js/plugins/bootstrap-table/bootstrap-table.min.js"></script>
    <script src="../js/plugins/layer/layer.min.js"></script>
    <script src="../js/plugins/dropzone/dropzone.js"></script>
    <script src="../js/bd-framework.js"></script>

</head>
<body class="white-bg">
<div>
    <table id="table" style="width:100%"></table>
    <div id="main-container" style="margin-top: 10px;">
        <form action="/" class="dropzone" enctype="multipart/form-data" id="my-dropzone"
              method="post">
        </form>
        <textarea id="remark" class="form-control" placeholder="上传备注" style="width:100%;height:100px;"></textarea>
        <div>
            <!--上传按钮,提供多张图片一次性上传的功能-->
            <button id="submit-all" class="btn btn-outline btn-default" type="submit" >上传</button>
        </div>
    </div>

</div>
<script>
    var $table = $('#table');
    $(function () {
        getFileList();
    })

    function getFileList() {
        $table.html("");
        $.get("/api/Attachment/getList?businessid="+GetQuery('businessid'), {}, function (result, resultState) {
         if (result.code == 200) {

                if (result.data.length > 0) {
                    $table.append('<tr style="text-align: center; vertical-align: middle; border-style:solid; border-width:1px; ">'
                                            + '<td style="border-style:solid; border-width:1px;"><div style="padding:10px;width:30px;">序号</div></td>'
                                            + '<td style="border-style:solid; border-width:1px;"><div style="padding:10px;width:100px;">文件名称</div></td>'
                                            + '<td style="border-style:solid; border-width:1px;"><div style="padding:10px;width:80px;">上传人员</div></td>'
                                            + '<td style="border-style:solid; border-width:1px;"><div style="padding:10px;width:100px;">上传日期</div></td>'
                                            + '<td style="border-style:solid; border-width:1px;"><div style="padding:10px;width:200px;">备注</div></td>'
                                            + '<td style="border-style:solid; border-width:1px;"><div style="padding:10px;width:50px;">操作</div></td>'
                                    + '</tr>'
                                       );
                    for (var i = 0; i < result.data.length; i++) {
                        if (result.data[i].EXTENSION != "pdf" || (result.data[i].TMP_BUSINESSID || '') =='') {
                            $table.append('<tr style="border-style:solid; border-width:1px;">'
                                          + '<td style="border-style:solid; border-width:1px;text-align:center;">' + (i + 1) + '</td>'
                                           + '<td style="border-style:solid; border-width:1px;">'
                                               + '<a onclick="Download(\'' + result.data[i].ID + '\')" style="float: left" href="javascript:void(0)"><u>' + result.data[i].NAME + '</u></a>'
                                           + '</td>'
                                           + '<td style="border-style:solid; border-width:1px;">' + result.data[i].CREATEUSERNAME + '</td>'
                                           + '<td style="border-style:solid; border-width:1px;">' + result.data[i].CREATETIME + '</td>'
                                           + '<td style="border-style:solid; border-width:1px;">' + (result.data[i].REMARK || '') + '</td>'
                                           + '<td style="border-style:solid; border-width:1px;">'
                                               + '<div style="position:relative;padding:10px;">'
                                                   + '<button type="button" onclick="FileDelete(\'' + result.data[i].ID + '\')" data-id="' + result.data[i].ID + '">'
                                                         + '删除'
                                                   + '</button>'
                                               + '</div>'
                                           + '</td>'
                                       + '</tr>'
                                       );
                        }

                    }
                }
            }
        })
    }
    function FileDelete(Id) {
        var index = parent.layer.confirm('是否删除?', {
            btn: ['是', '否'] //按钮
        }, function () {
            $.get("/api/Attachment/deleteByPDF", { id: Id }, function (result, resultState) {
                    if (result.code == 200) {
                    parent.layer.msg("删除成功!");
                    getFileList();
                } else {
                    parent.layer.msg(result.message);
                    getFileList();
                }
            }, "json");
            parent.layer.close(index);
        }, function () {
            parent.layer.close(index);
        });
    }
    function Download(Id) {
        window.open('/api/Attachment/downFile?id=' + Id);
    };
</script>
<script>
    var loadIndex = null;
    //Dropzone的初始化,myDropzone为form的id
    Dropzone.options.myDropzone = {
        params: {
            businessid: GetQuery('businessid'),
            filePath: 'DocM'
            // 您可以在这里添加其他参数
        },
        //指定上传的路径
        url: "/api/Attachment/uploadFilesToPDF",
        maxFiles: 1, // 用于限制此Dropzone将处理的最大文件数
        maxFilesize: 100, // MB
        acceptedFiles: ".doc,.docx,.pdf,.PDF,.jpg,.JPG,.gif,.GIF,.png,.PNG,.bmp,.BMP", // 上传的类型
        dictDefaultMessage:
            '<div style="text-align: center; vertical-align: middle;padding-top:38px;"><span>将文件拖拽到此处</span>  \
			<span>(或点击上传)</span></div>'
          ,
        dictResponseError: 'Error while uploading file!',
        dictFileTooBig: "最大100M",
        //添加上传取消和删除预览的链接,默认不添加
        addRemoveLinks: true,
        //关闭自动上传功能,默认会true会自动上传
        //也就是添加一张图片向服务器发送一次请求
        autoProcessQueue: false,
        //允许上传多个
        uploadMultiple: false,
        //记得修改web.config 限制上传文件大小的节
        parallelUploads: 100,
        init: function () {
            var submitButton = document.querySelector("#submit-all")
            myDropzone = this; // closure

            //为上传按钮添加点击事件
            submitButton.addEventListener("click", function () {
                Dropzone.options.myDropzone.params.remark = $("#remark").val();
                loadIndex = parent.layer.load();
                //手动上传所有文件
                myDropzone.processQueue();
            });

            //当添加图片后的事件,上传按钮恢复可用
            this.on("addedfile", function () {
                $("#submit-all").removeAttr("disabled");
            });
            //当上传完成后的事件,接受的数据为JSON格式
            this.on("complete", function (data) {
                parent.layer.close(loadIndex);
                if (this.getUploadingFiles().length === 0 && this.getQueuedFiles().length === 0) {
                    var res = eval('(' + data.xhr.responseText + ')');
                    var msg;
                    if (res.code == 200) {
                        msg = "已成功上传!";
                        this.removeAllFiles();
                        getFileList()
                    }
                    else {
                        msg = "上传失败,失败的原因是:" + res.Message;
                    }
                    parent.layer.msg(msg);
                }
            });

            //删除文件的事件,当上传的文件为空时,使上传按钮不可用状态
            this.on("removedfile", function () {
                if (this.getAcceptedFiles().length === 0) {
                    $("#submit-all").attr("disabled", true);
                }
            });
        }
    };
</script>

</body>

后端代码

    @CrossOrigin
    @ApiOperation(value = "文件上传并且转为PDF", notes = "")
    @PostMapping("/uploadFileToPDF")
    public Result uploadFileToPDF(@RequestBody byte[] fileByte,
                             @RequestParam(value = "fileName",required = true) String fileName,
                             @RequestParam(value = "filePath",required = true) String filePath,
                             @RequestParam(value = "area",required = false) String area,
                             @RequestParam(value = "businessid",required = false) String businessid,
                             @RequestParam(value = "tmp_businessid",required = false) String tmp_businessid,
                             @RequestParam(value = "createUserId",required = false) String createUserId,
                             @RequestParam(value = "createUserName",required = false) String createUserName,
                             @RequestParam(value = "remark",required = false) String remark) {
        try {
            String uuid = UUID.randomUUID().toString();
            // 获取文件名
            String extension = getFileExtension(fileName);
            String newFileName = uuid + "." + extension;
            DocM_Attachment attachment = new DocM_Attachment();
            attachment.setBusinessid(businessid);

            attachment.setFilename(fileName);
            DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyMMdd");

            String uploadPath = filePath + FileConnect + LocalDate.now().format(fmt);
            attachment.setId(uuid);
            attachment.setCreateTime(DateUtils.getNow());
            attachment.setDirectory(uploadPath);
            attachment.setFilepath(uploadPath + FileConnect + newFileName);
            attachment.setFileSize(bytesToMegabytes((long)fileByte.length));
            attachment.setArea(area);
            attachment.setTmp_businessid(tmp_businessid);
            attachment.setExtension(extension);
            attachment.setCreateUserId(createUserId);
            attachment.setCreateUserName(createUserName);
            attachment.setRemark(remark);
            SmbFileUtils.save(fileByte,uploadPath,newFileName);

            InputStream inputStream = new ByteArrayInputStream(fileByte);
            OutputStream pdfStream = null;
            if(extension.equals("docx") || extension.equals("doc")){
                 pdfStream = doc2pdf(inputStream);
            }else if(extension.equals("png") || extension.equals("jpg") || extension.equals("gif")|| extension.equals("bmp")){
                 pdfStream = image2pdf(inputStream);
            }


            DocM_Attachment pdfAttachment = new DocM_Attachment();
            BeanUtils.copyProperties(attachment,pdfAttachment);
            pdfAttachment.setId(UUID.randomUUID().toString());
            String newPdfFileName = pdfAttachment.getId() + ".pdf";
            pdfAttachment.setFilepath(uploadPath + FileConnect + newPdfFileName);
            pdfAttachment.setArea("pdf");
            pdfAttachment.setTmp_businessid(attachment.getId());
            pdfAttachment.setFilename(fileName.split("\\.")[0] + ".pdf");
            pdfAttachment.setExtension("pdf");

            SmbFileUtils.save(toByteArray(pdfStream),uploadPath,newPdfFileName);

            inputStream.close();
            pdfStream.close();
            attachmentService.add(attachment);
            attachmentService.add(pdfAttachment);
            return Result.success(uuid,"成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed("文件上传失败");
        }
    }

知识库详情页面

左侧菜单使用JSTree.js插件,右侧文档预览使用pdf.js插件

前端代码

<!DOCTYPE html>
<html>
<head>
    <title>文档管理</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="shortcut icon" href="favicon.ico">
    <link href="../css/bootstrap.min.css" rel="stylesheet">
    <link href="../css/font-awesome.min.css" rel="stylesheet">
    <link href="../css/plugins/jsTree/style.min.css" rel="stylesheet">
    <link href="../css/plugins/bootstrap-table/bootstrap-table.min.css" rel="stylesheet">
    <!-- 全局js -->
    <script src="../js/jquery.min.js"></script>
    <script src="../js/bootstrap.min.js"></script>
    <script src="../js/plugins/jsTree/jstree.min.js"></script>
    <script src="../js/plugins/bootstrap-table/bootstrap-table.min.js"></script>
    <script src="../js/plugins/layer/layer.min.js"></script>
    <script src="../js/bd-framework.js"></script>
</head>
<body class="white-bg">
    <style>
    .pdf
    {
       position:relative;    
    }
    .main_container
    {
      display:flex;
    }
    .left_container
    {
        width:320px;   
        border-right:1px solid #e7e9e8; 
        overflow:scroll;
    }
    .right_contaner
    {
        flex:1;
    }
    #tree
    {
     
    }
    .jstree-anchor {
      font-size: 14px !important;
     /* color:#262626 !important;*/
    }
    .jstree-default .jstree-node {
        margin-top: 3px; /* 调整节点顶部的间距 */
        margin-bottom: 3px; /* 调整节点底部的间距 */
    }
    .btn-power
    {
        display:none;    
    }
    </style>
    <div class="main_container">
         <div class="left_container">
             <div id="directoryExpand">
             <input type="text" id="Keywords" class="form-control" placeholder="关键词搜索" />
                 <div style="display:flex; justify-content: space-between;align-items: center;margin-bottom:8px;padding:4px;">
                    <div onclick="directoryExpand()" style="font-size: 14px;color:#262626;padding-left:25px;"><i class="fa fa-bars" style="margin-right:12px;"  ></i>目录</div>
                    <div>
                        <div style="position: absolute;margin-left:-90px;margin-top:-12px;" class="btn-power">
                         <button data-toggle="dropdown" class="btn btn-outline btn-default dropdown-toggle" type="button" aria-expanded="false">
                              <i class="glyphicon glyphicon-plus" aria-hidden="true"></i>     
                        </button>
                         <button  onclick="btn_deleteAll()" class="btn btn-outline btn-default" type="button" >
                              <i class="glyphicon glyphicon-trash" aria-hidden="true"></i>     
                        </button>
                        <ul class="dropdown-menu pull-right">
                            <li>
                                <a href="javascript:;" onclick="btn_add()">新建分组</a>
                            </li>
                            <li>
                                <a href="javascript:;" onclick="btn_edit()">编辑分组</a>
                            </li>
                            <li>
                                <a href="javascript:;" onclick="btn_uploadFile()">上传文档</a>
                            </li>
                        </ul>
                        </div>
                    
                    </div>
                </div>
               
                <div id="tree"></div>
            </div>
            <div id="directoryNoExpand" style="display:none;">
                <div onclick="directoryNoExpand()" style="font-size: 14px;color:#262626;padding-left:12px;"><i class="fa fa-bars" ></i></div>
            </div>
         </div>
         <div class="right_contaner"  >
            <div class="pdf" >       
                <iframe  id="iframe1" width="100%" height="100%" src="" frameborder="0" ></iframe>
            </div>
         </div>
    </div>
    <script>
        var currentNode = null;
        var loadIndex = null;
        function directoryExpand() {
            $("#directoryExpand").hide();
            $(".left_container").css("width", "40px");
            $("#directoryNoExpand").show();   
        }
        function directoryNoExpand() {
            $("#directoryExpand").show();
            $(".left_container").css("width", "320px");
            $("#directoryNoExpand").hide();
        }
    
        $(function () {
            getPower();
           
            $(this).bind("contextmenu", function (e) {
                e.preventDefault();
            });
            $(".left_container").height($(window).height() - 7);
            $(".pdf").height($(window).height() - 7);
            document.getElementById('iframe1').onload = function () {
                parent.layer.close(loadIndex);
            }
            $("#Keywords").keydown(function (event) {
                initTree();
            });
        })
        var isManager = false;
        function getPower() {
            $.get("/api/DocManage/IsPowerMange", { id: GetQuery('Id') }, function (result, resultState) {
                if (result.code == 200) {
                    $(".btn-power").show();
                    isManager = true;
                }
                initTree();
            }, "json");
        }
        var $tree = $('#tree');
    
        function initTree() {
            if (!!GetQuery('Id')) {
                var postData = {}
                postData.id = GetQuery('Id');
                postData.keywords = $("#Keywords").val();
                $.get("/api/DocManage/GetTreeJson", postData, function (result, resultState) {
                    if (isManager) {
                        $tree
                        .on('activate_node.jstree', function (obj, e) {
                            var node = e.node;
                            if (node.id != undefined) {
                                currentNode = node;
                                loadPdf(node.id);
                            }
                        })
                        .jstree({
                            "multiple ": true,
                            'plugins': ["checkbox"],
                            'checkbox': {
                                'three_state': false,
                                'whole_node': false,
                                'tie_selection': false // 只有点击复选框才能勾选节点
                            },
                            'core': {
                                'data': result.data,
                                'check_callback': function (operation, node, node_parent, node_position, more) {
                                    // 允许所有操作,包括创建、重命名、删除、移动和复制节点
                                    return true;
                                }
                            }
                        })
                    } else {
                        $tree
                        .on('activate_node.jstree', function (obj, e) {
                            var node = e.node;
                            if (node.id != undefined) {
                                currentNode = node;
                                loadPdf(node.id);
                            }
                        })
                        .jstree({
                            "multiple ": true,
                            'core': {
                                'data': result.data
                            }
                        })
                    }
                }, "json");
            }
        }
        function loadPdf(businessid) {
            loadIndex = parent.layer.msg('文档加载中,请耐心等待!', { time: 1000 * 20 });
           
            $.get("/api/Attachment/getList?businessid="+businessid+"&area=pdf", {}, function (result, resultState) {
                if (result.code == 200) {
                    if (result.data.length > 0) {
                        for (var i = 0; i < result.data.length; i++) {
                            if (result.data[i].EXTENSION == "pdf") {
                                if (result.data[i].ID != currentNode.id) {
                                    $("#iframe1").attr("src", "../js/plugins/pdfjs-2.14.305-dist/web/DocManageViewer.html?file=" + "/api/Attachment/viewFile?id=" + result.data[i].ID);
                                }
                                break;
                            }
                        }
                    } else {
                        $("#iframe1").attr("src", "");
                        parent.layer.close(loadIndex);
                    }
                }
            }, "json");
        }
        function btn_add() {
            var viewData = {};
            if (currentNode) {
                viewData.parentid = currentNode.id;
            } else {
                viewData.parentid = GetQuery('Id')
            }
            var layerIndex = parent.layer.open({
                btn: ['确认'], //按钮
                yes: function (index, layero) {
                    var postData = parent.$("#layui-layer-iframe" + index)[0].contentWindow.GetWebControls("#form1");
                    $.ajax({
                        url: "/api/DocManage/Save",
                        type: "post",
                        contentType: 'application/json',
                        data: JSON.stringify(postData),
                        dataType: "json",
                        success: function (result) {
                            parent.layer.close(layerIndex);
                            // 在选中的节点下插入新节点
                            $tree.jstree('create_node', currentNode, { text: result.data.name, id: result.data.id, icon: "no-icon" }, 'last', function (newNode) {
                                //console.log('New node created with ID:', newNode.id);
                            });
                        }
                    });
                },
                type: 2,
                title: '新增',
                area: ['350px', '550px'],
                shadeClose: true, //开启遮罩关闭
                content: '/docManage/menuEdit.html',
                success: function (layero, index) {
                    parent.$("#layui-layer-iframe" + index)[0].contentWindow.SetWebControls(viewData);
                }
            });
        }
    
        function btn_edit() {
            var viewData = {};
            if (currentNode) {
                viewData.parentid = currentNode.id;
            } else {
                parent.layer.msg("请选择一个分组")
                return;
            }
            var layerIndex = parent.layer.open({
                btn: ['确认', '删除'], //按钮
                yes: function (index, layero) {
                    var postData = parent.$("#layui-layer-iframe" + index)[0].contentWindow.GetWebControls("#form1");
                    $.ajax({
                        url: "/api/DocManage/Save",
                        type: "post",
                        contentType: 'application/json',
                        data: JSON.stringify(postData),
                        dataType: "json",
                        success: function (result) {
                            parent.layer.close(layerIndex);
                            $tree.jstree('rename_node', currentNode, result.data.name);
                        }
                    });
                }, btn2: function (index, layero) {
                    var index = parent.layer.confirm('是否删除?', {
                        btn: ['是', '否'] //按钮
                    }, function () {
                        if (currentNode) {
                            var postData = {};
                            postData.id = currentNode.id;
                            $.get("/api/DocManage/Delete", postData, function (result, resultState) {
                                if (result.code == 200) {
                                    parent.layer.close(layerIndex);
                                    $tree.jstree('delete_node', currentNode);
                                }
                            }, "json");
                        }
                        parent.layer.close(index);
                    }, function () {
                        parent.layer.close(index);
                    });
                },
                type: 2,
                title: '编辑',
                area: ['350px', '550px'],
                shadeClose: true, //开启遮罩关闭
                content: '/docManage/menuEdit.html',
                success: function (layero, index) {
                    $.get("/api/DocManage/GetDetail", { id: currentNode.id }, function (result, resultState) {
                        parent.$("#layui-layer-iframe" + index)[0].contentWindow.SetWebControls(result.data);
                    }, "json");
    
                }
            });
        }
        function btn_deleteAll() {
            var index = parent.layer.confirm('是否删除?', {
                btn: ['是', '否'] //按钮
            }, function () {
                var postData = {};
                postData.ids = getTreeSelectedData().join(',');
                $.get("/api/DocManage/DeleteAll", postData, function (result, resultState) {
                    parent.layer.close(index);
                    $tree.jstree('delete_node', getTreeSelectedData());
                }, "json");
            }, function () {
                parent.layer.close(index);
            });
        }
        function btn_uploadFile() {
            var viewData = {};
            if (currentNode) {
                viewData.parentid = currentNode.id;
            } else {
                parent.layer.msg("请选择一个分组")
                return;
            }
            parent.layer.open({
                type: 2,
                title: '上传文档',
                area: ['850px', '750px'],
                shadeClose: true, //开启遮罩关闭
                content: '/docManage/uploadFileViewToPDF.html?businessid=' + currentNode.id,
                success: function (layero, index) {
                }
            });
        }
        function getTreeSelectedData() {
            var nodes = $tree.jstree("get_checked"); //使用get_checked方法
            return nodes;
        }
    </script>
    
</body>

文件转为pdf

使用aspose把文件转为pdf

    public OutputStream doc2pdf(InputStream inputStream) throws Exception {
        // 验证License 若不验证则转化出的pdf文档会有水印产生
        if (!getLicense()) {
            return null;
        }
        try {
            OutputStream outputStream =  new ByteArrayOutputStream();
            Document doc = new Document(inputStream);
            // 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
            doc.save(outputStream, SaveFormat.PDF);
            return  outputStream;
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("word转pdf失败");
        }
    }

访问nas

使用jcifs-ng访问nas

导入包

<!-- samba共享文件夹-->
<dependency>
    <groupId>eu.agno3.jcifs</groupId>
    <artifactId>jcifs-ng</artifactId>
    <version>2.1.3</version>
</dependency>

工具栏

public class SmbFileUtils {
    static String ip = "";
    static String domain = "";
    static String userName = "";
    static String password = "";

    static void SmbFileUtils(){

    }
    //根据账号密码登录
    private static CIFSContext withNTLMCredentials(CIFSContext ctx) {
        return ctx.withCredentials(new NtlmPasswordAuthenticator(domain,
                userName, password));
    }

    //保存文件
    public static String save(byte[] byteArr, String url,String fileName) throws IOException {
        InputStream in = new ByteArrayInputStream(byteArr);
        String status = "";
        try {
            CIFSContext context = withNTLMCredentials(SingletonContext.getInstance());
            SmbFileWriter.createDirectory("smb://" + domain  +"/" + url, context);
            boolean result = SmbFileWriter.writeSmbFile(in, "smb://" + domain  +"/" + url +"/" + fileName, context);
            status = "success";
        } catch (Exception e) {
            e.printStackTrace();
            status = "error";
        } finally {
            in.close();
            return status;
        }
    }

    //获取文件
    public static  InputStream getFile(String filePath) throws IOException {
        String url = "smb://" + domain + "/" + filePath;

        try {
            CIFSContext context = withNTLMCredentials(SingletonContext.getInstance());
            SmbFileReader reader = new SmbFileReader();
            byte[] byteArr = reader.readSmbFile(url, context);
            InputStream input = new ByteArrayInputStream(byteArr);
            return  input;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    //删除文件
    public static String delete(String filePath) throws IOException {
        String status = "";
        String url = "smb://" + domain + "/" + filePath;

        try {
            CIFSContext context = withNTLMCredentials(SingletonContext.getInstance());
            SmbFile file = new SmbFile(url, context);
            if (file.exists()) {
                file.delete();
                status = "success";
            }
        } catch (Exception e) {
            e.printStackTrace();
            status = "error";
        }
        return status;
    }
    //获取文件列表
    public static  List<HashMap> getPathList(String path) throws IOException {
        String url = "smb://" + domain + "/" + path;
        List<HashMap> filesList = new ArrayList<>();
        CIFSContext context = withNTLMCredentials(SingletonContext.getInstance());
        try {
            SmbFile smbFile = new SmbFile(url, context);
            SmbFile[] files = smbFile.listFiles();
            for (SmbFile file : files) {
                HashMap fileMap =new HashMap();
                fileMap.put("fileName",file.getName());
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 定义日期格式
                fileMap.put("date",sdf.format(new Date(file.getDate())));
                fileMap.put("size", String.format("%.2f", (file.length()/1024.0)/1024.0)+"MB");
                fileMap.put("fileType",file.isDirectory()?"文件夹":"文件");
                filesList.add(fileMap);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return filesList;
    }

    static class SmbFileReader {
        public byte[] readSmbFile(String path, CIFSContext context) throws IOException {
            try  {
                SmbFile smbFile = new SmbFile(path, context);
                long fileSize = smbFile.length();
                if (fileSize > Integer.MAX_VALUE) {
                    System.out.println("file too big...");
                    return null;
                }
                InputStream fi = smbFile.getInputStream();
                byte[] buffer = new byte[(int) fileSize];
                int offset = 0;
                int numRead = 0;
                while (offset < buffer.length
                        && (numRead = fi.read(buffer, offset, buffer.length - offset)) >= 0) {
                    offset += numRead;
                }
                // 确保所有数据均被读取
                if (offset != buffer.length) {
                    throw new IOException("Could not completely read file "
                            + smbFile.getName());
                }
                fi.close();
                return buffer;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    }

    static class SmbFileWriter {
        static boolean writeSmbFile(String source, String target, CIFSContext context) throws IOException {
            if (StrUtils.isEmpty(source) || StrUtils.isEmpty(target)) {
                return false;
            }
            return writeSmbFile(Files.newInputStream(Paths.get(source)),
                    target, context);
        }

        static boolean writeSmbFile(InputStream in, String target, CIFSContext context) throws IOException {
            if (Objects.nonNull(in) && StrUtils.isNotEmpty(target)) {
                try (SmbFile file = new SmbFile(target, context)) {
                    try (SmbFile parent = new SmbFile(file.getParent(), context)) {
                        if (!parent.exists()) {
                            createDirectory(file.getParent(), context);
                        }
                        if (!file.exists()) {
                            file.createNewFile();
                        }
                    }
                    try (OutputStream os = file.getOutputStream()) {
                        byte[] bytes = new byte[1024];
                        while (in.read(bytes) != -1) {
                            os.write(bytes);
                        }
                        return true;
                    }
                }finally {
                    in.close();
                }
            }
            return false;
        }

        static SmbFile createDirectory(String targetDir, CIFSContext context) throws MalformedURLException,
                CIFSException, MalformedURLException {
            try (SmbFile dir = new SmbFile(targetDir, context)) {
                if (!dir.exists()) {
                    dir.mkdir();
                }
                return dir;
            }
        }
    }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1591375.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

RK3568平台 SPI设备驱动

一.SPI简介 SPI是许多不同设备使用的常见通信协议。例如&#xff0c;SD卡模块、RFID读卡器模块和2.4GHz无线发射机/接收器均使用SPI与微控制器进行通信。 SPI是串行外设接口&#xff08;Serial Peripheral Interface)的缩写&#xff0c;是一种高速的&#xff0c;全双工&#x…

短视频转gif怎么做?三十秒在线转换gif

在现在这个快节奏的时代&#xff0c;gif动画相较于长时间的视频更受大众的欢迎。当我们需要将短视频、电影等视频制作成gif动画图片的时候就可以使用gif动画图片&#xff08;https://www.gif.cn/&#xff09;制作网站-GIF中文网&#xff0c;无需下载软件&#xff0c;手机、pc均…

零售行业数字化广告评价标准 - 《IAB/MRC零售(广告)测量指南》

IAB/MRC零售&#xff08;广告&#xff09;测量指南 --- 最新标准&#xff0c;2024年1月发布 目录 1出台此标准的目的是什么&#xff1f;2标准宗旨3本标准的主要关键领域4为什么这对品牌和零售商很重要5能给零售媒体中小型玩家带来什么机会&#xff1f;6评价零售媒体效果的最…

【JS】获取接口返回 EventStream 结构的数据(即接收读取 stream 流)

文章目录 EventStream 是一种服务器推送的数据格式&#xff0c;可以用于实时数据传输。 接口返回的示例图 获取示例&#xff1a; // 这里的 url 为虚拟的&#xff0c;仅供演示用 fetch(https://test.cn.com/api/agent/2, {method: POST,headers: {Content-Type: applicatio…

EaticSearch学习

ES学习目标 1、全文检索 2、ES介绍 2.1 安装&#xff08;docker&#xff09; docker pull elasticsearch:7.14.0 docker run -d -p 9200:9200 -p 9300:9300 -e "discovery.typesingle-node" elasticsearch:7.14.0初步检索 1、_cat GET /_cat/nodes&#xff1a;查看所…

RocketMQ安装部署+简单实战开发

文章目录 1.简介、安装部署2.Springboot集成RocketMQ2.1.添加maven依赖&#xff1a;2.2.RocketMQ配置生产者配置消费者配置 2.3.生产者&#xff08;发送消息&#xff09;2.4.消费者&#xff08;接收消息&#xff09; 3.实战结果3.1.消费者服务3.2.生产者服务3.3.运行日志生产日…

【SpringBoot】-- 使用minio对象存储服务实现上传图片

目录 一、安装minio 拉取镜像 启动 查看 进入登录页面 创建bucket 二、安装miniomc 三、代码 application.yml MinioUtil Controller 四、拓展 以下基于云服务和docker使用minio服务 一、安装minio Minio 是一个开源的对象存储服务器。它允许用户在私有云环境中建…

【vue】watch 侦听器

watch&#xff1a;可监听值的变化&#xff0c;旧值和新值 <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><titl…

蓝桥杯嵌入式(G431)备赛——最后一晚查漏补缺

蓝桥杯嵌入式&#xff08;G431&#xff09;备赛笔记——初始化cubeMX 蓝桥杯嵌入式&#xff08;G431&#xff09;备赛笔记——LED 蓝桥杯嵌入式&#xff08;G431&#xff09;备赛笔记——按键模块设计 蓝桥杯嵌入式&#xff08;G431&#xff09;备赛笔记——LCD按键 蓝桥杯…

2023年度编程语言将花落谁家

2023年度编程语言将花落谁家 TIOBE的预测你预测年度最受欢迎的编程语言会是什么&#xff1f;TIOBE 认为 C# 最有可能成为年度编程语言&#xff0c;你同意吗&#xff1f;为什么&#xff1f;AI时代已经到来&#xff0c;你有学习新语言的打算吗&#xff1f; 以下是来自年度编程语言…

Android 纵向双选日历

这个日历的布局分两部分&#xff0c;一部分是显示星期几的LinearLayout&#xff0c;另外就是一个RecyclerView&#xff0c;负责纵向滚动了。 工具类&#xff1a; implementation com.blankj:utilcode:1.17.3上activity_calendar代码&#xff1a; <?xml version"1.0&…

Springboot+Vue项目-基于Java+MySQL的在线视频教育平台系统(附源码+演示视频+LW)

大家好&#xff01;我是程序猿老A&#xff0c;感谢您阅读本文&#xff0c;欢迎一键三连哦。 &#x1f49e;当前专栏&#xff1a;Java毕业设计 精彩专栏推荐&#x1f447;&#x1f3fb;&#x1f447;&#x1f3fb;&#x1f447;&#x1f3fb; &#x1f380; Python毕业设计 &…

中仕公考:非师范生能考教师编吗?

非师范生能考教师编吗?关于这个问题&#xff0c;首要条件是必须持有教师资格证。只要符合招聘条件&#xff0c;非师范专业背景的考生同样有资格报名参加教师编制考试。具体要求如下&#xff1a; 1. 年龄要求&#xff1a;申请人需年满35岁以下&#xff0c;对于特定职位&#x…

Elastic 线下 Meetup 将于 2024 年 4 月 27 号在重庆举办

2024 Elastic Meetup 重庆站活动&#xff0c;由 Elastic、新智锦绣联合举办&#xff0c;现诚邀广大技术爱好者及开发者参加。 活动时间 2024年4月27日 13:30-18:00 活动地点 中国重庆 沙坪坝区学城大道62-1号研发楼一期b3栋1楼(瑞幸咖啡旁&#xff09; 活动流程 14:00-14:50…

多因子模型的数据处理

优质博文&#xff1a;IT-BLOG-CN 数据处理的基本目的是从多量的、可能是杂乱无章的、难以理解的数据中抽取并推导出有价值、有意义的数据。特别是金融数据&#xff0c;存在数据缺失&#xff0c;不完整以及极端异常值等问题&#xff0c;对于我们的分析和建模影响很多。 对于我…

【新版HI3559AV100开发注意事项(四)】

新版HI3559AV100开发注意事项&#xff08;四&#xff09; 三十、HI3559A参数中对输入分辨率限制的原因是&#xff1f; 答&#xff1a;分辨率限制有两个来源&#xff1a; 一个是时钟频率最高为600M&#xff0c;开启一拍两像素之后相当于1200M。你这个数据量太大了&#xff0c;6…

(学习日记)2024.04.17:UCOSIII第四十五节:中断管理

写在前面&#xff1a; 由于时间的不足与学习的碎片化&#xff0c;写博客变得有些奢侈。 但是对于记录学习&#xff08;忘了以后能快速复习&#xff09;的渴望一天天变得强烈。 既然如此 不如以天为单位&#xff0c;以时间为顺序&#xff0c;仅仅将博客当做一个知识学习的目录&a…

微信小程序开发学习笔记——4.9【小案例】开启下拉刷新页面enablePullDownRefresh

>>跟着b站up主“咸虾米_”学习微信小程序开发中&#xff0c;把学习记录存到这方便后续查找。 课程连接&#xff1a;4.9.【小案例】开启下拉刷新页面enablePullDownRefresh_哔哩哔哩_bilibili 一、api2.json 小程序配置 / 页面配置 (qq.com) {"usingComponents&q…

抖音小店是什么?个人店、个体店、企业店,新手商家该如何选择?

大家好&#xff0c;我是电商花花。 抖音小店这两年来说都是一个发展不错的电商项目&#xff0c;凭借着直播电商的快速发展&#xff0c;让更多人看到了抖音小店其中的红利&#xff0c;吸引着商家入驻。 抖音小店是什么&#xff1f; 很多人会把抖音小店和达人橱窗搞混&#xff…

Harmony鸿蒙南向驱动开发-Regulator接口使用

功能简介 Regulator模块用于控制系统中某些设备的电压/电流供应。在嵌入式系统&#xff08;尤其是手机&#xff09;中&#xff0c;控制耗电量很重要&#xff0c;直接影响到电池的续航时间。所以&#xff0c;如果系统中某一个模块暂时不需要使用&#xff0c;就可以通过Regulato…