123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- package com.primeton.damp.fileclient;
- import com.jcraft.jsch.Channel;
- import com.jcraft.jsch.ChannelSftp;
- import com.jcraft.jsch.JSch;
- import com.jcraft.jsch.Session;
- import org.apache.commons.lang.StringUtils;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.util.List;
- import java.util.Properties;
- /**
- * <pre>
- *
- * Created by zhaopx.
- * User: zhaopx
- * Date: 2021/3/31
- * Time: 10:54
- *
- * </pre>
- *
- * @author zhaopx
- */
- public class JschSftpClientHandler implements X11ClientHandler {
- protected static Logger log = LoggerFactory.getLogger("JschSftpClientHandler");
- private final String sftpHost;
- private int sftpPort = 22;
- private final String ftpUsername;
- private String ftpPassword;
- private String privateKey = "/Users/zhenqin/.ssh/id_rsa";
- private ChannelSftp channelSftp;
- public JschSftpClientHandler(String sftpHost, int sftpPort, String ftpUsername, String ftpPassword) {
- this.sftpHost = sftpHost;
- this.sftpPort = sftpPort;
- this.ftpUsername = ftpUsername;
- this.ftpPassword = ftpPassword;
- }
- @Override
- public void reconnect(Properties params) throws IOException {
- JSch jsch = new JSch();
- try {
- /*
- if (Files.exists(Paths.get(privateKey))) {
- jsch.addIdentity(privateKey, "123456");
- if (StringUtils.isNotBlank(ftpPassword)) {
- jsch.addIdentity(privateKey, ftpPassword);
- } else {
- }
- }
- */
- Session session = jsch.getSession(ftpUsername, sftpHost, sftpPort);
- session.setPassword(ftpPassword);
- session.setConfig("StrictHostKeyChecking", "no");
- session.connect(CONNECT_TIMEOUT);
- // 创建sftp通信通道
- Channel channel = session.openChannel("sftp");
- channel.connect(CONNECT_TIMEOUT);
- log.info("Channel created to " + sftpHost + ".");
- channelSftp = (ChannelSftp) channel;
- } catch (Exception e) {
- if(e instanceof IOException) {
- throw (IOException)e;
- }
- throw new IOException(e);
- }
- }
- @Override
- public OutputStream writeFile(String file, boolean overwrite) throws IOException {
- return null;
- }
- @Override
- public InputStream readFile(String file) throws IOException {
- return null;
- }
- @Override
- public boolean rename(String file, String newFileName) {
- return false;
- }
- @Override
- public boolean deleteFile(String path) {
- return false;
- }
- @Override
- public boolean mkdir(String path) {
- return false;
- }
- @Override
- public boolean mkdirs(String path) {
- return false;
- }
- @Override
- public List<Path> getChildren(String ftpPath) {
- return null;
- }
- @Override
- public boolean exists(String path) {
- return false;
- }
- @Override
- public boolean existsDir(String path) {
- return false;
- }
- @Override
- public boolean existsFile(String path) {
- return false;
- }
- @Override
- public void close() throws IOException {
- if(channelSftp != null) {
- channelSftp.disconnect();
- }
- }
- public static void main(String[] args) throws IOException {
- Properties map = new Properties();
- String host = "192.168.30.143";
- int port = 22;
- String username = "root";
- String password = "admin-123456";
- try(JschSftpClientHandler handler = new JschSftpClientHandler(host, port, username, password)) {
- handler.reconnect(map);
- List<Path> result = handler.getChildren("/root/software/damp");
- for (Path path : result) {
- System.out.println(path.toString());
- }
- }
- }
- }
|