Java 使用SCP协议的文件传输

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20280884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 00:49:03  来源:igfitidea点击:

File tranfer using SCP protocol

javaapachescpjsch

提问by Rintos

Need a Java program to copy multiple files from local windows to Linux server and vice versa(upload and download) .I have written a utility which works for single file transfer. But when it comes for the multiple file while listing the files in a directory. The files are not getting searched out, due to the issue directory separator. In windows it is '\' where as Linux '/'. Is there any free library which helps to do so?

需要一个 Java 程序将多个文件从本地窗口复制到 Linux 服务器,反之亦然(上传和下载)。我编写了一个适用于单个文件传输的实用程序。但是当涉及多个文件时,同时列出目录中的文件。由于问题目录分隔符,文件没有被搜索出来。在 Windows 中它是“\”,而 Linux 是“/”。是否有任何免费图书馆可以帮助做到这一点?

It should be using SCP protocol. here is the snippet makes the main issue for me:

它应该使用SCP协议。这是对我来说主要问题的片段:

String files;
System.out.println("files=");
System.out.println();
File folder = new File(path);
System.out.println("folder="+folder);
File[] listOfFiles = folder.listFiles();

for (int i = 0; i < listOfFiles.length; i++) 
{

    if (listOfFiles[i].isFile()) 
    {
        files = listOfFiles[i].getName();
        System.out.println("hello here are files");
        System.out.println(files);
    }
}

回答by Dropout

JSchmight interest you. It's used for establishing a secure channel between two hosts - you can use it for SSH, SCP, SFTP, etc..

JSch可能会让您感兴趣。它用于在两个主机之间建立安全通道 - 您可以将它用于 SSH、SCP、SFTP 等。

Example of establishing a connection: example

建立连接的例子example

Example for SCP (directly from JCraft): example

SCP 示例(直接来自 JCraft):示例

This is how to get all properties(source and target absolute path from Fileobject) for your files in a directory: http://pastebin.com/FXS9cHPW

这是如何获取File目录中文件的所有属性(来自对象的源和目标绝对路径):http: //pastebin.com/FXS9cHPW