【远程传输】java调用scp,文件从本地复制到远程

阳光穿透心脏的1/2处 2022-05-16 03:06 1535阅读 0赞
  1. //数据服务器的ip地址
  2. private String dataServerIp = "192.168.123.123";
  3. //数据服务器的用户名
  4. private String dataServerUsername = "root";
  5. //数据服务器的密码
  6. private String dataServerPassword = "1234";
  7. //数据服务器的目的文件夹
  8. private String dataServerDestDir = "/export/App/uploadDir/";
  9. //从远程到本地的保存路径
  10. // private String localDir = "D:\\上传文件的临时目录";
  11. //文件scp到数据服务器
  12. Connection conn = new Connection(dataServerIp);
  13. log.info("开始scp文件");
  14. try {
  15. conn.connect();
  16. boolean isAuthenticated = conn.authenticateWithPassword(dataServerUsername, dataServerPassword);
  17. if (isAuthenticated == false)
  18. throw new IOException("Authentication failed.文件scp到数据服务器时发生异常");
  19. SCPClient client = new SCPClient(conn);
  20. client.put("D:\\00审计.zip", dataServerDestDir); //本地文件scp到远程目录
  21. // client.get(dataServerDestDir + "00审计.zip", localDir);//远程的文件scp到本地目录
  22. conn.close();
  23. } catch (IOException e) {
  24. e.printStackTrace();
  25. log.error("文件scp到数据服务器时发生异常");
  26. }
  27. log.info("scp文件结束");
  28. <!-- https://mvnrepository.com/artifact/ch.ethz.ganymed/ganymed-ssh2 -->
  29. <dependency>
  30. <groupId>ch.ethz.ganymed</groupId>
  31. <artifactId>ganymed-ssh2</artifactId>
  32. <version>build210</version>
  33. </dependency>

欢迎关注公众号:

" class="reference-link">70

发表评论

表情:
评论列表 (有 0 条评论,1535人围观)

还没有评论,来说两句吧...

相关阅读