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

相关推荐
DY009J7 小时前
从 MSYS2 环境中提取独立 MinGW-w64 工具链的技术方案
c++·windows
angushine8 小时前
Windows下循环复制一个文件
windows
421!8 小时前
C 语言学习笔记——11(函数指针与指针函数)
c语言·开发语言·笔记·单片机·学习
cch89188 小时前
汇编与C语言:底层对话VS高效指挥
c语言·开发语言·汇编
♪-Interpretation8 小时前
第七节:Python的内置容器
开发语言·python
Java成神之路-8 小时前
Spring IOC 注解开发实战:从环境搭建到纯注解配置详解(Spring系列3)
java·后端·spring
551只玄猫8 小时前
【数学建模 matlab 实验报告8】回归分析
开发语言·数学建模·matlab·课程设计·实验报告
凌波粒8 小时前
LeetCode--383.赎金信(哈希表)
java·算法·leetcode·散列表