文章目录
Java开源samba client
jcifs
官网: https://www.jcifs.org/
官方翻译描述:
感谢Samba组织主办JCIFS项目18年!
如果您正在寻找最新和最大的开源Java SMB库,这不是它。JCIFS仅在维护模式下运行了几年,尽管它所支持的功能运行良好(smb1、ntlmv2、milc、msrpc和各种实用程序类),但JCIFS不支持较新的smb2/3变种的smb2/3,这种变种的smb2/3正逐渐成为必需的(Windows 10需要smb2/3)。**JCIFS只支持SMB1,但微软在其产品中已弃用了SMB1。**因此,如果在您的网络上禁用了smb1,jcifs的文件相关操作将无法工作。
幸运的是,有多种新的开源SMB Java项目来尝试包括以下内容:
- jcifs-ng
A cleaned-up and improved version of the jCIFS library
一个经过清理和改进的JCIFS库版本 - smbj
smbj - SMB2/SMB3 client library for Java
关于JCIFS,maven描述:
JCIFS is an Open Source client library that implements the CIFS/SMB networking protocol in 100% Java
最新版本发布时间比较老,2012年。
关于 JCIFS NG maven中央仓库描述:
A pure-java CIFS/SMB client library
最新版本发布时间2019
关于 SMBJ maven中央仓库描述:
SMB2 protocol library for communication with Windows servers
最新版本发布时间2018
JCIFS NG
git官网:https://github.com/AgNO3/jcifs-ng
<!--java samba client api-->
<dependency>
<groupId>eu.agno3.jcifs</groupId>
<artifactId>jcifs-ng</artifactId>
<version>2.1.3</version>
</dependency>
jcifs-ng 2.1
This release enables SMB2 support by default and contains some experimental SMB3.0 support.
Protocol levels negotitated can now be controlled with jcifs.smb.client.minVersion and jcifs.smb.client.maxVersion (this deprecates the jcifs.smb.client.enableSMB2 / jcifs.smb.client.disableSMB1 properties). Default min/max versions are SMB1 to SMB210.
This release deprecates server browsing (i.e. server/workgroup enumeration) and contains some breaking API changes regarding authentication.
这个版本默认启用了smb2支持,并包含一些实验性的smb3.0支持。
协议级别协商现在可以使用jcifs.smb.client.minversion和jcifs.smb.client.maxversion进行控制(取消jcifs.smb.client.enablesmb2/jcifs.smb.client.disablesmb1属性)。默认的最小/最大版本是smb1到smb210。
此版本不支持服务器浏览(即服务器/工作组枚举),并包含一些有关身份验证的破坏性API更改。
常用类和方法整理
SmbFile: 此类标识在samba网络上的一个资源。主要资源是文件或目录,不过SmbFile也可以是服务或工作组。SmbFile实例是不可变对象。因此,一旦创建,代表这个对象的绝对路径pathname就不会改变。
/**
* This class represents a resource on an SMB network. Mainly these
* resources are files and directories however an <code>SmbFile</code>
* may also refer to servers and workgroups. If the resource is a file or
* directory the methods of <code>SmbFile</code> follow the behavior of
* the well known {@link java.io.File} class. One fundamental difference
* is the usage of a URL scheme [1] to specify the target file or
* directory. SmbFile URLs have the following syntax:
*
。。。
* Instances of the <code>SmbFile</code> class are immutable; that is,
* once created, the abstract pathname represented by an SmbFile object
* will never change.
CIFSContext接口
api测试
api使用基本套路:如下简单的demo测试,需要一个 CIFSContext 实例以及一个 sambaURL ,利用new SmbFile传入这两个参数。利用SmbFile实例的方法来操作。核心就是 SmbFile类提供的各种方法,来完成各种samba操作。
public class BaseCIFSTest {
public static void main(String[] args) {
String ip = "11.11.111.111";
String dir = "LogDir";
String shareUrl = "smb://" + ip + "/" + dir + "/";
Properties props = new Properties();
//props.putAll(this.properties);
CIFSContext ctx = null;
try {
ctx = new BaseContext(new PropertyConfiguration(props));
SmbResource f = new SmbFile(shareUrl, ctx.withAnonymousCredentials());
System<