我尝试了下面的代码,但我一直得到例外情况如下, 请让我知道我在这里缺少的东西..提前致谢...
com.jcraft.jsch.JSchException:Auth失败 在com.jcraft.jsch.Session.connect(Session.java:473) 在com.jcraft.jsch.Session.connect(Session.java:145) 在FileCopierOverNetwork.main(FileCopierOverNetwork.java:34)
示例程序:
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
public class FileCopierOverNetwork
{
public static void main(String args[])
{
String hostname = "1010.15.15";
String username = "xxxxxx";
String password = "yyyyyy";
String copyFrom = "/opt/jboss-eap-5.1/jboss-as/server/xxxdsd/test/";
String copyTo = "/opt/jboss-eap-5.1/jboss-as/server/xccxcxc/java_programs/test1/";
JSch jsch = new JSch();
Session session = null;
System.out.println("Trying to connect.....");
try
{
session = jsch.getSession(username, hostname, 22);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
//jsch.setKnownHosts("ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEA5XGH3MBKc84gKuZNVMZYrXOhZZuio3+43wVqKs22ZHrJj82zLNO9TQGLSlH47hLy32g/BDvdVJAko1SH04p/PRpdXijTf5f4xCsL+Ke3pEkZstSBu4+XPTxUDz0kNqZREtqapHGjf7YxbkRRRUdfvM+h4LWujcNO5r+U2la89xU=");
System.out.println("password before.....");
session.setPassword(password);
System.out.println("password after.....");
session.connect();
System.out.println("after connect.....");
Channel channel = session.openChannel("sftp");
ChannelSftp sftpChannel = (ChannelSftp) channel;
channel.connect();
sftpChannel.cd(".");
sftpChannel.get(copyFrom, copyTo);
sftpChannel.exit();
session.disconnect();
} catch (JSchException e) {
e.printStackTrace();
} catch (SftpException e) {
e.printStackTrace();
}
System.out.println("Done !!");
}
}