a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 系統 > Windows > 正文

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

2019-11-28 03:51:10
字體:
來源:轉載
供稿:網友
相關知識介紹
1.1 SMB
Microsoft 網絡配置中主要采用SMB 形式實現文件共享和打印服務,SMB (服務器消息塊)是一種客戶端/ 服務器文件共享協議。IBM 于20 世紀80 年代末期開發了服務器信息塊(SMB ),用于規范共享網絡資源(如目錄、文件、打印機以及串行端口)的結構。這是一種請求/ 響應協議。與FTP 協議支持的文件共享不同,SMB 協議中的客戶端要與服務器建立長期連接。一旦建立連接,客戶端用戶就可以訪問服務器上的資源,就如同資源位于客戶端主機上一樣。

從Windows 2000 系列軟件開始,Microsoft 修改了軟件的基礎結構,使其適用SMB 協議。而在以前的Microsoft 產品中,SMB 服務需要使用非TCP/IP 協議族來執行域名解析。從Windows 2000 開始,Microsoft 的所有產品都采用DNS 系統。由此,TCP/IP 協議族可以直接支持SMB 資源共享。

SMB協議中規定了文件系統訪問和客戶如何請求文件的方式以及SMB 協議進程間通信的方式。所有的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> 大小的參數以及數據組件。
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>是一個用JAVA 開發的SMB 客戶端庫,利用jcifs 可以操作windows 共享文件,可以得到域用戶,實現單點登錄,最新版本為:1.3.12 ,官方網址:http://jcifs.samba.org/

2. 代碼實現
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") ;
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 久久久久99| 超碰偷拍 | 蜜桃久久久久久久 | 国产特级毛片 | 日韩精品一区二区在线 | 在线播放www| 久久久久久av | 欧美大片一区二区 | 国产一级视频 | 狠狠av| 在线视频 亚洲 | 男女啪网站 | 天天久久 | 亚洲国产精品成人 | 国产精品一区二区三 | 男女羞羞视频网站 | 成人一边做一边爽爽视频 | 日韩中文字幕一区 | 亚洲成人二区 | 99久久99久久 | japan国产精选videos| 中文字幕av在线播放 | 四虎影院免费看 | 国产综合精品一区二区三区 | 中文字幕高清在线 | 亚洲精品乱码久久久久久金桔影视 | 亚洲成人日韩 | 97国产精品视频人人做人人爱 | 伊人欧美在线 | av在线一区二区 | 99精品欧美一区二区三区 | 久久久久久久久久久蜜桃 | 久久久久国产一区二区三区 | 国产美女精品视频 | igao视频| 欧美日本国产 | 亚洲日韩欧美一区二区在线 | 国产成人精品免费 | 在线观看视频一区 | 欧美同性三人交 | 在线成人一区 |