[英]Multithreaded read & write of a file , cannot stop operation, writes bad file
[英]Remote File Read & Write operation
我的远程系统中有 .txt 文件,例如:ip = 172.16.20.1 路径是 /etc/config
我需要如何从 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()));
但是当我使用带有 http 的路径时获取文件不是异常,如果我使用 https 获取 javax.net.ssl.SSLHandshakeException
我遗漏了一些文件 file.txt 在 /etc 文件夹中 (Linux)
检查这个以使用 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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.