I have .txt file in my remote system for eg: ip = 172.16.20.1 path is /etc/config
how do i need to connect to this file from java
here is my code.
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()));
but m getting file not exception when i use path with http & getting javax.net.ssl.SSLHandshakeException if i use https
M i missing something This file file.txt is in /etc folder (Linux)
Check this for reading a file from remote machine using commons-vfs,its working fine
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;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.