我正在尝试将文件从本地计算机移动到远程服务器。但我无法用JSch api做到这一点。
for (File f : fileList) {
channelSftp.put(new FileInputStream(f), "/ROOT/File/"+f.getName());
}
我正在循环我的本地计算机目录/Home/File/file1.txt,file2,txt file3.txt等,并将其保存到fileList。
我需要将这些文件传输到SFTP服务器中的不同目录。说/ROOT/File/file1.txt,file2,txt file3.txt。
我不知道如何更改/ Home到/ ROOT文件的路径名。而且我还需要复制所有文件而不使用sftpChannel.mkdir(文件夹);在JSch中[逐个创建文件夹]。
“这里我不知道如何更改/ Home到/ ROOT文件路径名。而且我还需要在JSch中复制所有文件而不使用sftpChannel.mkdir(文件夹); [逐个创建文件夹]。” - 我不清楚这一点。您不想在代码中使用mkdir并且已经存在目录吗? PFB示例代码。您可以在执行代码之前在服务器中创建ROOT目录。这将循环本地目录中的文件并将它们传输到SFTP服务器。
public static void main (String args[]) throws FileNotFoundException, IOException
{
try {
JSch jsch = new JSch();
Session session = jsch.getSession("sftpuser", "sftphost");
session.setPassword("sftppassword");
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
config.put("PreferredAuthentications",
"publickey,keyboard-interactive,password");
session.setConfig(config);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
System.out.println("sftp channel opened and connected.");
ChannelSftp channelSftp = (ChannelSftp) channel;
String sftpDirectory = "/target/Rootlocation";
File directory = new File("C:\\Windows\\HomeLocation");
File[] fList = directory.listFiles();
for (File file : fList){
if (file.isFile()){
String filename=file.getAbsolutePath();
channelSftp.put(filename, sftpDirectory, ChannelSftp.OVERWRITE);
System.out.println(filename + " transferred to " + sftpDirectory );
}
}
}
catch (JSchException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("Transfer Process Completed...");
}
}
PFB示例代码如果要根据本地计算机中的目录名称在远程服务器中自动创建目录。这将递归检查本地目录C:\ user \ Home中的所有文件,并将它们以相同的文件夹结构移动到linux服务器中的/ ROOT /目录。(例如:如果folder1,folder2与C中的文件一起出现: \ user \ Home,将在sftp服务器的/ Root /目录中创建相同的文件夹名称)如果文件已存在于sftp服务器中,这也将覆盖该文件。
public static void main (String args[]) throws FileNotFoundException, IOException, ParseException
{
try {
JSch jsch = new JSch();
Session session = jsch.getSession("sftpuser", "sftphost");
session.setPassword("sftppassword");
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
config.put("PreferredAuthentications",
"publickey,keyboard-interactive,password");
session.setConfig(config);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
channelSftp = (ChannelSftp) channel;
System.out.println("sftp channel opened and connected.");
sftpDirectory = "/ROOT/";
NewDir=sftpDirectory;
listf("C:\\user\\Home");
}
catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("Transfer Process Completed...");
}
public static List<File> listf(String directoryName) throws JSchException, SftpException {
File directory = new File(directoryName);
List<File> resultList = new ArrayList<File>();
File[] fList = directory.listFiles();
resultList.addAll(Arrays.asList(fList));
for (File file : fList) {
if (file.isFile()) {
String filename=file.getAbsolutePath();
channelSftp.put(filename, NewDir, ChannelSftp.OVERWRITE);
System.out.println(filename + " transferred to " + sftpDirectory );
} else if (file.isDirectory()) {
System.out.println(file.getAbsolutePath());
NewDir = sftpDirectory+file.getName();
channelSftp.mkdir(NewDir);
System.out.println(NewDir + " Folder created ");
resultList.addAll(listf(file.getAbsolutePath()));
}
}
System.out.println(fList);
return resultList;
}
另一种方法是将“File”目录转换为zip文件,然后使用sftp传输到远程服务器并解压缩zip文件。
ZipFile zipFile = new ZipFile("C:/temp/File.zip");
ArrayList<File> filesToAdd = new ArrayList<File>();
File folder = new File("C:/temp/File");
File[] listOfFiles = folder.listFiles();
// Add files to be archived into zip file
for (File file : listOfFiles) {
filesToAdd.add(file);
}
然后你需要使用sftp将Folder转换为Zip类型的传输文件
channelSftp.put(new FileInputStream("C:/temp/File.zip"), "/ROOT/");
传输zip文件后,使用ChannelExec将其解压缩。
ChannelExec channelExec = (ChannelExec) session.openChannel("exec");
channelExec.setCommand("unzip /ROOT/File.zip");
然后删除zip文件
channelExec.setCommand("rm /ROOT/File.zip");