Java中SMB的应用

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Java中SMB的应用

本猿的农耕指代手册   2020-01-28 我要评论

目录

  • SMB 服务操作
    • Ⅰ SMB简介
    • Ⅱ SMB配置
      • 2.1 Windows SMB
    • Ⅲ 添加SMB依赖
    • Ⅳ 路径格式
    • Ⅴ 操作共享
    • Ⅵ 登录验证

SMB 服务操作

Ⅰ SMB简介

​ SMB(全称是Server Message Block)是一个协议名,它能被用于Web连接和客户端与服务器之间的信息沟通。SMB协议作为一种局域网文件共享传输协议,常被用来作为共享文件安全传输研究的平台。
​ Windows操作系统都包括了客户机和服务器 SMB协议支持。Microsoft 为 Internet 提供了SMB的开源版本,即通用Internet文件系统CIFS。与现有 Internet 应用程序如文件传输协议FTP相比, CIFS 灵活性更大。对于UNIX系统,可使用一种称为Samba的共享软件。

Ⅱ SMB配置

2.1 Windows SMB

2.1.1 配置服务

​ 在本地机上以Windows10举例 :在控制面板 -->程序-->程序和功能-->启用或关闭Windows功能-->SMB 1.0/cifs file sharing support勾选SMB 1.0/CIFS ClientSMB 1.0/CIFS Server

2.1.2 验证服务

​ 开启之后来验证一下SMB是否正确开启:在DOS命令窗口用PowerShell命令进入程序输入Get-SmbServerConfiguration | Select EnableSMB1Protocol, EnableSMB2Protocol查看服务状态,如图所示:

2.1.3 共享文件

​ 在D盘新建一个测试文件D:\Test\SmbTest\GoalTest右键菜单-->授予访问权限-->特定用户选择一个用户进行授权,如图所示:

​ 授权给用户之后会提示你的文件夹已共享,在DOS窗口输入弹窗提示的共享连接\\DESKTOP-D5DVINV\Test即可进入共享文件夹,右击共享文件夹还可以设置访问密码,更改访问用户等等。

Ⅲ 添加SMB依赖

​ 在pom.xml中添加SMB服务相关的依赖:

<!-- 引用SmbFile类的jar包  -->
<dependency>
       <groupId>jcifs</groupId>
       <artifactId>jcifs</artifactId>
       <version>1.3.17</version>
<https://img.qb5200.com/download-x/dependency>

Ⅳ 路径格式

​ 在Java中SMB路径请求格式有如下三种情况:

  • 如果是无需密码的共享,格式类似:smb://ip/sharefolder(例如:smb://192.168.0.77/test)
  • 如果需要用户名和密码,格式类似:smb://username:password@ip/sharefolder(例:smb://chb:123456@192.168.0.1/test)
  • 如果用户名密码和域名,格式类似:smb:域名;用户名:密码@目的IP/文件夹/文件名.xxx(例:smb://orcl;wangjp:Password123@192.168.193.13/Test)

Ⅴ 操作共享

​ 以上步骤之后,就完成了在Windows上建立了一个SMB文件服务器和必要准备工作,接下来就是简单的代码环节,上传和下载的逻辑也比较简单,对SMB共享文件的操作其实就是处理SmbFile对象。

import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import jcifs.smb.SmbFileOutputStream;
import org.springframework.util.FileCopyUtils;
import java.io.*;
/**
 * @author: Create By WangJP
 * @description: SMB服务操作相关
 * @date: 2020/1/1
 */
public class Demo {

    private static final String SMB_SHARE_FOLDER = "smb://username:password@192.168.1.103/Test/";
    private static final String SHARE_FOLDER_PATH = "SmbTest\\GoalTest";
    private static final String FILE_NAME = "test.txt";
    private static final String LOCAL_DIR = "D:\\LocalTest";

    public static void main(String[] args) {
        downloadSmbFile(SMB_SHARE_FOLDER, SHARE_FOLDER_PATH, FILE_NAME, LOCAL_DIR);
        uploadFile(SMB_SHARE_FOLDER, SHARE_FOLDER_PATH, FILE_NAME, LOCAL_DIR);
    }

    /**
     * 从SMB共享文件夹下载文件到本地
     * @param remoteUrl       SMB请求路径Url
     * @param shareFolderPath 共享文件夹中SMB目标文件存放的完整路径
     * @param fileName        文件名
     * @param localDir        本地文件夹
     */
    public static void downloadSmbFile(String remoteUrl, String shareFolderPath, String fileName, String localDir) {
        InputStream in = null;
        OutputStream out = null;
        try {
            SmbFile smbfile = new SmbFile(remoteUrl + shareFolderPath + File.separator + fileName);
            File localFile = new File(localDir + File.separator + fileName);
            in = new BufferedInputStream(new SmbFileInputStream(smbfile));
            out = new BufferedOutputStream(new FileOutputStream(localFile));
            FileCopyUtils.copy(in, out);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeStreanm(in, out);
        }
    }

    /**
     * 将本地文件夹中的文件上传到SMB共享文件夹(与下载类似)
     * @param remoteUrl       SMB请求路径Url
     * @param shareFolderPath 共享文件夹中SMB目标文件存放的完整路径
     * @param fileName        文件名
     * @param localDir        本地文件夹
     */
    private static void uploadFile(String remoteUrl, String shareFolderPath, String fileName, String localDir) {
        InputStream in = null;
        OutputStream out = null;
        try {
            SmbFile smbfile = new SmbFile(remoteUrl + shareFolderPath + File.separator + fileName);
            File localFile = new File(localDir + File.separator + fileName);
            in = new BufferedInputStream(new FileInputStream(localFile));
            out = new BufferedOutputStream(new SmbFileOutputStream(smbfile));
            FileCopyUtils.copy(in, out);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeStreanm(in, out);
        }
    }

    private static void closeStreanm(InputStream in, OutputStream out) {
        try {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

​ 自己工作中有一个业务需求是要检测SMB共享目录中的某个文件是否存在,通过下载上传的例子,学习到获取 SmbFile 对象需要特定的的属性(url canon等等)构建,处理方法上有很多和File对象类似,代码示例如下:

 /**
     * 检验SMB共享文件是否存在
     * @param remoteUrl       SMB请求路径Url
     * @param shareFolderPath 共享文件夹中SMB目标文件存放的完整路径
     * @param fileName        文件名
     * @return true:存在 false:不存在
     */
    public static boolean checkSmbFile(String remoteUrl, String shareFolderPath, String fileName) {
        boolean result = false;
        try {
            SmbFile smbfile = new SmbFile(remoteUrl + shareFolderPath + File.separator + fileName);
            result = smbfile.exists();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

Ⅵ 登录验证

​ SMB的登录验证主要是为解决账号密码中存在特殊字符的问题(比如转义字符,链接里的特定字符),存在特殊字符的账号密码往往会报出下列异常:

Connected to the target VM, address: '127.0.0.1:54593', transport: 'socket'
jcifs.smb.SmbAuthException: Logon failure: unknown user name or bad password.

​ 这时为了构建合法的SmbFile对象,我们就需要先进行登录验证,再去尝试构建该对象:

private static String domainip = "192.168.170.13";
private static String username = "username";
private static String password = "password";
private static String remoteurl = "smb://192.168.170.13/share";
//进行账号IP地址登录验证
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(domainip, username, password);  
SmbFile smbfile = new SmbFile(remoteurl+"//"+folderpath,auth);

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们