通过java程序从Linux服务器读取文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21132861/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:49:39  来源:igfitidea点击:

Read a file from Linux Server through java program

javalinuxfile

提问by Malik Ehtasham

I have my Java Program which is running on the windows machine. From this Window machine i need to read a file from the Linux server. I have write this code to authenticate with the Linux Server, and its working fine

我有在 Windows 机器上运行的 Java 程序。在这台 Window 机器上,我需要从 Linux 服务器读取文件。我已经编写了这段代码来与 Linux 服务器进行身份验证,并且它工作正常

session = jsch.getSession("root", "ServerIP", 22);
            session.setConfig("StrictHostKeyChecking", "no");
            session.setPassword("Passwrod");
            session.connect();
            System.out.println("Server is connect");

I can see "Server is connect" is printing on my machine that's mean authentication is done with the server. And now there is need to read the file from this server and i have write this code

我可以看到“服务器已连接”正在我的机器上打印,这意味着身份验证是通过服务器完成的。现在需要从这个服务器读取文件,我写了这段代码

try
            {
            File file = new File("/var/log//callrec/core1.log");
            LineNumberReader sc = new LineNumberReader(new FileReader(file));
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }

But its throwing file not found exception. Can any body guide me how can i solve this.

但它的抛出文件未找到异常。任何机构都可以指导我如何解决这个问题。

回答by Andreas Fester

According to http://www.jcraft.com/jsch/examples/ScpFrom.java.html, you need to execute a command on the remote side through your sessionobject, then get the input and output streams to communicate with the remote command, and read the file meta data and contents through these channels:

根据http://www.jcraft.com/jsch/examples/ScpFrom.java.html,您需要通过您的session对象在远程端执行命令,然后获取输入和输出流以与远程命令进行通信,并通过这些渠道读取文件元数据和内容:

...
String command = "scp -f /var/log/callrec/core1.log";
Channel channel = session.openChannel("exec");
((ChannelExec)channel).setCommand(command);

OutputStream out = channel.getOutputStream();
InputStream in = channel.getInputStream();

channel.connect();

// Now use in and out to read the file, 
// see http://www.jcraft.com/jsch/examples/ScpFrom.java.html for a complete example
...

回答by Grooveek

Once you're logged in via SSH, your Java code isn't magically transported in the Linux server context. So when you're using Java tools to read files, it searches the file on your local machine.

一旦您通过 SSH 登录,您的 Java 代码就不会神奇地在 Linux 服务器上下文中传输。因此,当您使用 Java 工具读取文件时,它会在您的本地机器上搜索文件。

You have then at least two possibilities :

那么你至少有两种可能性:

1/ Copying the file from the Linux server then work on it. You can better use SFTPrather than SSHif it's the only command you send (JSCH example here)

1/ 从 Linux 服务器复制文件,然后对其进行处理。您可以更好地使用SFTP而不是SSH它是您发送的唯一命令(此处为 JSCH 示例

2/ Connecting to the Linux server like you do, then streaming the file back to your machine by lauching a cat myFilecommand and reading the outputStream of the SSHsession

2/ 像您一样连接到 Linux 服务器,然后通过启动cat myFile命令并读取SSH会话的 outputStream将文件流式传输回您的机器

My vote would be for the first method, which is cleaner.

我的投票是第一种方法,它更干净。