Java获取共享文件夹文件

Maven 使用 jcifs 1.3.17 版本
		<dependency>
            <groupId>jcifs</groupId>
            <artifactId>jcifs</artifactId>
            <version>1.3.17</version>
        </dependency>
JAVA 代码如下
package com.xxx.common;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.StrUtil;
import com.xxx.UIFrame;
import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @Date: 2022/10/25
 */
public class GainRemoteFile {

    private static String REMOTEURL = "//192.168.10.202/";
    private static String REMOTEUSERNAME = "user";
    private static String REMOTEPASSWORD = "psd";

    /**
     * 判断是否需要更新
     */
    public static boolean checkNeedUpdate() {
        String remoteVersion = "";
        try {
            remoteVersion = getRemoteFileLike(REMOTEURL, "查询工具");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return !UIFrame.VERSION.startsWith(remoteVersion);
    }

    /**
     * 获取远程文件
     *
     * @param remoteFilepath 远程文件地址,该参数需以IP打头,如'192.168.8.2/aa/bb.java'或者'192.168.8.2/aa/',如'192.168.8.2/aa'是不对的
     * @return SmbFile 获取SmbFile文件
     * @throws Exception
     */
    private static SmbFile getRemoteSmbFilePath(String remoteFilepath) {
        NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, REMOTEUSERNAME, REMOTEPASSWORD);
        SmbFile smbFile = null;
        try {
            smbFile = new SmbFile("smb:" + remoteFilepath, auth);
            if (!smbFile.exists()) {
                Console.log("no such folder");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return smbFile;
    }

    /**
     * 获取远程文件列表
     */
    public static List<SmbFile> getRemoteFileList(String remoteFilepath) throws Exception {
        SmbFile smbFile = getRemoteSmbFilePath(remoteFilepath);
        return smbFile == null ? Collections.EMPTY_LIST : Arrays.asList(smbFile.listFiles());
    }

    /**
     * 获取 远程文件名称
     */
    public static String getRemoteFileLike(String remoteFilepath, String remoteFileNameLike) throws Exception {
        if (StrUtil.isBlank(remoteFileNameLike)) {
            return null;
        }
        for (SmbFile smbFile : getRemoteFileList(remoteFilepath)) {
            String name = smbFile.getName();
            if (name.contains(remoteFileNameLike)) {
                return name;
            }
        }
        return null;
    }

    /**
     * 判断远程文件是否存在
     */
    public static boolean remoteFileExist(String remoteFilepath, String remoteFileNameLike) throws Exception {
        if (StrUtil.isNotBlank(remoteFileNameLike)) {
            return StrUtil.isNotBlank(getRemoteFileLike(remoteFilepath, remoteFileNameLike));
        }
        return false;
    }

    /**
     * 拷贝远程文件到本地目录
     */
    public static boolean copyRemoteFile(String remoteFilepath, String remoteFileNameLike, String localDirectory) throws Exception {
        for (SmbFile smbFile : getRemoteFileList(remoteFilepath)) {
            String name = smbFile.getName();
            if (!name.contains(remoteFileNameLike)) {
                continue;
            }
            File file = FileUtil.file(localDirectory, name);
            try {
                if (file.exists()) {
                    file.delete();
                } else if (smbFile.exists()) {
                    BufferedInputStream bis = new BufferedInputStream(new SmbFileInputStream(smbFile));
                    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
                    int len = 0;
                    byte[] bytes = new byte[1024];
                    while ((len = bis.read(bytes)) != -1) {
                        bos.write(bytes, 0, len);
                    }
                    bos.close();
                    bis.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return true;
    }
}

以下内容可以忽略

发文助手会检测您的文章标题、错别字、内容质量,助您提升文章质量。【创作规范】
发文助手会检测您的文章标题、错别字、内容质量,助您提升文章质量。【创作规范】
发文助手会检测您的文章标题、错别字、内容质量,助您提升文章质量。【创作规范】
发文助手会检测您的文章标题、错别字、内容质量,助您提升文章质量。【创作规范】
发文助手会检测您的文章标题、错别字、内容质量,助您提升文章质量。【创作规范】
发文助手会检测您的文章标题、错别字、内容质量,助您提升文章质量。【创作规范】
发文助手会检测您的文章标题、错别字、内容质量,助您提升文章质量。【创作规范】
发文助手会检测您的文章标题、错别字、内容质量,助您提升文章质量。【创作规范】
发文助手会检测您的文章标题、错别字、内容质量,助您提升文章质量。【创作规范】
发文助手会检测您的文章标题、错别字、内容质量,助您提升文章质量。【创作规范】
发文助手会检测您的文章标题、错别字、内容质量,助您提升文章质量。【创作规范】
发文助手会检测您的文章标题、错别字、内容质量,助您提升文章质量。【创作规范】
发文助手会检测您的文章标题、错别字、内容质量,助您提升文章质量。【创作规范】
发文助手会检测您的文章标题、错别字、内容质量,助您提升文章质量。【创作规范】

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

God__is__a__girl

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值