Accessing Files Using Java With Samba JCIFS
This tutorial shows how to access Samba resources from Java using the JCIFS library without mounting or mapping a network drive.
重點摘要
Use codelibs JCIFS (SMB3) via Maven to access Samba from Java
Authenticate with NtlmPasswordAuthenticator and CIFSContext to list and manage protected shares
Perform file operations (list, create, delete, copy) and stream data with SmbFile and SmbFile*Stream
This article demonstrates how to access Samba (SMB) shares from Java using the codelibs JCIFS implementation (SMB3). It shows the Maven dependency to include JCIFS and walks through a test Samba server set up on an Ubuntu VirtualBox guest with a host-only adapter and a fixed IP (192.168.56.101). The guide creates two shares—an anonymous publicshare and a password-protected sambashare—configures permissions and users, and presents code examples. Examples cover the default anonymous context, authenticated context via NtlmPasswordAuthenticator and withCredentials, and common SmbFile operations: exists(), listFiles(), isDirectory(), length(), lastModified(), createNewFile(), delete(), mkdir()/mkdirs(), and copyTo(). It also explains using SmbFileInputStream/SmbFileOutputStream to transfer local files to Samba shares. The article concludes by summarizing setup, authentication, file manipulations, and stream-based I/O with JCIFS.
使用 Java 与 Samba(JCIFS)访问文件
本教程展示如何在不挂载或映射网络驱动器的情况下,使用 JCIFS 库从 Java 访问 Samba 资源。
重點摘要
通过 Maven 引入支持 SMB3 的 codelibs JCIFS
使用 NtlmPasswordAuthenticator 与 CIFSContext 进行认证并访问受保护的共享
利用 SmbFile 与 SmbFileInput/OutputStream 执行列举、创建、删除、复制和流式读写
本文介绍如何使用 codelibs JCIFS(支持 SMB3)在 Java 中访问 Samba 共享。示例在 VirtualBox 的 Ubuntu Server 虚拟机上配置 host-only 网络,并将 DHCP 地址范围限定为单一 IP(192.168.56.101),创建用户 jane,并建立两个共享:匿名的 publicshare 和需密码的 sambashare。文章包含 Maven 依赖、smb.conf 配置段与命令,并演示如何使用 SingletonContext 作为默认上下文、通过 NtlmPasswordAuthenticator 与 withCredentials 获取认证上下文以列出受保护共享内容。随后展示 SmbFile 的常见操作(exists、listFiles、isDirectory、length、lastModified、createNewFile、delete、mkdir(s) 与 copyTo),并说明使用 SmbFileInputStream/SmbFileOutputStream 将本地文件流式传输到 Samba。
使用 Java 與 Samba(JCIFS)存取檔案
本教學示範如何在不掛載或對應網路磁碟的情況下,使用 JCIFS 函式庫從 Java 存取 Samba 資源。
重點摘要
透過 Maven 引入支援 SMB3 的 codelibs JCIFS
以 NtlmPasswordAuthenticator 與 CIFSContext 進行身分驗證並存取受保護的分享
使用 SmbFile 與 SmbFileInput/OutputStream 進行列舉、建立、刪除、複製與串流讀寫
本文說明如何使用 codelibs JCIFS(支援 SMB3)在 Java 中直接存取 Samba 分享。示範環境為 VirtualBox 中的 Ubuntu Server,透過 host-only 網路並將 DHCP 範圍限制到單一 IP(192.168.56.101),建立使用者 jane 並設定兩個分享:匿名的 publicshare 與需密碼的 sambashare。文章提供 Maven 相依設定與 smb.conf 範例,並示範使用 SingletonContext 作為匿名上下文、以 NtlmPasswordAuthenticator 建立帶認證的 CIFSContext,接著以 SmbFile 完成檔案存在檢查、列出項目、檔案/資料夾屬性查詢、建立與刪除檔案或資料夾、遞迴建立目錄與 copyTo 複製分享間內容。最後介紹 SmbFileInputStream/SmbFileOutputStream 用於從本機串流讀寫到 Samba。