SMB/CIFS 是 Windows 文件/列印共享的標準協議。Java 天然跨平台,透過 JCIFS (Samba 組織維護的純 Java 實現) 可在任何平台訪問共享資源,企業應用優勢明顯。
jcifs-1.1.9.jar) 加入專案 Build Pathtest 與檔案 response.txt,帳號 administrator,密碼 122122,IP 192.168.0.22import jcifs.smb.SmbFileInputStream;
public class ReadShareFile {
public static void main(String[] args) {
byte[] buffer = new byte[1024];
int readed;
try {
// SMB URL 格式:smb://user:password@host/share/path
SmbFileInputStream in = new SmbFileInputStream(
"smb://administrator:122122@192.168.0.22/test/response.txt");
while ((readed = in.read(buffer)) != -1) {
System.out.write(buffer, 0, readed);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
smb: 協議administrator:登入用戶名 (共享主機帳號)122122:密碼 (以冒號分隔)192.168.0.22:共享主機 IPtest:共享資料夾名response.txt:目標檔案JCIFS 提供類 java.io.File 風格 API,支援多執行緒、線程安全、豐富擴展,純 Java 無原生依賴,真正實現「一次編寫,到處訪問共享」。