如何使用 Java JSch 库逐行读取远程文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25657603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use Java JSch library to read remote file line by line?
提问by user3898238
I am trying to use Java to read a file line by line, which is very simple (there are multiple solutions for this on stackoverflow.com), but the caveat here is that the file is located on a remote server, and it is not possible to get a local copy (it is a massive collection of millions of Amazon reviews in a single .txt file).
我正在尝试使用 Java 逐行读取文件,这非常简单(stackoverflow.com 上有多种解决方案),但这里的警告是该文件位于远程服务器上,而不是可以获取本地副本(它是单个 .txt 文件中数百万条亚马逊评论的大量集合)。
JSch comes with two example classes that copy files to and from remote hosts, namely ScpTo and ScpFrom. I am interested in reading the file from the remote host line by line; ScpFrom would try to copy the whole thing into a local file, which would take ages.
JSch 带有两个示例类,用于将文件复制到远程主机和从远程主机复制文件,即 ScpTo 和 ScpFrom。我有兴趣从远程主机逐行读取文件;ScpFrom 会尝试将整个内容复制到本地文件中,这需要很长时间。
Here is a link to ScpFrom: http://www.jcraft.com/jsch/examples/ScpFrom.java.html
这是 ScpFrom 的链接:http: //www.jcraft.com/jsch/examples/ScpFrom.java.html
I would try to cargo cult the code there and then modify it to read a remote file line by line rather than write to a local file, but most of the code is Greek to me once the author declares a byte array and starts reading bytes from the remote file. I'll admit this is something I have almost no understanding of; BufferedReader provides a much higher level interface. Essentially I want to do this: How to read a large text file line by line using Java?
我会尝试在那里加载代码,然后修改它以逐行读取远程文件而不是写入本地文件,但是一旦作者声明了一个字节数组并开始从中读取字节,大部分代码对我来说都是希腊语远程文件。我承认这是我几乎不了解的事情;BufferedReader 提供了一个更高级别的接口。基本上我想这样做:How to read a large text file line by line using Java?
except using a BufferReader that can also read remote files line by line, if provided the host name and user credentials (password, etc.), i.e. RemoteBufferReader?
除了使用还可以逐行读取远程文件的 BufferReader 之外,如果提供主机名和用户凭据(密码等),即 RemoteBufferReader?
This is the test code I've written; how do I read in the remote file line by line using JSCh?
这是我写的测试代码;如何使用 JSCh 逐行读取远程文件?
public class test2
{
static String user = "myusername";
static String host = "user@remotehost";
static String password = "mypasswd";
static String rfile = "/path/to/remote/file/on/remote/host";
public static void main(String[] args) throws FileNotFoundException, IOException, JSchException
{
JSch jsch=new JSch();
Session session=jsch.getSession(user, host, 22);
session.setPassword(password);
session.connect();
// exec 'scp -f rfile' remotely
String command="scp -f "+rfile;
Channel channel=session.openChannel("exec");
((ChannelExec)channel).setCommand(command);
// get I/O streams for remote scp
OutputStream out=channel.getOutputStream();
channel.connect()
//no idea what to do next
}
}
采纳答案by Kenster
To manipulate files through ssh, you're better off using sftp than scp or pure ssh. Jsch has built-in support for sftp. Once you've opened a session, do this to open an sftp channel:
要通过 ssh 操作文件,使用 sftp 比使用 scp 或纯 ssh 更好。Jsch 内置了对 sftp 的支持。打开会话后,请执行以下操作以打开 sftp 频道:
ChannelSftp sftp = (ChannelSftp) session.openChannel("sftp");
Once you've opened an sftp channel, there are methodsto read a remote file which let you access the file's content as an InputStream
. You can convert that to a Reader
if you need to read line-by-line:
一旦你打开了一个 sftp 通道,就可以通过一些方法来读取远程文件,让你以InputStream
. Reader
如果需要逐行阅读,可以将其转换为 a :
InputStream stream = sftp.get("/some/file");
try {
BufferedReader br = new BufferedReader(new InputStreamReader(stream));
// read from br
} finally {
stream.close();
}
Using try with resourcessyntax, your code might look more like this:
使用try with resources语法,您的代码可能看起来更像这样:
try (InputStream is = sftp.get("/some/file");
InputStreamReader isr = new InputStreamReader(stream);
BufferedReader br = new BufferedReader(isr)) {
// read from br
}
回答by Ankur jain
JSch library is the powerful library that can be used to read file from SFTP server. Below is the tested code to read file from SFTP location line by line
JSch 库是一个强大的库,可用于从 SFTP 服务器读取文件。以下是从 SFTP 位置逐行读取文件的测试代码
JSch jsch = new JSch();
Session session = null;
try {
session = jsch.getSession("user", "127.0.0.1", 22);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword("password");
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
InputStream stream = sftpChannel.get("/usr/home/testfile.txt");
try {
BufferedReader br = new BufferedReader(new InputStreamReader(stream));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException io) {
System.out.println("Exception occurred during reading file from SFTP server due to " + io.getMessage());
io.getMessage();
} catch (Exception e) {
System.out.println("Exception occurred during reading file from SFTP server due to " + e.getMessage());
e.getMessage();
}
sftpChannel.exit();
session.disconnect();
} catch (JSchException e) {
e.printStackTrace();
} catch (SftpException e) {
e.printStackTrace();
}
Please refer the blogfor whole program.
整个程序请参考博客。