近期项目中需要把windows上的一个脚本上传到linux的服务器上,于是上网搜了下方法,可以通过scp来做,代码如下:
package com.infobird.scp;
import java.io.IOException;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SCPClient;
public class SCPUtil {
private Connection conn;
private String ip;
private String user;
private String password;
public SCPUtil(String ip, String user, String password) {
this.ip = ip;
this.user = user;
this.password = password;
}
private boolean login() throws IOException {
conn = new Connection(ip);
conn.connect();
return conn.authenticateWithPassword(user, password);
}
public int exec(String cmds) {
int ret = -1;
try {
if(login()) {
SCPClient scpClient = conn.createSCPClient();
//服务器端的文件下载到本地的目录下
scpClient.get("/data/test_hbase.pig", "F:/log/");
//将本地文件上传到服务器端的目录下
scpClient.put("D:/Workspaces/others1/TaskPlatform/src/m