java scp 上传文件_java基于Scp实现Linux下远程获取、上传文件

前不久在项目开发中遇上的一个需求:需要在B机器上将A机器某路径下的文件拷贝到B机器上做保存。

简单分析下来,自己的理解就是:一个RPC,主要就是解决跨域传输文件的问题。在提前能拿到A机器的IP、Port、userName、Pwd的情况下还是很好解决的。

参考下Linux下,实现方式:

将本机文件上传到远程服务器

#scp /usr/local/news.txt root@192.168.1.1:/etc/opt

** /usr/local/** 本地文件的绝对路径

news.txt 要复制到服务器上的本地文件

root 通过root用户登录到远程服务器(也可以使用其他拥有同等权限的用户)

192.168.6.129 远程服务器的ip地址(也可以使用域名或机器名)

/etc/opt 将本地文件复制到位于远程服务器上的路径

将远程服务器文件下载到本机

#scp root@192.168.1.1:/usr/local/news.txt /usr/local

root 通过remote用户登录到远程服务器(也可以使用其他拥有同等权限的用户)

192.168.1.1 远程服务器的IP地址(当然也可以使用该服务器域名)

/usr/local/news.txt 欲复制到本机的位于远程服务器上的文件

/usr/local 将远程文件复制到本地的绝对路径

java 实现的方式参照上述就简单了,如下:

需要导入Jar包:

ch.ethz.ganymed

ganymed-ssh2

build210

工具类:

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;

}

/**

* 远程拷贝文件

* @param remoteFile 远程源文件路径

* @param localTargetDirectory 本地存放文件路径

*/

public Map 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");

}

/**

* 远程上传文件

* @param localFile 本地文件路径

* @param remoteTargetDirectory 远程存放文件路径

*/

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());

}

}

/**

* 远程上传文件并对上传文件重命名

* @param localFile 本地文件路径

*@param remoteFileName远程文件名

* @param remoteTargetDirectory 远程存放文件路径

*@param mode 默认"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;

}

}

调用姿势:

Scpclient scpclient = Scpclient.getInstance(String IP, int port,String username, String passward);

// 拷贝

scpclient .getFile(...);

// 上传

scpclient .putFile(...);

最后,只想说一句,我们不生产代码,我们只是代码搬运工。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值