使用Java局域网读取windows共享文件夹(smb协议)

本文介绍使用Java在局域网读取Windows共享文件夹,基于SMB协议。先简单介绍SMB协议,接着给出demo,包括设置文件夹为共享文件夹的步骤,以及Java代码实现,如添加Maven依赖、获取共享文件夹文件、下载文件到指定文件夹,强调需处于同一局域网。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


前言

使用Java局域网读取windows共享文件夹(smb协议)


一、smb协议简单了解

SMB(全称是Server Message Block)是一个网络协议名,它能被用于Web连接和客户端与服务器之间的信息沟通。SMB最初是IBM的贝瑞·费根鲍姆(Barry Feigenbaum)研制的,其目的是将DOS操作系统中的本地文件接口“中断13”改造为网络文件系统。

二、demo

1.设置文件夹为共享文件夹

(1)右键文件夹点击属性
右键点击属性

(2)点击共享
在这里插入图片描述
(3)添加everyone 点击共享
在这里插入图片描述
(4)点击网络共享中心配置
在这里插入图片描述
在这里插入图片描述
(4)另一台局域网电脑怎么访问共享的文件夹。
在这里插入图片描述

2.Java代码

(1)用到的Maven依赖

<!--smb协议获取共享文件夹内容-->
        <dependency>
            <groupId>jcifs</groupId>
            <artifactId>jcifs</artifactId>
            <version>1.3.17</version>
        </dependency>

(2)获取共享文件夹下所有的文件

/**
     * 读取共享文件夹下的所有文件(文件夹)的名称
     *
     * @param "smb://192.168.1.215/ecg/2022-09-29/ 
     */
    public static SmbFile[] getSharedFileList(String smbFileUrl) {
        SmbFile smbFile;
        try {
            smbFile = new SmbFile(smbFileUrl);
            if (!smbFile.exists()) {
                log.error("文件夹不存在");
            } else {
                SmbFile[] files = smbFile.listFiles();
                return files;
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (SmbException e) {
            e.printStackTrace();
        }
        return null;
    }

(2)下载文件到指定文件夹

/**
     * 下载文件到指定文件夹
     * @param remoteUrl 远程文件夹路径
     * @param fileName 文件名称
     * @param localDir 要下载到本地的路径
     * @param localFileName 下载到本地的文件重命名名称
     */
    public static void downloadFileToFolder(String remoteUrl, String fileName, String localDir, String localFileName) {
        InputStream in = null;
        OutputStream out = null;
        try {
            File dir = new File(localDir);
            if(!dir.exists()){
                dir.mkdir();
            }
            SmbFile remoteFile = new SmbFile(remoteUrl  + "/" + fileName);
            File localFile = new File(localDir + "/" + localFileName);
            in = new BufferedInputStream(new SmbFileInputStream(remoteFile));
            out = new BufferedOutputStream(new FileOutputStream(localFile));
            byte[] buffer = new byte[1024];
            while (in.read(buffer) != -1) {
                out.write(buffer);
                buffer = new byte[1024];
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

总结

注意要处与同一局域网内

Java中,如果你想要通过用户名和密码连接到另一台电脑的共享文件夹,通常会使用SMB(Server Message Block)或CIFS(Common Internet File System)协议,因为它们常用于局域网内的文件共享。你可以利用`java.net.Socket`或`java.nio.file`包下的API来进行操作,但是最常见的是使用`java.io.FileURLConnection`配合`java.net.URL`。 以下是基本步骤: 1. 创建URL对象,指定共享文件夹的路径和主机名: ```java URL url = new URL("smb://[username]:[password]@[remote_host]/shared_folder"); ``` 记得将 `[username]` 和 `[password]` 替换为实际的用户名和密码,`[remote_host]` 替换为目标电脑的IP地址或域名。 2. 使用`FileURLConnection`创建连接: ```java FileURLConnection connection = (FileURLConnection) url.openConnection(); connection.setDoOutput(true); // 如果需要写入数据 connection.setRequestProperty("User-Agent", "Java App"); // 添加身份验证信息 ``` 3. 连接共享文件夹并进行读取、写入等操作: ```java InputStream in = connection.getInputStream(); // 用于读取文件内容 OutputStream out = connection.getOutputStream(); // 用于写入文件内容 // 使用合适的IO流处理文件 try (BufferedReader reader = new BufferedReader(new InputStreamReader(in)); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out))) { String line; while ((line = reader.readLine()) != null) { // 处理读取的行 } // 对于写入,可以像下面这样写入一行文本 writer.write("新内容"); } in.close(); out.close(); connection.disconnect(); ``` 务必注意安全问题,尤其是处理敏感信息时,应考虑加密传输以及避免硬编码密码。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值