简体   繁体   English

通过Java程序从Linux Server读取文件

[英]Read a file from Linux Server through java program

I have my Java Program which is running on the windows machine. 我有在Windows机器上运行的Java程序。 From this Window machine i need to read a file from the Linux server. 我需要从这台Window机器上从Linux服务器读取文件。 I have write this code to authenticate with the Linux Server, and its working fine 我已经编写了此代码以通过Linux Server进行身份验证,并且其工作正常

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. 任何人都可以指导我如何解决这个问题。

According to http://www.jcraft.com/jsch/examples/ScpFrom.java.html , you need to execute a command on the remote side through your session object, 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
...

Once you're logged in via SSH, your Java code isn't magically transported in the Linux server context. 通过SSH登录后,就不会在Linux服务器上下文中神奇地传输Java代码。 So when you're using Java tools to read files, it searches the file on your local machine. 因此,当您使用Java工具读取文件时,它将在本地计算机上搜索文件。

You have then at least two possibilities : 然后,您至少有两种可能性:

1/ Copying the file from the Linux server then work on it. 1 /从Linux服务器复制文件,然后对其进行处理。 You can better use SFTP rather than SSH if it's the only command you send ( JSCH example here ) 如果这是您发送的唯一命令,则最好使用SFTP而不是SSH此处为JSCH示例

2/ Connecting to the Linux server like you do, then streaming the file back to your machine by lauching a cat myFile command and reading the outputStream of the SSH session 2 /像您一样连接到Linux服务器,然后通过启动cat myFile命令并读取SSH会话的outputStream将文件流式传输回您的计算机

My vote would be for the first method, which is cleaner. 我的投票赞成第一种方法,它更清洁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM