国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 常見(jiàn)問(wèn)題 > 正文

java 操作windows 共享目錄方法介紹

2019-12-24 02:46:46
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
相關(guān)知識(shí)介紹 
1.1 SMB 
Microsoft 網(wǎng)絡(luò)配置中主要采用SMB 形式實(shí)現(xiàn)文件共享和打印服務(wù),SMB (服務(wù)器消息塊)是一種客戶端/ 服務(wù)器文件共享協(xié)議。IBM 于20 世紀(jì)80 年代末期開(kāi)發(fā)了服務(wù)器信息塊(SMB ),用于規(guī)范共享網(wǎng)絡(luò)資源(如目錄、文件、打印機(jī)以及串行端口)的結(jié)構(gòu)。這是一種請(qǐng)求/ 響應(yīng)協(xié)議。與FTP 協(xié)議支持的文件共享不同,SMB 協(xié)議中的客戶端要與服務(wù)器建立長(zhǎng)期連接。一旦建立連接,客戶端用戶就可以訪問(wèn)服務(wù)器上的資源,就如同資源位于客戶端主機(jī)上一樣。 

從Windows 2000 系列軟件開(kāi)始,Microsoft 修改了軟件的基礎(chǔ)結(jié)構(gòu),使其適用SMB 協(xié)議。而在以前的Microsoft 產(chǎn)品中,SMB 服務(wù)需要使用非TCP/IP 協(xié)議族來(lái)執(zhí)行域名解析。從Windows 2000 開(kāi)始,Microsoft 的所有產(chǎn)品都采用DNS 系統(tǒng)。由此,TCP/IP 協(xié)議族可以直接支持SMB 資源共享。

SMB協(xié)議中規(guī)定了文件系統(tǒng)訪問(wèn)和客戶如何請(qǐ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 開(kāi)發(fā)的SMB 客戶端庫(kù),利用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 本地文件絕對(duì)路徑 
*/ 
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 本地文件絕對(duì)路徑 
*/ 
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說(shuō)明 
remoteUrl 如何填寫(xiě)是值得注意的 
如果是無(wú)需密碼的共享,則類(lèi)似如下格式: 
smb://ip/sharefolder (例如:smb://192.168.0.77/test ) 
如果需要用戶名、密碼,則類(lèi)似如下格式: 
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") ;
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 利川市| 昌邑市| 藁城市| 庆阳市| 泸溪县| 陈巴尔虎旗| 剑川县| 山阳县| 定南县| 松桃| 浑源县| 金湖县| 周口市| 广安市| 黔南| 栖霞市| 浑源县| 钟祥市| 寿阳县| 报价| 巨鹿县| 革吉县| 凤台县| 呈贡县| 东平县| 平凉市| 白山市| 美姑县| 隆德县| 山阴县| 吉林省| 南漳县| 荣昌县| 平江县| 镇赉县| 西充县| 海淀区| 淮安市| 高台县| 泾阳县| 城固县|