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 ...
随机推荐
- Android开发优化之——从代码角度进行优化
通常我们写程序,都是在项目计划的压力下完成的,此时完成的代码可以完成具体业务逻 辑,但是性能不一定是最优化的.一般来说,优秀的程序员在写完代码之后都会不断的对代码进行重构.重构的好处有很多,其中一点, ...
- 本地ubuntu下pycharm 如何利用远程开发环境时显示图片
最近使用pycharm远程开发tensorflow,每次在想显示图像时,苦于不知怎么操作,就通过保存后再看结果,使得调试很不方便.今天打算解决这个问题,收获也是很多啊. 我首先参考了这两篇博客: ht ...
- android VLayout 全面解析
概述 前不久.阿里新开源了2个东西,Atlas和vlayout.今天我来介绍下vlayout的使用. 在介绍前,先抱怨两句,阿里放开源出来,感觉就是让我们这群人给他们找bug~~我曾遇到一个奇怪的问题 ...
- UniCode 下 CString 转 char* 的方法
今天进行文件操作时,将CString的GetBuffer()后直接倒到char数组后写入文件发现 每个字符与字符之间都有一个空格存在,而且有内容丢失.原来CString类对象GetBuffer(),后 ...
- HDU 4946 Area of Mushroom 共线凸包
题意是在二维平面上 给定n个人 每一个人的坐标和移动速度v 若对于某个点,仅仅有 x 能最先到达(即没有人能比x先到这个点或者同一时候到这个点) 则这个点称作被x占有 若有人能占有无穷大的面积 则输出 ...
- 在Quartus中如何使用TCL脚本文件配制管脚
quartus软件分配管脚的方法有两种,一是选择菜单“assignments->pins”进入管脚分配视图手动分配:第二种方法是利用tcl脚本文件自动分配.这里我来介绍第二种方法. 1.生成tc ...
- mybatis-spring 项目简介
mybatis-spring 主要是为了解决 spring3.0 中支持 mybatis3.0 1 mybatis-spring 主要包含以下几个部分 数据源 SqlSessionFactory Sq ...
- 宏里面的(void)0
在<c标准库>实现assert.h中有一个语句: #define assert(test) ((test)?(void)0 : _Assert(__FILE__":"_ ...
- python—networkx:在一张图中画出多个子图
通过plt.subplot能够在一张图中画出多个子图 #coding: utf-8 #!/usr/bin/env python """ Draw a graph with ...
- 开源项目AndroidUtil-採用Fragment实现TabHost
原文出自:方杰|http://fangjie.sinaapp.com/?p=141 转载请注明出处 学习Android也有一段时间了.感觉大部分的Android应用都有非常多类似的组件,所以就打算做了 ...