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

相关推荐
langsiming2 分钟前
【无标题】
java·开发语言·数据库
꧁细听勿语情꧂4 分钟前
合并两个有序表、判断链表的回文结构、相交链表、环的链表一和二
c语言·开发语言·数据结构·算法
Rust语言中文社区5 分钟前
【Rust日报】2026-04-24 Vizia 0.4 发布——纯 Rust 声明式响应式 GUI 框架
开发语言·后端·rust
weisian1518 分钟前
Java并发编程--45-分布式一致性协议入门:Raft、Paxos与ZAB的核心思想
java·分布式·raft·paxos·zab
木井巳10 分钟前
【递归算法】解数独
java·算法·leetcode·决策树·深度优先·剪枝
普通网友14 分钟前
JavaScript:ESLint+Prettier 规范代码格式
开发语言·javascript·ecmascript
t***54415 分钟前
如何在 Dev-C++ 中切换编译器
java·开发语言·c++
Lisonseekpan17 分钟前
Git:如何将一个分支的特定提交合并到另一个分支?
java·大数据·git·后端·elasticsearch
Boop_wu18 分钟前
[Java EE 进阶]Mybatis进阶(动态SQL)
java·数据库·maven·mybatis
jiayong2327 分钟前
第 38 课:任务列表里高亮当前正在查看详情的任务
开发语言·前端·javascript·vue.js·学习