java訪問遠程共享文件

轉載自:http://hu-bj.javaeye.com/blog/327198 
使用開源庫JCIFS 
http://jcifs.samba.org/ 
  Microsoft使用NetBIOS實現了一個網絡文件/打印服 
務系統,這個系統基於NetBIOS設定了一套文件共享協議,Microsoft稱之爲SMB( 
Server Message Block)協議。這個協議被Microsoft用於它們Lan Manager和Wi 
ndows NT服務器系統中,而Windows系統均包括這個協議的客戶軟件,因而這個協 
議在局域網系統中影響很大。 
  隨着Internet的流行,Microsoft希望將這個協議擴展到Internet上去,成爲 
Inter net上計算機之間相互共享數據的一種標準。因此它將原有的幾乎沒有多少 
技術文檔的SMB協議進行整理,重新命名爲 CIFS(Common Internet File Syste 
m),並打算將它與NetBIOS相脫離,試圖使它成爲Internet上的一個標準協議。 
  jcifs是CIFS在JAVA中的一個實現,是samba組織負責維護開發的一個開源項目, 
專注於使用java語言對cifs協議的設計和實現。他們將jcifs設計成爲一個完整的,豐 
富的,具有可擴展能力且線程安全的客戶端庫。這一庫可以應用於各種java虛擬機訪 
問遵循CIFS/SMB網絡傳輸協議的網絡資源。 
示例: 
import java.io.BufferedInputStream; 
import java.io.BufferedOutputStream; 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.util.Date; 
import jcifs.smb.SmbFile; 
import jcifs.smb.SmbFileInputStream; 
public class RemoteShareFile { 
      public static void main(String[] args) { 
          String smbMachine="smb://用戶名:密碼@IP地址/目錄/文件名"; 
          String localPath="D://"; 
          File file=readFromSmb(smbMachine,localPath); 
      } 
      public static File readFromSmb(String smbMachine,String localpath){ 
          File localfile=null; 
          InputStream bis=null; 
          OutputStream bos=null; 
          try { 
              SmbFile rmifile = new SmbFile(smbMachine); 
              String filename=rmifile.getName(); 
              bis=new BufferedInputStream(new SmbFileInputStream(rmifile)); 
              localfile=new File(localpath+File.separator+filename); 
              bos=new BufferedOutputStream(new FileOutputStream(localfile)); 
              int length=rmifile.getContentLength(); 
              byte[] buffer=new byte[length]; 
              Date date=new Date(); 
              bis.read(buffer); 
              bos.write(buffer); 
              Date end=new Date(); 
              int time= (int) ((end.getTime()-date.getTime())/1000); 
              if(time>0) 
                  System.out.println("用時:"+time+"秒 "+"速度:"+length/time/1024+"kb/秒");          
          } catch (Exception e){ 
              System.out.println(e.getMessage()); 
          }finally{ 
              try { 
                  bos.close(); 
                  bis.close(); 
              } catch (IOException e) { 
                  e.printStackTrace(); 
              } 
          } 
          return localfile; 
     } 

}

jcifs 項目地址 http://jcifs.samba.org/ 

import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;

public class ReadShareFile {

	public static void main(String[] args) {

		try {
			SmbFile smbFile = new SmbFile(
					"smb://test:[email protected]/out/test.txt");
			int length = smbFile.getContentLength();// 得到文件的大小
			byte buffer[] = new byte[length];
			SmbFileInputStream in = new SmbFileInputStream(smbFile);
			// 建立smb文件輸入流
			while ((in.read(buffer)) != -1) {

				System.out.write(buffer);
				System.out.println(buffer.length);
			}
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

例2:

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;

import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
  
public class TestReadSmb {   
    public static void main(String[] args){   
            String smbMachine="smb://test:[email protected]/temp/test.txt";
    	
            String localPath="D:\\temp";   
            File file=readFromSmb(smbMachine,localPath);   
            removeFile(file);   
    }   
  
    /** ***  
     * 從smbMachine讀取文件並存儲到localpath指定的路徑  
     *   
     * @param smbMachine  
     *            共享機器的文件,如smb://xxx:[email protected]/myDocument/測試文本.txt,xxx:xxx是共享機器的用戶名密碼  
     * @param localpath  
     *            本地路徑  
     * @return  
     */  
    public static File readFromSmb(String smbMachine,String localpath){   
        File localfile=null;   
        InputStream bis=null;   
        OutputStream bos=null;   
        try{   
            SmbFile rmifile = new SmbFile(smbMachine);   
            String filename=rmifile.getName();   
            bis=new BufferedInputStream(new SmbFileInputStream(rmifile));   
            localfile=new File(localpath+File.separator+filename);  
            System.out.println("localfile=="+localfile);
            bos=new BufferedOutputStream(new FileOutputStream(localfile));   
            int length=rmifile.getContentLength();  
            System.out.println("length=="+length);
            byte[] buffer=new byte[length];   
            Date date=new Date();   
            bis.read(buffer);  
            bos.write(buffer); 

            Date end=new Date();   
            int time= (int) ((end.getTime()-date.getTime())/1000);   
            if(time>0)   
                System.out.println("用時:"+time+"秒 "+"速度:"+length/time/1024+"kb/秒");               
        } catch (Exception e){    
            System.out.println(e.getMessage());   
               
        }finally{   
            try {   
                bos.close();   
                bis.close();   
            } catch (IOException e) {   
                e.printStackTrace();   
            }               
        }   
        return localfile;   
    }   
    public static boolean removeFile(File file) {   
        return file.delete();   
    }   
}  


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章