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 ...
随机推荐
- 关于视频YUV
这里有一篇摘自MSDN的文章.介绍了YUV视频数据格式. About YUV Video Digital video is often encoded in a YUV format. This ar ...
- storm源码阅读笔记之任务调度算法
3种Scheduler概述 EventScheduler:将系统中的可用资源均匀地分配给需要资源的topology,其实也不是绝对均匀,后续会详细说明 DefaultScheduler:和Evenet ...
- 转:Socket常用选项
功能描述 获取或者设置与某个套接字关联的选 项.选项可能存在于多层协议中,它们总会出现在最上面的套接字层.当操作套接字选项时,选项位于的层和选项的名称必须给出.为了操作套接字层的选项,应该 将层的值指 ...
- iOS开发:icon和启动图尺寸(转)
转至:http://www.cnblogs.com/shaoting/p/4941634.html 歪果仁的总结: Asset iPhone 6s Plus and iPhone 6 Plus (@3 ...
- C#指南,重温基础,展望远方!(11)C#委托
委托类型表示对具有特定参数列表和返回类型的方法的引用. 通过委托,可以将方法视为可分配给变量并可作为参数传递的实体. 委托类似于其他一些语言中的函数指针概念,但与函数指针不同的是,委托不仅面向对象,还 ...
- HDU 4927 Series 1 ( 组合+高精度)
pid=4927">Series 1 大意: 题意不好翻译,英文看懂也不是非常麻烦,就不翻译了. Problem Description Let A be an integral se ...
- 使用PsExec tool在Session 0下运行程序
在Service程序中使用OutputDebugString输出log信息, 在当前用户直接运行DbgView.exe, log信息是不会输出到DbgView窗口的.原因是Server程序运行在Ses ...
- C# BeginInvoke和EndInvoke方法
转载自:BeginInvoke和EndInvoke方法 IDE:Visual Studio 2008 本系列教程主要包括如下内容:1. BeginInvoke和EndInvoke方法 2. Threa ...
- 网站收到的url请求链接,字母全部变为小写
http://www.ithao123.cn/content-5360465.html
- 稳固而窒息 jquery attr 和 Prop的区别
通常在获取或者设置checked,selected,readonly,disabled等的时候使用prop效果更好 attr是通过setAtrribute和getAttribute来设置的,使用的是D ...