Java 执行linux scp 远程获取文件和上传
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SCPClient;
public class Scpclient {
// public Scpclient(){}
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;
}
public void getFile(String remoteFile, String localTargetDirectory) {
Connection conn = new Connection(ip,port);
try {
conn.connect();
boolean isAuthenticated = conn.authenticateWithPassword(username,
password);
if (isAuthenticated == false) {
System.err.println("authentication failed");
}
SCPClient client = new SCPClient(conn);
client.get(remoteFile, localTargetDirectory);
conn.close();
} catch (IOException ex) {
Logger.getLogger(SCPClient.class.getName()).log(Level.SEVERE, null,ex);
}
}
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) {
System.err.println("authentication failed");
}
SCPClient client = new SCPClient(conn);
client.put(localFile, remoteTargetDirectory);
conn.close();
} catch (IOException ex) {
Logger.getLogger(SCPClient.class.getName()).log(Level.SEVERE, null,ex);
}
}
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) {
System.err.println("authentication failed");
}
SCPClient client = new SCPClient(conn);
if((mode == null) || (mode.length() == 0)){
mode = "0600";
}
client.put(localFile, remoteFileName, remoteTargetDirectory, mode);
//重命名
ch.ethz.ssh2.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 ex) {
Logger.getLogger(SCPClient.class.getName()).log(Level.SEVERE, null,ex);
}
}
// public void putFile(String localFile, String remoteFileName,String remoteTargetDirectory) {
// Connection conn = new Connection(ip,port);
// try {
// conn.connect();
// boolean isAuthenticated = conn.authenticateWithPassword(username,
// password);
// if (isAuthenticated == false) {
// System.err.println("authentication failed");
// }
// SCPClient client = new SCPClient(conn);
// client.put(getBytes(localFile), remoteFileName, remoteTargetDirectory);
// conn.close();
// } catch (IOException ex) {
// Logger.getLogger(SCPClient.class.getName()).log(Level.SEVERE, null,ex);
// }
// }
public static byte[] getBytes(String filePath) {
byte[] buffer = null;
try {
File file = new File(filePath);
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream byteArray = new ByteArrayOutputStream(1024*1024);
byte[] b = new byte[1024*1024];
int i;
while ((i = fis.read(b)) != -1) {
byteArray.write(b, 0, i);
}
fis.close();
byteArray.close();
buffer = byteArray.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return buffer;
}
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 scp = Scpclient.getInstance(ip, port,uname,pwd);
scp.putFile("本地文件路径", fileFeed.getName()+".tmp", "推送文件,到服务器的目录路径", null);
scp.getFile(remoteFile, localTargetDirectory);
Java 执行linux scp 远程获取文件和上传的更多相关文章
- linux scp 远程复制文件
1.从本机复制文件到远程scp 文件名 远程计算机用户名@远程计算机的ip:远程计算机存放该文件的路径2.从远程复制文件到本机:scp 远程计算机用户名@远程计算机ip:文件名 存放该文件的本机路径3 ...
- linux scp远程拷贝文件及文件夹
[http://www.jb51.net/LINUXjishu/73131.html] 1.拷贝本机/home/administrator/test整个目录至远程主机192.168.1.100的/ro ...
- 【转】linux scp远程拷贝文件及文件夹
转自:http://www.jb51.net/LINUXjishu/73131.html 1.拷贝本机/home/administrator/test整个目录至远程主机192.168.1.100的/r ...
- Linux scp 远程文件/目录传输
Linux scp远程文件/目录传输 1.获取远程服务器上的文件 scp -P 22 root@www.test.com:/root/test.tar.gz /home/test.tar.gz 上端口 ...
- java执行cmd命令并获取输出结果
1.java执行cmd命令并获取输出结果 import java.io.BufferedReader; import java.io.InputStreamReader; import org.apa ...
- Linux下远程备份、上传工程,重启服务器
Linux下远程备份.上传工程,重启服务器 Linux服务器实现远程,原项目的备份.删除,新项目上传,以及远程重启服务器!分成一个主shell调用三个shell文件步骤完成.mainsh.sh一次按顺 ...
- Node.js:上传文件,服务端如何获取文件上传进度
内容概述 multer是常用的Express文件上传中间件.服务端如何获取文件上传的进度,是使用的过程中,很常见的一个问题.在SF上也有同学问了类似问题<nodejs multer有没有查看文件 ...
- Django积木块三——静态文件和上传文件
静态文件和上传的文件 # 静态文件 STATIC_URL = '/static/' STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'static'), ) # ...
- js获取文件上传进度
js获取文件上传进度: <input name="file" id="FileUpload" type="file" /> &l ...
随机推荐
- MVC中cshtml文件中怎样使用ViewBag进行筛选?
@foreach (PermissionForRoleModel item in ((List<PermissionForRoleModel>)ViewBag.PermissionsFor ...
- ORA-16014 与 ORA-00312
场景: SQL> alter system archive log current; alter system archive log current * 第 1 行出现错误: ORA-1601 ...
- java web 中的MVC
M:相当于Bean V:jsp C:servlet 当客户端发来请求,servlet响应请求处理请求,并把要发送给客户端的数据封装到Bean中,然后通过转发,将这个封装好了数据Bean送给jsp,js ...
- linux之进程管理详解
|-进程管理 进程常用命令 |- w查看当前系统信息 |- ps进程查看命令 |- kill终止进程 |- 一个存放内存中的特殊目 ...
- C#四舍五入保留一位小数
DateTime d1 = hrStaff.DateJoin; DateTime d2 = DateTime.Now; TimeSpan d3 = d2.Subtract(d1); ; //int i ...
- github get 请求指定页面的代码
https://raw.githubusercontent.com/dragon8github/wx-h5/master/push.sh
- Django URL中r的意思
例如: urlpatterns = patterns('', # ... (r'^time/plus/\d+/$', hours_ahead), # ... ) 正则表达式字符串的开头字母“r”. 它 ...
- 会话过程保存数据对象cookie和session
1 cookie是以键值对保存在浏览器端,服务器端可以创建.接收.发送 cookie 信息. request可以接收 cookie, response 可以发送 cookie. 1)cookie 可以 ...
- PHP域名whois查询代码(数据源万网、新网)
对于whois查询,数据来自万网.新网,数据也比较权威,需要的朋友可以参考下. 万网 whois(使用的接口为万网提供合法接口) function whois_hichina($domain) { ...
- 分布式ID生成方案
系统唯一ID是设计一个系统的时候常常会遇到的问题,也常常为这个问题而纠结. 生成ID的方法有很多,适应不同的场景.需求以及性能要求.所以有些比较复杂的系统会有多个ID生成的策略. 0. 分布式ID要求 ...