JAVA遠端存取共用目錄

來源:互聯網
上載者:User

 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.111cn.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. 代碼實現

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.