JschSftpClientHandler.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package com.primeton.damp.fileclient;
  2. import com.jcraft.jsch.Channel;
  3. import com.jcraft.jsch.ChannelSftp;
  4. import com.jcraft.jsch.JSch;
  5. import com.jcraft.jsch.Session;
  6. import org.apache.commons.lang.StringUtils;
  7. import org.slf4j.Logger;
  8. import org.slf4j.LoggerFactory;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import java.io.OutputStream;
  12. import java.nio.file.Files;
  13. import java.nio.file.Path;
  14. import java.nio.file.Paths;
  15. import java.util.List;
  16. import java.util.Properties;
  17. /**
  18. * <pre>
  19. *
  20. * Created by zhaopx.
  21. * User: zhaopx
  22. * Date: 2021/3/31
  23. * Time: 10:54
  24. *
  25. * </pre>
  26. *
  27. * @author zhaopx
  28. */
  29. public class JschSftpClientHandler implements X11ClientHandler {
  30. protected static Logger log = LoggerFactory.getLogger("JschSftpClientHandler");
  31. private final String sftpHost;
  32. private int sftpPort = 22;
  33. private final String ftpUsername;
  34. private String ftpPassword;
  35. private String privateKey = "/Users/zhenqin/.ssh/id_rsa";
  36. private ChannelSftp channelSftp;
  37. public JschSftpClientHandler(String sftpHost, int sftpPort, String ftpUsername, String ftpPassword) {
  38. this.sftpHost = sftpHost;
  39. this.sftpPort = sftpPort;
  40. this.ftpUsername = ftpUsername;
  41. this.ftpPassword = ftpPassword;
  42. }
  43. @Override
  44. public void reconnect(Properties params) throws IOException {
  45. JSch jsch = new JSch();
  46. try {
  47. /*
  48. if (Files.exists(Paths.get(privateKey))) {
  49. jsch.addIdentity(privateKey, "123456");
  50. if (StringUtils.isNotBlank(ftpPassword)) {
  51. jsch.addIdentity(privateKey, ftpPassword);
  52. } else {
  53. }
  54. }
  55. */
  56. Session session = jsch.getSession(ftpUsername, sftpHost, sftpPort);
  57. session.setPassword(ftpPassword);
  58. session.setConfig("StrictHostKeyChecking", "no");
  59. session.connect(CONNECT_TIMEOUT);
  60. // 创建sftp通信通道
  61. Channel channel = session.openChannel("sftp");
  62. channel.connect(CONNECT_TIMEOUT);
  63. log.info("Channel created to " + sftpHost + ".");
  64. channelSftp = (ChannelSftp) channel;
  65. } catch (Exception e) {
  66. if(e instanceof IOException) {
  67. throw (IOException)e;
  68. }
  69. throw new IOException(e);
  70. }
  71. }
  72. @Override
  73. public OutputStream writeFile(String file, boolean overwrite) throws IOException {
  74. return null;
  75. }
  76. @Override
  77. public InputStream readFile(String file) throws IOException {
  78. return null;
  79. }
  80. @Override
  81. public boolean rename(String file, String newFileName) {
  82. return false;
  83. }
  84. @Override
  85. public boolean deleteFile(String path) {
  86. return false;
  87. }
  88. @Override
  89. public boolean mkdir(String path) {
  90. return false;
  91. }
  92. @Override
  93. public boolean mkdirs(String path) {
  94. return false;
  95. }
  96. @Override
  97. public List<Path> getChildren(String ftpPath) {
  98. return null;
  99. }
  100. @Override
  101. public boolean exists(String path) {
  102. return false;
  103. }
  104. @Override
  105. public boolean existsDir(String path) {
  106. return false;
  107. }
  108. @Override
  109. public boolean existsFile(String path) {
  110. return false;
  111. }
  112. @Override
  113. public void close() throws IOException {
  114. if(channelSftp != null) {
  115. channelSftp.disconnect();
  116. }
  117. }
  118. public static void main(String[] args) throws IOException {
  119. Properties map = new Properties();
  120. String host = "192.168.30.143";
  121. int port = 22;
  122. String username = "root";
  123. String password = "admin-123456";
  124. try(JschSftpClientHandler handler = new JschSftpClientHandler(host, port, username, password)) {
  125. handler.reconnect(map);
  126. List<Path> result = handler.getChildren("/root/software/damp");
  127. for (Path path : result) {
  128. System.out.println(path.toString());
  129. }
  130. }
  131. }
  132. }