java代码从Linux服务器的目录,下载目录中的文件到本地。(不是FTP,不是FTP,不是FTP)

废话不多说开干

这里需要的maven依赖

		<dependency>
			<groupId>sshtools</groupId>
			<artifactId>j2ssh-core</artifactId>
			<version>0.2.9</version>
		</dependency>
		
		<dependency>
			<groupId>ch.ethz.ganymed</groupId>
			<artifactId>ganymed-ssh2</artifactId>
			<version>262</version>
		</dependency>

这里是代码

import java.io.*;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
public class TestRemoteConnect {
    public static void main(String[] args) {
        String path = "/usr/local/20200720/20200720112820286_72.jpg";//Linux服务器中的文件路径
        String hostName = "192.168.1.53";//Linux服务器的地址
        int port = 22;//SSH访问22端口
        String username = "root";//Linux服务器的用户名
        String password = "123456";//Linux服务器的密码
        //创建SSH连接
        Connection connect = getConnect(hostName, username, password, port);
        //下载文件
        downloadFile(path, connect);
    }


    /**
     * 创建SSH连接
     * @param hostName
     * @param username
     * @param password
     * @param port
     * @return
     */
    public static Connection getConnect(String hostName, String username, String password, int port) {
        Connection conn = new Connection(hostName, port);
        try {
            // 连接到主机
            conn.connect();
            // 使用用户名和密码校验
            boolean isconn = conn.authenticateWithPassword(username, password);
            if (!isconn) {
                System.out.println("用户名称或者是密码不正确");
            } else {
                System.out.println("服务器连接成功.");
                return conn;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 下载文件
     * @param path
     * @param conn
     */
    public static void downloadFile(String path, Connection conn) {
        if (conn != null) {
            Session session = null;
            try {
                FileOutputStream fileOut = null;
                session = conn.openSession();
                 //设置读取文件大小 102400
                session.execCommand("tail -102400 ".concat(path));
                InputStream inputStream = new StreamGobbler(session.getStdout());
                BufferedInputStream bis = new BufferedInputStream(inputStream);
                //写入到文件(注意文件保存路径的后面一定要加上文件的名称)
                fileOut = new FileOutputStream("F:/20200720112820286_72.jpg");
                BufferedOutputStream bos = new BufferedOutputStream(fileOut);
                byte[] buf = new byte[4096];
                int length = bis.read(buf);
                //保存文件
                while(length != -1)
                {
                    bos.write(buf, 0, length);
                    length = bis.read(buf);
                }
                bos.close();
                bis.close();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                // 连接的Session和Connection对象都需要关闭
                if (session != null) {
                    session.close();
                }
                if (conn != null) {
                    conn.close();
                }
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_41033385/article/details/109217665