【发布时间】:2015-08-24 01:24:26
【问题描述】:
我在尝试执行时收到“INFO:远程计算机已断开连接:协议错误:数据包太长:65580”错误:
sUpload("server.host.com", "username", "/home/localuser/.ssh/id_rsa", "filename", "");
文件不会传输到 SFTP 服务器(在另一端仅创建零字节)。当通过 Unix shell 手动 SFTP 时,一切正常。我在网上看到它可能是 SftpClient 中的 BLOCK_SIZE 的问题,但首先我找不到改变大小的设置方法,其次,默认值似乎是 65535,这完全不能解释错误中的 65580 值消息。
有什么想法吗?
我有以下使用 Java Ssh 工具 (j2ssh-core-0.2.9.jar) 的实用方法:
private static void sUpload(String ftpServer, String user, String password,
String localFile, String remoteFile) throws IOException {
String methodName = "sUpload: ";
System.out.println(" START ftpServer="+ftpServer+" user="+user+" password="+password);
int result = 0;
System.out.println("ftpServer " + ftpServer);
System.out.println("user " + user);
System.out.println("password " + password);
System.out.println("localFile " + localFile);
System.out.println("remoteFile " + remoteFile);
SshClient ssh = new SshClient();
ssh.connect(ftpServer);
System.out.println("Server Connected ");
if (password.contains("\\") || password.contains("/")) {
PublicKeyAuthenticationClient pk =
new PublicKeyAuthenticationClient();
pk.setUsername(user);
// Open up the private key file
SshPrivateKeyFile file = SshPrivateKeyFile
.parse(new File(password));
SshPrivateKey key = file.toPrivateKey(null);
pk.setKeyfile(password);
pk.setKey(key);
// Try the authentication
result = ssh.authenticate(pk);
} else {
// Create a password authentication instance
PasswordAuthenticationClient pwd =
new PasswordAuthenticationClient();
// Get the users name
pwd.setUsername(user);
// Get the password
pwd.setPassword(password);
// Try the authentication
result = ssh.authenticate(pwd);
}
System.out.println("Result fromssh.authenticate(pwd) " + result);
// Evaluate the result
if (result == AuthenticationProtocolState.COMPLETE) {
// The connection is authenticated we can now do some real work!
SftpClient sftp = ssh.openSftpClient();
System.out.println("openSftpClient");
// Upload a file
if(remoteFile != null && remoteFile.trim().length() > 0){
sftp.put(localFile, remoteFile);
System.out.println("======== no remote ======================================== file transfer success =======================================================================");
}
else {
System.out.println("================================================ file transfer starting =======================================================================");
sftp.put(localFile);
System.out.println("================================================ file transfer success =======================================================================");
}
// Quit
sftp.quit();
ssh.disconnect();
}
System.out.println(" END ");
}
【问题讨论】:
-
您要发送的文件的大小是多少?
-
文件大小各不相同,但通常在 100-150 KB 左右。这个特定的文件是 109874 字节