scp跨服务器

来源:互联网 发布:高斯金字塔算法 matlab 编辑:程序博客网 时间:2024/06/07 00:14
需要下载一个jar。ganymed-ssh2-build210.jar#============================package com.alqsoft.utils.scp;import java.io.IOException;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import com.alibaba.dubbo.common.URL;import com.alqsoft.service.impl.appversion.AppVersionServiceImpl;import ch.ethz.ssh2.Connection;import ch.ethz.ssh2.SCPClient;/** *  * @Description: TODO * @author wudi * @version v1.0 * @create-time 2017年8月9日 下午9:17:54 *  */public class ScpUtils {private static Logger log = LoggerFactory.getLogger(ScpUtils.class);    private static String IP = "10.0.11.10";  //IP地址    private static int PORT = 31575;               //ssh的端口号       private static String USER = "sunyaowei-ystops";        //登录用户名      private static String PASSWORD ="sun!@#135";//用户密码    private static Connection connection = new Connection(IP, 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;      }        public static boolean isAuth() {                      return isAuthedWithPassword(USER, PASSWORD);             }      //下载文件    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 {              log.info("用户连接"+"认证失败");            }          } catch (IOException e) {              e.printStackTrace();          } finally {              connection.close();          }      }      //上传文件    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 {              log.info("用户连接"+"认证失败");             }          } catch (Exception ex) {          log.info("上传数据"+ex);        } finally {              connection.close();          }      }      //测试方法/* public static void main(String[] args) {          try {              // getFile("/home/users/ubuntu/error.txt", "c://");              putFile("d://wd.txt", "/data/web_jianqiao/mobile-5166/webapps/ROOT/upload");          } catch (Exception e) {              e.printStackTrace();          }     }  */}  

原创粉丝点击