由於工作需要讀取局域網中一台機器的共享目錄中的文件,需要jcifs-1.1.11.jar的支持,使用SMB協議,以下是實現了遠程讀取文件的功能:
package junit; import jcifs.smb.SmbFile; /** * java訪問共享目錄 * * @author 林計欽 * @version 1.0 2013-7-16 上午09:18:38 */ public class SmbTest { public static void main(String[] args) throws Exception { //smb://xxx:xxx@192.168.2.188/testIndex/ //xxx:xxx是共享機器的用戶名密碼 String url="smb://192.168.2.188/testIndex/"; SmbFile file = new SmbFile(url); if(file.exists()){ SmbFile[] files = file.listFiles(); for(SmbFile f : files){ System.out.println(f.getName()); } } } }