Java实现将共享文件

  1. 依赖注入
        <!--网络共享内文件传输-->
        <!--java连接共享文件夹-->
        <dependency>
            <groupId>org.samba.jcifs</groupId>
            <artifactId>jcifs</artifactId>
            <version>1.3.14-kohsuke-1</version>
        </dependency>

2.代码接口

    @GetMapping(value = "/getShareFile")
    @ApiOperation(value = "获取共享文件信息")
    public RestResult<List<String>> getShareFile(@RequestParam(value = "netWorkPath") String netWorkPath) {
        //String a = "smb:" + "//administrator:123@192.168.0.111/defect-M/";
        File shareFile=new File(shareFilePath);
        if (!shareFile.exists()){
            shareFile.mkdirs();
        }
        String a = "smb:" + netWorkPath;
        SmbFile file = null;
        try {
            file = new SmbFile(a);
            file.connect();
            List<String> list = new LinkedList<>();
            if (file.exists()) {
                if (file.isDirectory()) {
                    SmbFile[] files = file.listFiles();
                    for (SmbFile f : files
                    ) {
                        if (f.getPath().contains("Thumbs.db") || f.isDirectory()) {
                            continue;
                        }
                        smbGet(f.getPath(), shareFilePath);
                        list.add(httpPath + shareFilePath.replaceAll(shareFilePath.split("/")[0], "") + f.getName());
                    }
                }
            }
            return RestResultUtil.genSuccessResult(list);
        } catch (Exception e) {
            log.info("访问共享路径错误");
            e.printStackTrace();
        }
        return RestResultUtil.failed();
    }



    @ApiOperation(value = "从共享目录下载文件到本地")
    private void smbGet(String remoteUrl, String localDir) {
        InputStream in = null;
        OutputStream out = null;
        try {
            SmbFile remoteFile = new SmbFile(remoteUrl);
            if (remoteFile == null) {
                return;
            }
            String fileName = remoteFile.getName();
            File localFile = new File(localDir + File.separator + fileName);
            in = new BufferedInputStream(new SmbFileInputStream(remoteFile));
            out = new BufferedOutputStream(new FileOutputStream(localFile));
            byte[] buffer = new byte[1024];
            while (in.read(buffer) != -1) {
                out.write(buffer);
                buffer = new byte[1024];
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    @GetMapping(value = "getWebToLocalFile")
    @ApiOperation("下载共享文件到本地(保存数据库,方便以后展示)")
    public RestResult<Object> getWebToLocalFile(@RequestParam("webPath") String webPath
            , @RequestParam("updateId") String updateId) {
        File file=new File(createPath);
        if (!file.exists()){
            file.mkdirs();
        }
        NdeUploadFile ndeUploadFile = ndeUploadFileService.ndeUploadById(updateId);
        //查询上传服务表最大批次
        Integer i = ndeFileServerService.selectMaxByBatch();
        String[] split = webPath.split(",");
        List<NdeFileServer> fileServersList = new ArrayList<>();
        for (String sp : split
        ) {
            String fileName = sp.substring(sp.lastIndexOf("/") + 1);
            File webFile = new File(shareFilePath + fileName);
            NdeFileServer fileServer = new NdeFileServer();
            try {

                //获取图片的宽高
                BufferedImage sourceImg = ImageIO.read(new FileInputStream(webFile));
                fileServer.setHeight(sourceImg.getHeight());
                fileServer.setWidth(sourceImg.getWidth());


                //保存到本地
                String s = createPath + "/" + ndeUploadFile.getDirUrl() + "/" + fileName;
                File localFile = new File(s);
                //清理缓存
                System.gc();
                boolean b = webFile.renameTo(localFile);
                if (!b) {
                    //renameTo移动失败,就复制文件,然后删除原文件
                    FileUtils.copyFile(webFile, localFile);
                    webFile.delete();
                }
                fileServer.setType(1);
                fileServer.setHttpUrl(httpPath + s.replaceAll(s.split("/")[0], ""));
                fileServer.setFilePath(s);

                //批次
                if (StringUtils.isEmpty(i)) {
                    fileServer.setBatch(1);
                } else {
                    fileServer.setBatch(i + 1);
                }
                fileServer.setUploadFileId(updateId);
                fileServer.setFileName(fileName);
                fileServer.setId(StringUtils.createUUID());
                fileServer.setCreateTime(new Date());
                ndeFileServerService.insertSelective(fileServer);
                fileServersList.add(fileServer);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return RestResultUtil.genSuccessResult(fileServersList);
    }

3.开启SMB服务
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值