- 前不久在項(xiàng)目開發(fā)中遇上的一個(gè)需求:需要在B機(jī)器上將A機(jī)器某路徑下的文件拷貝到B機(jī)器上做保存岳枷。
簡(jiǎn)單分析下來(lái),自己的理解就是:一個(gè)RPC呜叫,主要就是解決跨域傳輸文件的問題空繁。在提前能拿到A機(jī)器的IP、Port怀偷、userName家厌、Pwd的情況下還是很好解決的。
參考下Linux下椎工,實(shí)現(xiàn)方式:
將本機(jī)文件上傳到遠(yuǎn)程服務(wù)器
#scp /usr/local/news.txt root@192.168.1.1:/etc/opt
** /usr/local/** 本地文件的絕對(duì)路徑
news.txt 要復(fù)制到服務(wù)器上的本地文件
root 通過root用戶登錄到遠(yuǎn)程服務(wù)器(也可以使用其他擁有同等權(quán)限的用戶)
192.168.6.129 遠(yuǎn)程服務(wù)器的ip地址(也可以使用域名或機(jī)器名)
/etc/opt 將本地文件復(fù)制到位于遠(yuǎn)程服務(wù)器上的路徑
將遠(yuǎn)程服務(wù)器文件下載到本機(jī)
#scp root@192.168.1.1:/usr/local/news.txt /usr/local
root 通過remote用戶登錄到遠(yuǎn)程服務(wù)器(也可以使用其他擁有同等權(quán)限的用戶)
192.168.1.1 遠(yuǎn)程服務(wù)器的IP地址(當(dāng)然也可以使用該服務(wù)器域名)
/usr/local/news.txt 欲復(fù)制到本機(jī)的位于遠(yuǎn)程服務(wù)器上的文件
/usr/local 將遠(yuǎn)程文件復(fù)制到本地的絕對(duì)路徑
- java 實(shí)現(xiàn)的方式參照上述就簡(jiǎn)單了饭于,如下:
需要導(dǎo)入Jar包:
<!-- https://mvnrepository.com/artifact/ch.ethz.ganymed/ganymed-ssh2 -->
<dependency>
<groupId>ch.ethz.ganymed</groupId>
<artifactId>ganymed-ssh2</artifactId>
<version>build210</version>
</dependency>
工具類:
public class Scpclient {
private static final FileLog logger = new FileLog(Scpclient.class.getName());
static private Scpclient instance;
static synchronized public Scpclient getInstance(String IP, int port,
String username, String passward) {
if (instance == null) {
instance = new Scpclient(IP, port, username, passward);
}
return instance;
}
public Scpclient(String IP, int port, String username, String passward) {
this.ip = IP;
this.port = port;
this.username = username;
this.password = passward;
}
/**
* 遠(yuǎn)程拷貝文件
* @param remoteFile 遠(yuǎn)程源文件路徑
* @param localTargetDirectory 本地存放文件路徑
*/
public Map<String,Object> getFile(String remoteFile, String localTargetDirectory) {
Connection conn = new Connection(ip,port);
try {
conn.connect();
boolean isAuthenticated = conn.authenticateWithPassword(username,
password);
if (isAuthenticated == false) {
logger.log(Level.ERROR_INT,"authentication failed");
return Common.pageMap(NetMsgUtil.INVOKING_SERVER_EXCEPTION,"authentication failed");
}
SCPClient client = new SCPClient(conn);
client.get(remoteFile, localTargetDirectory);
conn.close();
} catch (IOException e) {
logger.log(Level.ERROR_INT,e.getMessage());
return Common.pageMap(NetMsgUtil.INVOKING_SERVER_EXCEPTION,e.getMessage());
}
return Common.pageMap(NetMsgUtil.NET_NORMAL_MSG,"Document Success");
}
/**
* 遠(yuǎn)程上傳文件
* @param localFile 本地文件路徑
* @param remoteTargetDirectory 遠(yuǎn)程存放文件路徑
*/
public void putFile(String localFile, String remoteTargetDirectory) {
Connection conn = new Connection(ip,port);
try {
conn.connect();
boolean isAuthenticated = conn.authenticateWithPassword(username,
password);
if (isAuthenticated == false) {
logger.log(Level.ERROR_INT,"authentication failed");
}
SCPClient client = new SCPClient(conn);
client.put(localFile, remoteTargetDirectory);
conn.close();
} catch (IOException e) {
logger.log(Level.ERROR_INT,e.getMessage());
}
}
/**
* 遠(yuǎn)程上傳文件并對(duì)上傳文件重命名
* @param localFile 本地文件路徑
*@param remoteFileName遠(yuǎn)程文件名
* @param remoteTargetDirectory 遠(yuǎn)程存放文件路徑
*@param mode 默認(rèn)"0600",length=4
*/
public void putFile(String localFile, String remoteFileName,String remoteTargetDirectory,String mode) {
Connection conn = new Connection(ip,port);
try {
conn.connect();
boolean isAuthenticated = conn.authenticateWithPassword(username,
password);
if (isAuthenticated == false) {
logger.log(Level.ERROR_INT,"authentication failed");
}
SCPClient client = new SCPClient(conn);
if((mode == null) || (mode.length() == 0)){
mode = "0600";
}
client.put(localFile, remoteFileName, remoteTargetDirectory, mode);
//重命名
Session sess = conn.openSession();
String tmpPathName = remoteTargetDirectory + File.separator+ remoteFileName;
String newPathName = tmpPathName.substring(0, tmpPathName.lastIndexOf("."));
sess.execCommand("mv " + remoteFileName + " " + newPathName);
conn.close();
} catch (IOException e) {
logger.log(Level.ERROR_INT,e.getMessage());
}
}
private String ip;
private int port;
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
}
調(diào)用姿勢(shì):
Scpclient scpclient = Scpclient.getInstance(String IP, int port,String username, String passward);
// 拷貝
scpclient .getFile(...);
// 上傳
scpclient .putFile(...);
- 最后维蒙,只想說(shuō)一句掰吕,我們不生產(chǎn)代碼,我們只是代碼搬運(yùn)工颅痊。