2016-10-03 47 views
-1

我在遠程系統.txt文件的如:IP = 172.16.20.1 路徑是/ etc /配置遠程文件讀取和寫入操作

怎麼辦,我需要從Java

連接到這個文件

這是我的代碼。

String path = "http://172.16.20.1/etc/config/file.txt"; 

URL url = new URL(path); 
URLConnection yc = url.openConnection(); 
BufferedReader br = new BufferedReader(new InputStreamReader (yc.getInputStream())); 

但m到處文件也不例外,當我如果我使用HTTPS

Mi個失去了一些東西該文件file.txt的是/ etc文件夾使用路徑以http &越來越javax.net.ssl.SSLHandshakeException (Linux)的

+0

M geeting文件未找到異常 –

+0

獲取java.net.ConnectException:連接被拒絕,如果我使用FTP協議 –

回答

0

選中此使用的commons-VFS從遠程計算機讀取文件,它的做工精細

public static void readRemoteManifestFile(String ipAddress,String filePath,String username,String password){ 

    //filePath="/usr/local/tomcat/webapps/abc/test/NavigationPanel.html"; 

    try { 

     StandardFileSystemManager manager = new StandardFileSystemManager(); 
     manager.init(); 

     FileObject remoteFile = manager.resolveFile(createConnectionString(ipAddress, username,password, 
       filePath), createDefaultOptions()); 
     if(!remoteFile.exists()){ 
      System.out.println(filePath+": no such file"); 
     }else{ 
      Reader  inputStreamReader = new InputStreamReader( remoteFile.getContent().getInputStream()); 
      char c; 
       int i; 
      while((i=inputStreamReader.read())!=-1) 
      { 
       // int to character 
       c=(char)i;    
       // print char 
       System.out.println("Character Read: "+c); 
      } 
     } 

    } catch (Exception e) { 
     System.out.println("Failed to read for "+ 
       filePath+": "+e); 
     System.out.println("Failed to read for "+ 
       filePath+": "+e.getMessage()); 

    } 


} 
public static String createConnectionString(String hostName, 
     String username, String password, String remoteFilePath) { 
    return "sftp://" + username + ":" + password + "@" + hostName + "/" + remoteFilePath; 
} 

public static FileSystemOptions createDefaultOptions() 
     throws FileSystemException { 
    FileSystemOptions opts = new FileSystemOptions(); 
    SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(
      opts, "no"); 
    SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, false); 
    SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000); 
    return opts; 
} 

}