選中此使用的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;
}
}
M geeting文件未找到異常 –
獲取java.net.ConnectException:連接被拒絕,如果我使用FTP協議 –