JAVA遠程訪問共享目錄

1 相關知識介紹 javascript

  1.1 SMB html

  Microsoft網絡配置中主要採用SMB形式實現文件共享和打印服務,SMB(服務器消息塊)是一種客戶端/服務器文件共享協議。IBM於20世紀80年代末期開發了服務器信息塊(SMB),用於規範共享網絡資源(如目錄、文件、打印機以及串行端口)的結構。這是一種請求/響應協議。與FTP協議支持的文件共享不一樣,SMB協議中的客戶端要與服務器創建長期鏈接。一旦創建鏈接,客戶端用戶就能夠訪問服務器上的資源,就如同資源位於客戶端主機上同樣。 java

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

  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/ .net

  2. 代碼實現 server

  package uploadSMB; htm

  import java.io.BufferedInputStream; blog

  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") ;

相關文章
相關標籤/搜索