SMB(全称是Server Message Block)是一个协议名,它能被用于Web链接和客户端与服务器之间的信息沟通。SMB协议做为一种局域网文件共享传输协议,常被用来做为共享文件安全传输研究的平台。
Windows操做系统都包括了客户机和服务器 SMB协议支持。Microsoft 为 Internet 提供了SMB的开源版本,即通用Internet文件系统CIFS。与现有 Internet 应用程序如文件传输协议FTP相比, CIFS 灵活性更大。对于UNIX系统,可以使用一种称为Samba的共享软件。spring
在本地机上以Windows10举例 :在控制面板
-->程序
-->程序和功能
-->启用或关闭Windows功能
-->SMB 1.0/cifs file sharing support
勾选SMB 1.0/CIFS Client
和SMB 1.0/CIFS Server
windows
开启以后来验证一下SMB是否正确开启:在DOS命令窗口用PowerShell
命令进入程序输入Get-SmbServerConfiguration | Select EnableSMB1Protocol, EnableSMB2Protocol
查看服务状态,如图所示:安全
在D盘新建一个测试文件D:\Test\SmbTest\GoalTest
,右键菜单
-->授予访问权限
-->特定用户
选择一个用户进行受权,如图所示:服务器
受权给用户以后会提示你的文件夹已共享,在DOS窗口输入弹窗提示的共享链接\\DESKTOP-D5DVINV\Test
便可进入共享文件夹,右击共享文件夹还能够设置访问密码,更改访问用户等等。dom
在pom.xml
中添加SMB服务相关的依赖:socket
<!-- 引用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对象相似,代码示例以下:url
/** * 检验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);