【问题标题】:Transfer Files With Directory To SFTP Server Using Jsch使用 Jsch 将带有目录的文件传输到 SFTP 服务器
【发布时间】:2017-12-14 13:24:20
【问题描述】:

我正在尝试将文件从本地计算机移动到远程服务器。但我无法用 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(folder) 的情况下复制所有文件; [在JSch中一个一个创建文件夹]。

【问题讨论】:

  • 问题是什么?你的代码有什么问题?

标签: java io jsch


【解决方案1】:

“这里我不知道如何将 /Home 更改为 /ROOT 文件路径名。而且我需要复制所有文件而不使用 sftpChannel.mkdir(folder); [在 JSch 中一一创建文件夹。” - 这个我不太清楚。您不想在代码中使用 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...");
    }
}

【讨论】:

  • @ Siva.. 如果我给出完整的目录文件路径,那么它将自动创建目录并插入文件。如果文件已经存在,则更新。
  • 简单来说,C:/user//Home 就是我在本地机器上的根目录。 /ROOT 是 sftp 机器的根目录。例如。将文件从windows复制到linux。
【解决方案2】:

PFB 示例代码 如果您想根据本地计算机中的目录名称在远程服务器中自动创建目录。这将递归地检查本地目录 C:\user\Home 中的所有文件,并将它们以相同的文件夹结构移动到 linux 服务器中的 /ROOT/ 目录。(例如:如果文件夹 1、文件夹 2 与 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;
} 

【讨论】:

    【解决方案3】:

    另一种方法是将“文件”目录转换为 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 将文件夹转换为 Zip 类型的传输文件

     channelSftp.put(new FileInputStream("C:/temp/File.zip"), "/ROOT/");
    

    一旦 zip 文件被传输,使用 ChannelExec 解压缩它。

    ChannelExec channelExec = (ChannelExec) session.openChannel("exec");
    channelExec.setCommand("unzip /ROOT/File.zip");
    

    然后删除压缩文件

    channelExec.setCommand("rm /ROOT/File.zip");
    

    【讨论】:

      猜你喜欢
      • 2021-12-31
      • 2022-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多