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的文件夹路径必须是/结尾,否则读取子文件夹会有 问题。

相关推荐
咖啡八杯3 小时前
GoF设计模式——解释器模式
java·后端·spring·设计模式
优橙教育3 小时前
5G网优培训 vs Java开发:转行选哪个?
java·开发语言·5g
糖果店的幽灵3 小时前
【DeepAgents 从入门到精通】Context Management 上下文管理
java·人工智能·后端·spring·中间件·langgraph·deepagents
腻害兔4 小时前
【若依项目-产品经理视角】RuoYi-Vue-Pro 源码拆解:字典、短信、邮件、通知——后台系统的“基础设施四件套“!
java·前端·vue.js·产品经理·ai编程
SeaTunnel5 小时前
从 Python Script 地狱到标准化数据集成框架
大数据·开发语言·python·程序员·代码·seatunnel
碎光拾影5 小时前
ARM交叉工具链各工具作用及IMX6ULL平台LED+蜂鸣器裸机程序实现
java·开发语言·数据库
CCPC不拿奖不改名5 小时前
大模型推理架构与开源生态知识整理
数据库·windows·python·架构·langchain·开源·github
Marst Code6 小时前
(python)2026Plotly 库评估:交互式可视化到底值不值得引入?
开发语言·python
Miao121316 小时前
微服务 API 测试实践:海外某民宿平台如何构建模式驱动测试基础设施
java·开发语言
腻害兔6 小时前
【若依项目-产品经理视角】RuoYi-Vue-Pro 源码拆解:支付模块 yudao-module-pay,一个让产品经理都看懂的支付中台设计
java·前端·vue.js·产品经理·ai编程