Java Jcraft 1.5 SFTP覆盖故障ID 4
我的覆盖不起作用,并返回失败4 ID。我可以写入此目标,而不会出现任何问题,但当过火继续失败时。当我使用winscp和这里使用的相同用户凭据时,我可以写和覆盖而不会出现问题。我不知道我的覆盖选项到底是怎么回事。如果文件在其中且覆盖为真,则应覆盖同名文件,但其不工作。我使用的是Jcraft 1.50,我不想升级到1.54,因为在我恢复库之前,该版本也存在同样的问题Java Jcraft 1.5 SFTP覆盖故障ID 4,java,sftp,put,jsch,overwrite,Java,Sftp,Put,Jsch,Overwrite,我的覆盖不起作用,并返回失败4 ID。我可以写入此目标,而不会出现任何问题,但当过火继续失败时。当我使用winscp和这里使用的相同用户凭据时,我可以写和覆盖而不会出现问题。我不知道我的覆盖选项到底是怎么回事。如果文件在其中且覆盖为真,则应覆盖同名文件,但其不工作。我使用的是Jcraft 1.50,我不想升级到1.54,因为在我恢复库之前,该版本也存在同样的问题 //connect try { session = jsch.getSession(user, host, port);
//connect
try {
session = jsch.getSession(user, host, port);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword( password );
session.connect();
Channel channel = session.openChannel( "sftp" );
channel.connect();
sftpChannel = (ChannelSftp) channel;
//make sure that file does not exist or
//user wants to overwrite the file
testStream = sftpChannel.get(destination);
if (testStream != null && !overwrite) {
throw new SftpException(ChannelSftp.SSH_FX_OP_UNSUPPORTED, "File already exists");
}
} catch (SftpException e) {
if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE){
//file does not yet exist. continue.
} else {
resultComments = "SFTP Exception " + e.getMessage();
status = RunResult.FAIL;
e.printStackTrace();
}
} catch (JSchException e) {
resultComments = "Jsch Exception " + e.getMessage();
status = RunResult.FAIL;
e.printStackTrace();
}
try {
//if file does not exist or overwrite selected, transfer file
if (testStream == null || overwrite) {
ByteArrayInputStream fileByteInputStream = new ByteArrayInputStream(fileByteArray);
if (sftpChannel != null) {
String workingDirectory = destination.substring(0, destination.lastIndexOf("/") + 1);
String fileName = destination.substring(destination.lastIndexOf('/') + 1, destination.length());
sftpChannel.cd(workingDirectory);
sftpChannel.put(fileByteInputStream, fileName, sftpChannel.OVERWRITE);
try {
fileByteInputStream.close();
} catch (IOException e) {
resultComments = "SFTP Exception: Unable to close Input Stream." + e.getMessage();
status = RunResult.FAIL;
e.printStackTrace();
}
sftpChannel.exit();
}
if (session != null) {
session.disconnect();
}
}
} catch (SftpException e) {
resultComments = "SFTP Exception " + e.getMessage();
status = RunResult.FAIL;
e.printStackTrace();
}
所以我解决了这个问题。似乎这个方法
sftpChannel.put(fileByteInputStream, fileName, sftpChannel.OVERWRITE);
不能正常工作。尽管Jcraft文档清楚地说
put(String src, String dst, int mode)
//Uploads a file.
当我将其更改为另一种put方法时
sftpChannel.put(fileByteInputStream, fileName, null, sftpChannel.OVERWRITE);
通过使用put方法,我的覆盖问题似乎消失了
put(String src, String dst, SftpProgressMonitor monitor, int mode)
//Uploads a file.
当文件已经存在并且我想覆盖它时,我能够传输而不发生任何故障