java远程服务器上传下载文件

  • Java远程服务器代码
package com.znyw.znywtts.utils;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SCPClient;
import com.znyw.znywtts.common.Constant;

import java.io.File;
import java.io.IOException;

/**
 * @Author [email protected]
 * Date on 2021/10/11  9:34 上午
 * 远程上传下载服务器文件
 */
public class ScpUtil {


    /**
     * 使用用户名和密码来进行登录验证。如果为true则通过用户名和密码登录,false则使用rsa免密码登录
     */
    private static boolean usePassword = false;
    /**
     * ssh连接对象
     */
    private static Connection connection = new Connection(Constant.RemoteServer.IP, Constant.RemoteServer.PORT);

    /**
     * ssh用户登录验证,使用用户名和密码来认证
     *
     * @param user
     * @param password
     * @return
     */
    public static boolean isAuthedWithPassword(String user, String password) {
        try {
            return connection.authenticateWithPassword(user, password);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * ssh用户登录验证,使用用户名、私钥、密码来认证 其中密码如果没有可以为null,生成私钥的时候如果没有输入密码,则密码参数为null
     *
     * @param user
     * @param privateKey
     * @param password
     * @return
     */
    public static boolean isAuthedWithPublicKey(String user, File privateKey, String password) {
        try {
            return connection.authenticateWithPublicKey(user, privateKey, password);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 认证
     * @return
     */
    public static boolean isAuth() {
        if (usePassword) {
            return isAuthedWithPassword(Constant.RemoteServer.USER, Constant.RemoteServer.PASSWORD);
        } else {
            return isAuthedWithPublicKey(Constant.RemoteServer.USER,
                    new File(Constant.RemoteServer.PRIVATE_KEY), Constant.RemoteServer.PASSWORD);
        }
    }

    /**
     * 从远程服务器下载文件
     * @param remoteFile
     * @param path
     */
    public static void getFile(String remoteFile, String path) {
        try {
            connection.connect();
            boolean isAuthed = isAuth();
            if (isAuthed) {
                System.out.println("认证成功!");
                SCPClient scpClient = connection.createSCPClient();
                scpClient.get(remoteFile, path);
            } else {
                System.out.println("认证失败!");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            connection.close();
        }
    }

    /**
     * 向远程服务器上传文件
     * @param localFile
     * @param remoteTargetDirectory
     */
    public static void putFile(String localFile, String remoteTargetDirectory) {
        try {
            connection.connect();
            boolean isAuthed = isAuth();
            if (isAuthed) {
                SCPClient scpClient = connection.createSCPClient();
                scpClient.put(localFile, remoteTargetDirectory);
            } else {
                System.out.println("认证失败!");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            connection.close();
        }
    }

    public static void main(String[] args) {
        try {
             getFile(Constant.RemoteServer.REMOTE_PATH, Constant.RemoteServer.LOCAL_PATH);
//            putFile("/Users/wuyongjia/Desktop/截屏2021-09-15 上午10.00.55.png",
//                    Constant.RemoteServer.REMOTE_PATH);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

  • 错误
java.io.IOException: There was a problem while connecting to 10.211.55.3:22
    at ch.ethz.ssh2.Connection.connect(Connection.java:699)
    at ch.ethz.ssh2.Connection.connect(Connection.java:490)
    at com.znyw.znywtts.utils.ScpUtil.putFile(ScpUtil.java:97)
    at com.znyw.znywtts.utils.ScpUtil.main(ScpUtil.java:116)
Caused by: java.io.IOException: Key exchange was not finished, connection is closed.
    at ch.ethz.ssh2.transport.KexManager.getOrWaitForConnectionInfo(KexManager.java:91)
    at ch.ethz.ssh2.transport.TransportManager.getConnectionInfo(TransportManager.java:229)
    at ch.ethz.ssh2.Connection.connect(Connection.java:655)
    ... 3 more
Caused by: java.io.IOException: Cannot negotiate, proposals do not match.
    at ch.ethz.ssh2.transport.KexManager.handleMessage(KexManager.java:412)
    at ch.ethz.ssh2.transport.TransportManager.receiveLoop(TransportManager.java:753)
    at ch.ethz.ssh2.transport.TransportManager$1.run(TransportManager.java:468)
    at java.lang.Thread.run(Thread.java:748)

解决 :在远程服务器/etc/ssh/sshd_config最后添加如下行内容解决问题
KexAlgorithms diffie-hellman-group1-sha1,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha1,diffie-hellman-group-exchange-sha256,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group1-sha1,[email protected]
参考:https://blog.csdn.net/lingyudeai/article/details/106663105

parallels@10:~$ cd /etc/ssh/
parallels@10:/etc/ssh$ sudo su
root@10:/etc/ssh# vim sshd_config
  • 账号密码登录方式验证成功
  • ras免密登陆方式:需要从远处服务器设置密钥,然后拷贝到本地,在scp的时候戴上密钥
    1.参考该[网址]配置rsa密钥(https://blog.csdn.net/TQWei00001/article/details/96431523)
    2.验证出错
java.io.IOException: Publickey authentication failed.
    at ch.ethz.ssh2.auth.AuthenticationManager.authenticatePublicKey(AuthenticationManager.java:259)
    at ch.ethz.ssh2.Connection.authenticateWithPublicKey(Connection.java:377)
    at ch.ethz.ssh2.Connection.authenticateWithPublicKey(Connection.java:428)
    at com.znyw.znywtts.utils.ScpUtil.isAuthedWithPublicKey(ScpUtil.java:53)
    at com.znyw.znywtts.utils.ScpUtil.isAuth(ScpUtil.java:68)
    at com.znyw.znywtts.utils.ScpUtil.getFile(ScpUtil.java:81)
    at com.znyw.znywtts.utils.ScpUtil.main(ScpUtil.java:120)
Caused by: java.io.IOException: Invalid PEM structure, '-----BEGIN...' missing
    at ch.ethz.ssh2.crypto.PEMDecoder.parsePEM(PEMDecoder.java:137)
    at ch.ethz.ssh2.crypto.PEMDecoder.decode(PEMDecoder.java:312)
    at ch.ethz.ssh2.auth.AuthenticationManager.authenticatePublicKey(AuthenticationManager.java:168)
    ... 6 more

3.本地cmd 使用rsa免密方式连接远程服务器没问题
4.解决

你可能感兴趣的:(java远程服务器上传下载文件)