Java使用jcifs读取Windows的共享文件

依赖配置

复制代码
<dependency>
            <groupId>org.codelibs</groupId>
            <artifactId>jcifs</artifactId>
            <version>3.0.2</version>
        </dependency>

Java类

java 复制代码
package com.cim.ext.components;

import com.cim.ext.dto.FileInfo;
import lombok.extern.slf4j.Slf4j;
import org.codelibs.jcifs.smb.CIFSContext;
import org.codelibs.jcifs.smb.context.SingletonContext;
import org.codelibs.jcifs.smb.impl.NtlmPasswordAuthenticator;
import org.codelibs.jcifs.smb.impl.SmbFile;
import org.codelibs.jcifs.smb.impl.SmbFileInputStream;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 使用jcifs读取SMB共享文件的工具类
 */
@Component
@Slf4j
public class SmbFileReader {

    @Value("${smb.user}")
    private String smbUser;

    @Value("${smb.password}")
    private String smbPassword;

    public SmbFile readSmbFile(String path) throws MalformedURLException{
        // 创建认证器
        NtlmPasswordAuthenticator auth = new NtlmPasswordAuthenticator(
                null,  // 域名,这里不需要填
                smbUser,
                smbPassword
        );

        // 创建CIFS上下文
        CIFSContext context = SingletonContext.getInstance().withCredentials(auth);

        // 初始化SMB文件对象
        return new SmbFile(path, context);
    }

    public SmbFile[] list(String path) {


        List<SmbFile> smbFiles = new ArrayList<>();
        SmbFile dir = null;
        
        try {
            dir = readSmbFile(path);
            
            if (!dir.exists()) {
                log.warn("Directory not found: {}", path);
                return null;
            }
            
            if (!dir.isDirectory()) {
                log.warn("Path is not a directory: {}", path);
                return null;
            }
            
            SmbFile[] files = dir.listFiles();
            return files;


            // smb://10.20.12.114/FreedoData/2023/
            
        } catch (Exception ex) {
            log.error("Failed to list files from path: {}", path, ex);
            return null;
        } finally {
            if (dir != null) {
                dir.close();
            }
        }

    }


    public SmbFile[] list2(String path) throws MalformedURLException {


        SmbFile dir = new SmbFile(path);

        try {
            dir = readSmbFile(path);

            if (!dir.exists()) {
                log.warn("Directory not found: {}", path);
                return null;
            }

            if (!dir.isDirectory()) {
                log.warn("Path is not a directory: {}", path);
                return null;
            }

            SmbFile[] files = dir.listFiles();
            return files;


            // smb://10.20.12.114/FreedoData/2023/

        } catch (Exception ex) {
            log.error("Failed to list files from path: {}", path, ex);
            return null;
        } finally {
            if (dir != null) {
                dir.close();
            }
        }

    }


}

这个框架需要注意的是Smb的文件夹路径必须是/结尾,否则读取子文件夹会有 问题。

相关推荐
咩咩啃树皮8 小时前
第40篇:Vue3组件化开发精讲——组件拆分、复用、父子通信、工程化架构
java·前端·架构
灯澜忆梦8 小时前
GO_并发编程---定时器
开发语言·后端·golang
鱟鲥鳚8 小时前
Spring Boot 集成 LangChain4j:从模型调用到 Tool Calling(Demo版)
java·spring boot
-银雾鸢尾-8 小时前
C#中的StringBuilder相关方法
开发语言·c#
-银雾鸢尾-9 小时前
C#中结构体与类的区别;抽象类与接口的区别
开发语言·c#
广州灵眸科技有限公司9 小时前
xfce桌面触摸校准:基于灵眸科技EASY-EAl-Orin-Nano
数据库·windows·科技
大模型码小白10 小时前
【Python零基础教程】继承、多态与魔法函数:面向对象编程三大核心特性详解
java·大数据·开发语言·人工智能·python·ai编程
腾渊信息科技公司10 小时前
Spring Boot对接MES实战:视觉检测数据自动同步方案
java·人工智能·spring boot·后端·计算机视觉·ai·软件需求
爱笑的源码基地11 小时前
高并发 Redis 缓存门诊HIS系统源码,含财务统计药房进销存
java·程序·门诊系统·诊所系统·云诊所源码
wuqingshun31415912 小时前
TCP超时重传机制是为了解决什么问题?
java