jcifs 库1.x版本,使用示例
时间: 2025-08-05 21:07:36 浏览: 4
<think>我们正在寻找jcifs 1.x版本的使用示例。jcifs是一个用于CIFS/SMB协议(Windows文件共享)的Java库。1.x版本是较旧的版本,但可能仍有一些遗留项目在使用。
由于jcifs 1.x已经很老了,我们可能需要查找一些历史文档或示例。以下是一个典型的使用示例,包括如何通过jcifs访问共享文件:
1. 添加jcifs依赖(假设使用Maven):
注意:jcifs 1.x在Maven中央仓库中可能以`jcifs`作为groupId,而2.x版本则使用`org.samba.jcifs`。对于1.x,我们可以使用如下依赖:
```xml
<dependency>
<groupId>jcifs</groupId>
<artifactId>jcifs</artifactId>
<version>1.3.17</version> <!-- 这是1.x的最后一个版本 -->
</dependency>
```
2. 示例代码:列出共享文件夹中的文件
```java
import jcifs.smb.*;
public class JcifsExample {
public static void main(String[] args) {
try {
// 设置用户认证信息(如果需要)
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("domain", "username", "password");
// 或者使用匿名登录(如果允许)
// NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("");
// 共享文件夹的URL,格式为:smb://hostname/share/path/
String path = "smb://192.168.1.100/shared/";
SmbFile dir = new SmbFile(path, auth);
// 列出共享文件夹下的文件和子文件夹
SmbFile[] files = dir.listFiles();
for (SmbFile file : files) {
System.out.println(file.getName());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
3. 示例代码:上传文件到共享
```java
import jcifs.smb.*;
public class JcifsUploadExample {
public static void main(String[] args) {
try {
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("domain", "username", "password");
// 目标文件路径
String remoteUrl = "smb://192.168.1.100/shared/newfile.txt";
SmbFile remoteFile = new SmbFile(remoteUrl, auth);
// 本地文件路径
java.io.File localFile = new java.io.File("localfile.txt");
// 打开输入流读取本地文件
java.io.InputStream in = new java.io.FileInputStream(localFile);
// 打开输出流写入远程文件
SmbFileOutputStream out = new SmbFileOutputStream(remoteFile);
// 缓冲区
byte[] buffer = new byte[4096];
int len;
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
out.close();
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
注意:jcifs 1.x使用NTLMv1进行认证,这在现代Windows系统中可能默认被禁用(因为安全性较低)。如果需要连接较新的Windows服务器,可能需要启用NTLMv1或使用更高级的认证方式(但jcifs 1.x不支持NTLMv2)。这也是为什么后来有jcifs-ng(下一代)和jcifs2等项目的出现。
参考引用:jcifs的官方文档已经很难找到1.x版本,但我们可以参考一些历史资料或博客。例如,[jcifs官方旧网站](http://jcifs.samba.org/)可能提供了一些旧文档,但请注意该网站现在可能已经不再维护。
另外,由于jcifs 1.x已经过时,强烈建议如果可能的话升级到jcifs-ng(https://github.com/AgNO3/jcifs-ng)或jcifs2(https://github.com/codelibs/jcifs),这些项目支持NTLMv2和更现代的协议。
§§相关问题§§
1. jcifs 1.x和jcifs-ng有什么区别?
2. 如何解决jcifs 1.x连接Windows 10失败的问题?
3. jcifs是否支持SMB2或SMB3协议?</think>### jcifs 1.x 使用示例
jcifs 是一个用于实现 CIFS/SMB 网络协议(Windows 文件共享)的 Java 库。1.x 版本是经典实现,以下是核心功能的使用示例:
#### 1. 基础认证与连接
```java
import jcifs.smb.*;
public class JcifsDemo {
public static void main(String[] args) {
try {
// 设置认证信息 (域/用户名/密码)
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(
"DOMAIN", "username", "password"
);
// 访问共享文件
SmbFile file = new SmbFile("smb://192.168.1.100/SharedFolder/test.txt", auth);
System.out.println("文件大小: " + file.length() + " bytes");
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
#### 2. 目录遍历
```java
SmbFile dir = new SmbFile("smb://server/share/docs/", auth);
for (SmbFile f : dir.listFiles()) {
System.out.println(f.getName() + (f.isDirectory() ? " [DIR]" : ""));
}
```
#### 3. 文件上传
```java
try (SmbFileOutputStream out = new SmbFileOutputStream(
new SmbFile("smb://server/share/upload.txt", auth));
InputStream in = new FileInputStream("local.txt")) {
byte[] buf = new byte[4096];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
}
```
#### 4. 文件下载
```java
try (SmbFileInputStream in = new SmbFileInputStream(
new SmbFile("smb://server/share/report.xlsx", auth));
FileOutputStream out = new FileOutputStream("local.xlsx")) {
byte[] buffer = new byte[8192];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
}
```
#### 5. 认证参数说明
- **无密码访问**:`new NtlmPasswordAuthentication(null, "guest", "")`
- **IP直连**:`smb://192.168.1.100/share/`
- **域名解析**:`smb://fileserver.company.com/docs/`
#### 依赖配置 (Maven)
```xml
<dependency>
<groupId>jcifs</groupId>
<artifactId>jcifs</artifactId>
<version>1.3.17</version> <!-- 1.x最终版本 -->
</dependency>
```
> **注意**:jcifs 1.x 仅支持 SMB1 协议,现代 Windows 系统默认禁用该协议。若需连接 Windows 10+ 或 Server 2016+,需在目标机启用 SMB1 或使用 [jcifs-ng](https://github.com/AgNO3/jcifs-ng) 替代[^1]。
---
### 常见问题解决
1. **认证失败**
检查防火墙设置,确保 TCP 445 端口开放
```java
jcifs.Config.setProperty("jcifs.smb.client.responseTimeout", "30000"); // 超时设置
```
2. **编码问题**
添加 VM 参数:
```
-Djcifs.encoding=UTF-8
```
3. **性能优化**
```java
jcifs.Config.setProperty("jcifs.smb.client.disablePlainTextPasswords", "false");
jcifs.Config.setProperty("jcifs.tcpnodelay", "true");
```
---
阅读全文
相关推荐




















