目录
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 Client 和 SMB 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>
- </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);
来源: https://www.cnblogs.com/WangJpBlog/p/12239999.html