我想从Linux服务器上通过Java程序远程访问文件。我已经编写了此代码,但无法正常工作。
public static void main(String arg[]) throws FileNotFoundException
{
JSch jsch = new JSch();
Session session = null;
BufferedReader br = null;
try {
session = jsch.getSession("root", "IPServer", 22);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword("Password");
session.connect();
System.out.println("Server is connect");
try
{
String command = "scp -f /var/log/callrec/core1.log";
Channel channel = session.openChannel("exec");
((ChannelExec)channel).setCommand(command);
OutputStream out = channel.getOutputStream();
InputStream in = (InputStream) channel.getInputStream();
channel.connect();
br = new BufferedReader(new InputStreamReader(in));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
System.out.println("\nDone!");
}
catch(Exception e)
{
e.printStackTrace();
}
session.disconnect();
} catch (JSchException e) {
e.printStackTrace();
// } catch (SftpException e) {
// e.printStackTrace();
}
}
我的程序没有抛出任何错误,我可以在控制台上看到“服务器已连接”消息,然后挂起,没有给出任何其他输出,并且它没有终止。