//复制文件到文件夹
org.apache.commons.io.FileUtils.copyFileToDirectory(file, new File(targetFilePath));
//复制文件夹到文件夹
org.apache.commons.io.FileUtils.copyDirectory(new File(sourceFilePath), new File(filePath));
//获取文件夹下所有的文件
File[] files = new File(filePath).listFiles();
使用ch.ethz.ssh2工具包进行远程服务器的文件操作
ch.ethz.ganymed
ganymed-ssh2
build210
//简单的跨服务器文件传输
String dataServerIp = "192.168.1.1";
//数据服务器的用户名
String dataServerUsername = "root";
//数据服务器的密码
String dataServerPassword = "root";
//数据服务器的目的文件夹
String dataServerDestDir = "/home/www/test/";
Connection conn = new Connection(dataServerIp);
try {
conn.connect();
boolean isAuthenticated = conn.authenticateWithPassword(dataServerUsername, dataServerPassword);
if (isAuthenticated == false){
System.out.println("Authentication failed.文件scp到数据服务器时发生异常");
}
SCPClient client = new SCPClient(conn);
// client.put("D:\\text.txt", dataServerDestDir); //本地文件scp到远程目录
//远程的文件scp到本地目录
client.get(dataServerDestDir+"text_remote.TXT" , "E:\\");
conn.close();
} catch (IOException e) {
e.printStackTrace();
}
//关于SFTPv3Client的一些操作可以查询该文章
https://blog.youkuaiyun.com/wangmuming/article/details/20537289
SFTPv3Client sftpv3Client = new SFTPv3Client(conn);
//获取远程文件的路径下文件的属性,如判断是否是文件夹
sFTPv3FileAttributes = sftpv3Client.lstat(dataServerIp);
sFTPv3FileAttributes.isDirectory()
//获取目标文件夹中所有文件
Vector<?> fileVector = sftpv3Client.ls(dataServerDestDir);
SCPClient client = conn.createSCPClient();
client.get(dataServerDestDir , "E:\\")