我没有找到将zip文件从远程服务器a目录复制到远程服务器b目录的解决方案。
例如,我要将文件从/ RD-CRO-TMF / Boundary /复制到/ RD-CRO-TMF-PROD / Test_Load / PPD / test/。
文件名= abc.zip。
由于在网络上的大多数解决方案都是将文件从远程服务器获取到本地,或者将文件从本地上传到远程服务器,所以我在这里呆了几天。
package test;
public class Tesing {
private static final String USER_PROMPT = "Enter username@hostname:port";
private static final boolean USE_GUI = true;
public static void main(final String[] arg) {
Session session = null;
ChannelSftp channelSftp = null;
try {
final JSch jsch = new JSch();
final String defaultInput = System.getProperty("user.name") + "@localhost:22";
String input = (USE_GUI) ? JOptionPane.showInputDialog(USER_PROMPT, defaultInput)
: System.console().readLine("%s (%s): ", USER_PROMPT, defaultInput);
if (input == null || input.trim().length() == 0) {
input = defaultInput;
}
final int indexOfAt = input.indexOf('@');
final int indexOfColon = input.indexOf(':');
String user = "oaz4188846";
String password = "POG84286";
String host = "us1salx43564.corpnet2.com";
int port = 22;
session = jsch.getSession(user, host, 22);
final UserInfo ui = new MyUserInfo();
session.setUserInfo(ui);
System.out.println("Establishing Connection...");
session.connect();
System.out.println("Connection established.");
System.out.println("Creating SFTP Channel.");
channelSftp = (ChannelSftp) session.openChannel("sftp");
channelSftp.connect();
System.out.println("SFTP Channel created.");
String newfile = "abc.zip";
String FileDirectory = "/RD-CRO-TMF/Boundary/";
String FileDirectory1 = "/RD-CRO-TMF-PROD/Test_Load/PPD/test/";
channelSftp.cd(FileDirectory);
if (channelSftp.get(newfile) != null) {
channelSftp.rename(FileDirectory + newfile, FileDirectory1 + newfile);
// channelSftp.cd(FileDirectory);
}
// channelSftp.put("RD-CRO-TMF/Boundary/abc.zip",
// "/RD-CRO-TMF-PROD/Test_Load/PPD/test");
} catch (JSchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SftpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
if (channelSftp != null) {
channelSftp.exit();
}
if (session != null) {
session.disconnect();
}
}
}
public static class MyUserInfo implements UserInfo {
private String password;
@Override
public String getPassword() {
return password;
}
@Override
public boolean promptYesNo(final String str) {
final Object[] options = { "yes", "no" };
final boolean yesNo = (USE_GUI)
? JOptionPane.showOptionDialog(null, str, "Warning", JOptionPane.DEFAULT_OPTION,
JOptionPane.WARNING_MESSAGE, null, options, options[0]) == 0
: System.console().readLine("Enter y or n: ").equals("y");
return yesNo;
}
@Override
public String getPassphrase() {
return null;
}
@Override
public boolean promptPassphrase(final String message) {
return true;
}
@Override
public boolean promptPassword(final String message) {
if (!USE_GUI) {
password = new String(System.console().readPassword("Password: "));
return true;
} else {
final JTextField passwordField = new JPasswordField(20);
final Object[] ob = { passwordField };
final int result = JOptionPane.showConfirmDialog(null, ob, message, JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) {
password = passwordField.getText();
return true;
} else {
return false;
}
}
}
@Override
public void showMessage(final String message) {
if (!USE_GUI) {
System.console().printf(message);
} else {
JOptionPane.showMessageDialog(null, message);
}
}
}
}
我在运行时看到以下异常
Establishing Connection...
Connection established.
Creating SFTP Channel.
SFTP Channel created.
4: Failure
at com.jcraft.jsch.ChannelSftp.throwStatusError(Unknown Source)
at com.jcraft.jsch.ChannelSftp.rename(Unknown Source)
at test.Tesing.main(Tesing.java:69)
我从这里看到了示例代码 How to move file from directory A to directory B in remote server? 但不确定在我的要求中要合并的位置。请帮助
enter code here
String existingfile = "abc.jpg";
String newfile = "123.jpg";
FileDirectory = "/appl/user/home/test/";
sftp.cd(FileDirectory+"temp/");
if (sftp.get( newfile ) != null){
sftp.rename(FileDirectory + "temp/" + newfile ,
FileDirectory + newfile );
sftp.cd(FileDirectory);
sftp.rm(existingfile );
}