本地/远程文件操作一些代码

//复制文件到文件夹
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.csdn.net/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:\\")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值