2016-10-03 11 views
-1

を書く私は例えばのための私のリモートシステムで.txtファイルがあります:私は、Javaリモートファイル読み取りと操作

からこのファイルに接続する必要がありますどのようにIP = 172.16.20.1 パスは、/ etcある/ configに

ここは私のコードです。

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

が、私は、HTTP &がjavax.net.ssl.SSLHandshakeExceptionを取得してパスを使用する場合、私はhttpsを使用する場合

M私は、このファイルfile.txtは何かが欠けているが、/ etcフォルダにあるファイルではない例外を取得メートル(Linux)の

+0

ゲットファイルが見つからない例外 –

+0

java.net.ConnectExceptionを取得中:私がFTPプロトコルを使用している場合、接続が拒否されました –

答えて

0

コモンズ-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; 
} 

}

関連する問題