java 操作windows 共享目錄方法介紹
發(fā)布時(shí)間:2012-11-25 18:08:59 作者:佚名
我要評論

用JAVA 開發(fā)的SMB 客戶端庫,利用jcifs 可以操作windows 共享文件,可以得到域用戶,實(shí)現(xiàn)單點(diǎn)登錄,需要的朋友可以參考下
相關(guān)知識介紹
1.1 SMB
Microsoft 網(wǎng)絡(luò)配置中主要采用SMB 形式實(shí)現(xiàn)文件共享和打印服務(wù),SMB (服務(wù)器消息塊)是一種客戶端/ 服務(wù)器文件共享協(xié)議。IBM 于20 世紀(jì)80 年代末期開發(fā)了服務(wù)器信息塊(SMB ),用于規(guī)范共享網(wǎng)絡(luò)資源(如目錄、文件、打印機(jī)以及串行端口)的結(jié)構(gòu)。這是一種請求/ 響應(yīng)協(xié)議。與FTP 協(xié)議支持的文件共享不同,SMB 協(xié)議中的客戶端要與服務(wù)器建立長期連接。一旦建立連接,客戶端用戶就可以訪問服務(wù)器上的資源,就如同資源位于客戶端主機(jī)上一樣。
從Windows 2000 系列軟件開始,Microsoft 修改了軟件的基礎(chǔ)結(jié)構(gòu),使其適用SMB 協(xié)議。而在以前的Microsoft 產(chǎn)品中,SMB 服務(wù)需要使用非TCP/IP 協(xié)議族來執(zhí)行域名解析。從Windows 2000 開始,Microsoft 的所有產(chǎn)品都采用DNS 系統(tǒng)。由此,TCP/IP 協(xié)議族可以直接支持SMB 資源共享。
SMB協(xié)議中規(guī)定了文件系統(tǒng)訪問和客戶如何請求文件的方式以及SMB 協(xié)議進(jìn)程間通信的方式。所有的SMB 消息都采用一種格式。該格式采用固定大小的文件頭,后跟可變 <script src="http://hi.images.csdn.net/js/blog/tiny_mce/themes/advanced/langs/zh.js" type="text/javascript"></script><script src="http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js" type="text/javascript"></script> 大小的參數(shù)以及數(shù)據(jù)組件。
1.2 jcifs
Jcifs <script src="http://hi.images.csdn.net/js/blog/tiny_mce/themes/advanced/langs/zh.js" type="text/javascript"></script><script src="http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js" type="text/javascript"></script> pan>是一個(gè)用JAVA 開發(fā)的SMB 客戶端庫,利用jcifs 可以操作windows 共享文件,可以得到域用戶,實(shí)現(xiàn)單點(diǎn)登錄,最新版本為:1.3.12 ,官方網(wǎng)址:http://jcifs.samba.org/
2. 代碼實(shí)現(xiàn)
Java代碼
package uploadSMB;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import jcifs.smb.SmbFileOutputStream;
public class UploadDownloadUtil {
/**
* Description: 從共享目錄拷貝文件到本地
* @Version1.0 Sep 25, 2009 3:48:38 PM
* @param remoteUrl 共享目錄上的文件路徑
* @param localDir 本地目錄
*/
public void smbGet(String remoteUrl,String localDir) {
InputStream in = null;
OutputStream out = null;
try {
SmbFile remoteFile = new SmbFile(remoteUrl);
if(remoteFile==null){
System.out.println("共享文件不存在");
return;
}
String fileName = remoteFile.getName();
File localFile = new File(localDir+File.separator+fileName);
in = new BufferedInputStream(new SmbFileInputStream(remoteFile));
out = new BufferedOutputStream(new FileOutputStream(localFile));
byte[] buffer = new byte[1024];
while(in.read(buffer)!=-1){
out.write(buffer);
buffer = new byte[1024];
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* Description: 從本地上傳文件到共享目錄
* @Version1.0 Sep 25, 2009 3:49:00 PM
* @param remoteUrl 共享文件目錄
* @param localFilePath 本地文件絕對路徑
*/
public void smbPut(String remoteUrl,String localFilePath) {
InputStream in = null;
OutputStream out = null;
try {
File localFile = new File(localFilePath);
String fileName = localFile.getName();
SmbFile remoteFile = new SmbFile(remoteUrl+"/"+fileName);
in = new BufferedInputStream(new FileInputStream(localFile));
out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));
byte[] buffer = new byte[1024];
while(in.read(buffer)!=-1){
out.write(buffer);
buffer = new byte[1024];
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args){
UploadDownloadUtil test = new UploadDownloadUtil() ;
// smb:域名;用戶名:密碼@目的IP/文件夾/文件名.xxx
//test.smbGet("smb://szpcg;jiang.t:xxx@192.168.193.13/Jake/test.txt", "c://") ;
test.smbPut("smb://szpcg;jiang.t:xxx@192.168.193.13/Jake", "c://test.txt") ;
}
}
package uploadSMB;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import jcifs.smb.SmbFileOutputStream;
public class UploadDownloadUtil {
/**
* Description: 從共享目錄拷貝文件到本地
* @Version1.0 Sep 25, 2009 3:48:38 PM
* @param remoteUrl 共享目錄上的文件路徑
* @param localDir 本地目錄
*/
public void smbGet(String remoteUrl,String localDir) {
InputStream in = null;
OutputStream out = null;
try {
SmbFile remoteFile = new SmbFile(remoteUrl);
if(remoteFile==null){
System.out.println("共享文件不存在");
return;
}
String fileName = remoteFile.getName();
File localFile = new File(localDir+File.separator+fileName);
in = new BufferedInputStream(new SmbFileInputStream(remoteFile));
out = new BufferedOutputStream(new FileOutputStream(localFile));
byte[] buffer = new byte[1024];
while(in.read(buffer)!=-1){
out.write(buffer);
buffer = new byte[1024];
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* Description: 從本地上傳文件到共享目錄
* @Version1.0 Sep 25, 2009 3:49:00 PM
* @param remoteUrl 共享文件目錄
* @param localFilePath 本地文件絕對路徑
*/
public void smbPut(String remoteUrl,String localFilePath) {
InputStream in = null;
OutputStream out = null;
try {
File localFile = new File(localFilePath);
String fileName = localFile.getName();
SmbFile remoteFile = new SmbFile(remoteUrl+"/"+fileName);
in = new BufferedInputStream(new FileInputStream(localFile));
out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));
byte[] buffer = new byte[1024];
while(in.read(buffer)!=-1){
out.write(buffer);
buffer = new byte[1024];
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args){
UploadDownloadUtil test = new UploadDownloadUtil() ;
// smb:域名;用戶名:密碼@目的IP/文件夾/文件名.xxx
//test.smbGet("smb://szpcg;jiang.t:xxx@192.168.193.13/Jake/test.txt", "c://") ;
test.smbPut("smb://szpcg;jiang.t:xxx@192.168.193.13/Jake", "c://test.txt") ;
}
}
2.3 remoteUrl說明
remoteUrl 如何填寫是值得注意的
如果是無需密碼的共享,則類似如下格式:
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
//test.smbGet("smb://szpcg;jiang.t:xxx@192.168.193.13/Jake/test.txt", "c://") ;
test.smbPut("smb://szpcg;jiang.t:xxx@192.168.193.13/Jake", "c://test.txt") ;
1.1 SMB
Microsoft 網(wǎng)絡(luò)配置中主要采用SMB 形式實(shí)現(xiàn)文件共享和打印服務(wù),SMB (服務(wù)器消息塊)是一種客戶端/ 服務(wù)器文件共享協(xié)議。IBM 于20 世紀(jì)80 年代末期開發(fā)了服務(wù)器信息塊(SMB ),用于規(guī)范共享網(wǎng)絡(luò)資源(如目錄、文件、打印機(jī)以及串行端口)的結(jié)構(gòu)。這是一種請求/ 響應(yīng)協(xié)議。與FTP 協(xié)議支持的文件共享不同,SMB 協(xié)議中的客戶端要與服務(wù)器建立長期連接。一旦建立連接,客戶端用戶就可以訪問服務(wù)器上的資源,就如同資源位于客戶端主機(jī)上一樣。
從Windows 2000 系列軟件開始,Microsoft 修改了軟件的基礎(chǔ)結(jié)構(gòu),使其適用SMB 協(xié)議。而在以前的Microsoft 產(chǎn)品中,SMB 服務(wù)需要使用非TCP/IP 協(xié)議族來執(zhí)行域名解析。從Windows 2000 開始,Microsoft 的所有產(chǎn)品都采用DNS 系統(tǒng)。由此,TCP/IP 協(xié)議族可以直接支持SMB 資源共享。
SMB協(xié)議中規(guī)定了文件系統(tǒng)訪問和客戶如何請求文件的方式以及SMB 協(xié)議進(jìn)程間通信的方式。所有的SMB 消息都采用一種格式。該格式采用固定大小的文件頭,后跟可變 <script src="http://hi.images.csdn.net/js/blog/tiny_mce/themes/advanced/langs/zh.js" type="text/javascript"></script><script src="http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js" type="text/javascript"></script> 大小的參數(shù)以及數(shù)據(jù)組件。
1.2 jcifs
Jcifs <script src="http://hi.images.csdn.net/js/blog/tiny_mce/themes/advanced/langs/zh.js" type="text/javascript"></script><script src="http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js" type="text/javascript"></script> pan>是一個(gè)用JAVA 開發(fā)的SMB 客戶端庫,利用jcifs 可以操作windows 共享文件,可以得到域用戶,實(shí)現(xiàn)單點(diǎn)登錄,最新版本為:1.3.12 ,官方網(wǎng)址:http://jcifs.samba.org/
2. 代碼實(shí)現(xiàn)
Java代碼
復(fù)制代碼
代碼如下:package uploadSMB;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import jcifs.smb.SmbFileOutputStream;
public class UploadDownloadUtil {
/**
* Description: 從共享目錄拷貝文件到本地
* @Version1.0 Sep 25, 2009 3:48:38 PM
* @param remoteUrl 共享目錄上的文件路徑
* @param localDir 本地目錄
*/
public void smbGet(String remoteUrl,String localDir) {
InputStream in = null;
OutputStream out = null;
try {
SmbFile remoteFile = new SmbFile(remoteUrl);
if(remoteFile==null){
System.out.println("共享文件不存在");
return;
}
String fileName = remoteFile.getName();
File localFile = new File(localDir+File.separator+fileName);
in = new BufferedInputStream(new SmbFileInputStream(remoteFile));
out = new BufferedOutputStream(new FileOutputStream(localFile));
byte[] buffer = new byte[1024];
while(in.read(buffer)!=-1){
out.write(buffer);
buffer = new byte[1024];
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* Description: 從本地上傳文件到共享目錄
* @Version1.0 Sep 25, 2009 3:49:00 PM
* @param remoteUrl 共享文件目錄
* @param localFilePath 本地文件絕對路徑
*/
public void smbPut(String remoteUrl,String localFilePath) {
InputStream in = null;
OutputStream out = null;
try {
File localFile = new File(localFilePath);
String fileName = localFile.getName();
SmbFile remoteFile = new SmbFile(remoteUrl+"/"+fileName);
in = new BufferedInputStream(new FileInputStream(localFile));
out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));
byte[] buffer = new byte[1024];
while(in.read(buffer)!=-1){
out.write(buffer);
buffer = new byte[1024];
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args){
UploadDownloadUtil test = new UploadDownloadUtil() ;
// smb:域名;用戶名:密碼@目的IP/文件夾/文件名.xxx
//test.smbGet("smb://szpcg;jiang.t:xxx@192.168.193.13/Jake/test.txt", "c://") ;
test.smbPut("smb://szpcg;jiang.t:xxx@192.168.193.13/Jake", "c://test.txt") ;
}
}
package uploadSMB;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import jcifs.smb.SmbFileOutputStream;
public class UploadDownloadUtil {
/**
* Description: 從共享目錄拷貝文件到本地
* @Version1.0 Sep 25, 2009 3:48:38 PM
* @param remoteUrl 共享目錄上的文件路徑
* @param localDir 本地目錄
*/
public void smbGet(String remoteUrl,String localDir) {
InputStream in = null;
OutputStream out = null;
try {
SmbFile remoteFile = new SmbFile(remoteUrl);
if(remoteFile==null){
System.out.println("共享文件不存在");
return;
}
String fileName = remoteFile.getName();
File localFile = new File(localDir+File.separator+fileName);
in = new BufferedInputStream(new SmbFileInputStream(remoteFile));
out = new BufferedOutputStream(new FileOutputStream(localFile));
byte[] buffer = new byte[1024];
while(in.read(buffer)!=-1){
out.write(buffer);
buffer = new byte[1024];
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* Description: 從本地上傳文件到共享目錄
* @Version1.0 Sep 25, 2009 3:49:00 PM
* @param remoteUrl 共享文件目錄
* @param localFilePath 本地文件絕對路徑
*/
public void smbPut(String remoteUrl,String localFilePath) {
InputStream in = null;
OutputStream out = null;
try {
File localFile = new File(localFilePath);
String fileName = localFile.getName();
SmbFile remoteFile = new SmbFile(remoteUrl+"/"+fileName);
in = new BufferedInputStream(new FileInputStream(localFile));
out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));
byte[] buffer = new byte[1024];
while(in.read(buffer)!=-1){
out.write(buffer);
buffer = new byte[1024];
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args){
UploadDownloadUtil test = new UploadDownloadUtil() ;
// smb:域名;用戶名:密碼@目的IP/文件夾/文件名.xxx
//test.smbGet("smb://szpcg;jiang.t:xxx@192.168.193.13/Jake/test.txt", "c://") ;
test.smbPut("smb://szpcg;jiang.t:xxx@192.168.193.13/Jake", "c://test.txt") ;
}
}
2.3 remoteUrl說明
remoteUrl 如何填寫是值得注意的
如果是無需密碼的共享,則類似如下格式:
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
//test.smbGet("smb://szpcg;jiang.t:xxx@192.168.193.13/Jake/test.txt", "c://") ;
test.smbPut("smb://szpcg;jiang.t:xxx@192.168.193.13/Jake", "c://test.txt") ;
相關(guān)文章
電腦如何清除運(yùn)行窗口歷史記錄? Windows系統(tǒng)清除運(yùn)行窗口歷史記錄的技
winodws的命令行界面是一個(gè)不錯(cuò)的快捷方式,我們只需要通過"win+r"就可以調(diào)用運(yùn)行命令,隨著時(shí)間的累積,我們這里會(huì)積累下不少歷史命令,有泄露隱私的風(fēng)險(xiǎn),所以2025-04-17電腦鎖屏壁紙?jiān)趺垂潭ú蛔? Windows系統(tǒng)鎖定桌面背景圖的方法
最近就有不少用戶想要鎖定電腦的桌面背景圖,但不清楚具體如何操作,其實(shí)方法非常簡單,詳細(xì)請看下文介紹2025-04-17怎么關(guān)閉windows索引器? Windows系統(tǒng)禁用索引器回退功能的技巧
使用Windows系統(tǒng)中的搜索功能都是在索引功能的基礎(chǔ)上建立的,因此,若想要加快搜索索引速度,就要把系統(tǒng)中的索引器回退功能設(shè)置為禁用,詳細(xì)請看下文介紹2025-04-17微軟應(yīng)用商店無法打開怎么辦? 0x80248014錯(cuò)誤的原因分析與解決方案
近有不少小伙伴反映,Windows 10自帶的微軟商店怎么都打不開,顯示0x80248014錯(cuò)誤錯(cuò)誤代碼,下面我們就來看看詳細(xì)解決方案2025-04-16Windows系統(tǒng)如何關(guān)閉自動(dòng)維護(hù)功能? 關(guān)閉自動(dòng)維護(hù)輕松提升電腦性能
最近有不少用戶想關(guān)閉其中的自動(dòng)維護(hù)功能,但不清楚具體如何操作,下面我們就來看看Windows系統(tǒng)關(guān)閉自動(dòng)維護(hù)功能的操作方法2025-04-15微軟發(fā)布多個(gè) OOB 更新: 用于修復(fù) Active Directory 組策略 Bug
微軟發(fā)布多個(gè) OOB 更新用于修復(fù) Active Directory 組策略 Bug,該問題具體表現(xiàn)為:當(dāng)設(shè)備已啟用 "審核登錄 / 注銷事件" 功能且實(shí)際生效時(shí),本地組策略編輯器或本2025-04-14定期掃描病毒功能有什么用? Windows系統(tǒng)開啟定期掃描病毒功能的技巧
定期掃描病毒功能可以很好的幫助我們電腦定義清理病毒,很多小伙伴都不知道在那里設(shè)置進(jìn)行使用,該怎么開啟這個(gè)功能呢?詳細(xì)請看下文介紹2025-04-01Windows如何設(shè)置麥克風(fēng)增強(qiáng)? Windows系統(tǒng)麥克風(fēng)權(quán)限開啟全攻略
麥克風(fēng)功能作為日常溝通和娛樂的重要工具,其表現(xiàn)尤為關(guān)鍵,然而,不少windows用戶發(fā)現(xiàn)麥克風(fēng)的聲音偏小,影響了使用體驗(yàn),下面我們就來看看Windows系統(tǒng)設(shè)置麥克風(fēng)增強(qiáng)功能2025-04-01提升性能30%! Windows系統(tǒng)關(guān)閉VBS的簡單方法
關(guān)閉VBS功能可以釋放部分系統(tǒng)資源,提高系統(tǒng)性能和游戲流暢度,windows系統(tǒng)該怎么關(guān)閉vbs功能呢?下面我們就來看看詳細(xì)教程2025-04-01Windows Server 2025安裝 Hyper-V Docker 容器的圖文教程
Hyper-V 容器是 Windows Server 2025 中的一項(xiàng)強(qiáng)大功能,今天我們就來介紹如何在 Windows Server 2025 上安裝、配置和運(yùn)行容器主機(jī),并下載運(yùn)行一個(gè) IIS 容器2025-03-27