【轉載】通過java的jcifs類庫訪問網路位置共用檔案[代碼]

來源:互聯網
上載者:User

轉載自笨笨熊的BLOG:http://www.mkv8.com/?p=42

 

以下是通過java的jcifs類庫,訪問網路位置上的共用檔案代碼。
相關類庫:http://www.mkv8.com/?p=48

  1 public class UploadDownloadUtil
  2 {
  3 
  4  /**
  5   * 從共用目錄拷貝檔案到本地
  6   * @param remoteUrl 共用目錄上的檔案路徑
  7   * @param localDir 本地目錄
  8   */
  9  public void smbGet(String remoteUrl, String localDir)
 10  {
 11   InputStream in = null;
 12   OutputStream out = null;
 13   try
 14   {
 15    SmbFile remoteFile = new SmbFile(remoteUrl);
 16    //這一句很重要
 17    remoteFile.connect();
 18    if (remoteFile == null)
 19    {
 20     System.out.println("共用檔案不存在");
 21     return;
 22    }
 23    String fileName = remoteFile.getName();
 24    File localFile = new File(localDir + File.separator + fileName);
 25    in = new BufferedInputStream(new SmbFileInputStream(remoteFile));
 26    out = new BufferedOutputStream(new FileOutputStream(localFile));
 27    byte[] buffer = new byte[1024];
 28    while (in.read(buffer) != -1)
 29    {
 30     out.write(buffer);
 31     buffer = new byte[1024];
 32    }
 33   }
 34   catch (Exception e)
 35   {
 36    e.printStackTrace();
 37   }
 38   finally
 39   {
 40    try
 41    {
 42     out.close();
 43     in.close();
 44    }
 45    catch (IOException e)
 46    {
 47     e.printStackTrace();
 48    }
 49   }
 50  }
 51 
 52  /**
 53   * 從本地上傳檔案到共用目錄
 54   * @Version1.0 Sep 25, 2009 3:49:00 PM
 55   * @param remoteUrl 共用檔案目錄
 56   * @param localFilePath 本地檔案絕對路徑
 57   */
 58  public void smbPut(String remoteUrl, String localFilePath)
 59  {
 60   InputStream in = null;
 61   OutputStream out = null;
 62   try
 63   {
 64    File localFile = new File(localFilePath);
 65 
 66    String fileName = localFile.getName();
 67    SmbFile remoteFile = new SmbFile(remoteUrl + "/" + fileName);
 68    in = new BufferedInputStream(new FileInputStream(localFile));
 69    out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));
 70    byte[] buffer = new byte[1024];
 71    while (in.read(buffer) != -1)
 72    {
 73     out.write(buffer);
 74     buffer = new byte[1024];
 75    }
 76   }
 77   catch (Exception e)
 78   {
 79    e.printStackTrace();
 80   }
 81   finally
 82   {
 83    try
 84    {
 85     out.close();
 86     in.close();
 87    }
 88    catch (IOException e)
 89    {
 90     e.printStackTrace();
 91    }
 92   }
 93  }
 94 
 95  public static void main(String[] args)
 96  {
 97   UploadDownloadUtil test = new UploadDownloadUtil();
 98   // smb:網域名稱;使用者名稱:密碼@目的IP/檔案夾/檔案名稱.xxx
 99   // test.smbGet("smb://szpcg;jiang.t:xxx@192.168.193.13/Jake/test.txt",
100   // "c://") ;
101   
102 //  test.smbPut("smb://szpcg;jiang.t:xxx@192.168.193.13/Jake",
103 //    "c://test.txt");
104   
105   
106   //使用者名稱密碼不能有強字元,也就是不能有特殊字元,否則會被作為分斷處理
107   test.smbGet("smb://CHINA;xieruilin:123456Xrl@10.70.36.121/project/report/網上問題智能分析助手使用文檔.doc",
108   "c://Temp/");
109 
110  }
111 
112 }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.