java通过SCP链接Linux服务器上传文件

    Connection connection = new Connection(ip,port);try {connection.connect();boolean isAuthenticated = connection.authenticateWithPassword(name,password);if(!isAuthenticated){System.out.println("连接建立失败");return ;}SCPClient scpClient = new SCPClient(connection);SCPOutputStream os = scpClient.put(f.getName(),length,remoteTargetDirectory,mode);byte[] b = new byte[4096];FileInputStream fis = new FileInputStream(f);int i;while ((i = fis.read(b)) != -1) {os.write(b, 0, i);}os.flush();fis.close();os.close();connection.close();}catch (IOException e) {e.printStackTrace();}

下载文件

    Connection connection = new Connection(ip,port);try {connection.connect();boolean isAuthenticated = connection.authenticateWithPassword(name,password);if(!isAuthenticated){System.out.println("连接建立失败");return ;}SCPClient scpClient = new SCPClient(connection);SCPOutputStream os = scpClient.put(f.getName(),length,remoteTargetDirectory,mode);byte[] b = new byte[4096];FileInputStream fis = new FileInputStream(f);int i;while ((i = fis.read(b)) != -1) {os.write(b, 0, i);}os.flush();fis.close();os.close();connection.close();System.out.println("upload ok");} catch (IOException e) {e.printStackTrace();}

相关jar包:

<dependency><groupId>ch.ethz.ganymed</groupId><artifactId>ganymed-ssh2</artifactId><version>262</version>
</dependency>

完整代码:

/*** java服务器通过SCP上传文件至Linux服务器*/
public class ScpClient {private static ScpClient instance;private String ip;private int port;private String name;private String password;/*** 私有化默认构造函数* 实例化对象只能通过getInstance*/private ScpClient(){}/*** 私有化有参构造函数* @param ip 服务器ip* @param port 服务器端口 22* @param name 登录名* @param password 登录密码*/private ScpClient(String ip,int port,String name,String password){this.ip = ip ;this.port = port;this.name = name;this.password = password;}/*** download* @param remoteFile 服务器上的文件名* @param remoteTargetDirectory 服务器上文件的所在路径* @param newPath 下载文件的路径*/public void downloadFile(String remoteFile, String remoteTargetDirectory,String newPath){Connection connection = new Connection(ip,port);try {connection.connect();boolean isAuthenticated = connection.authenticateWithPassword(name,password);if(isAuthenticated){SCPClient scpClient = connection.createSCPClient();SCPInputStream sis = scpClient.get(remoteTargetDirectory + "/" + remoteFile);File f = new File(newPath);if(!f.exists()){f.mkdirs();}File newFile = new File(newPath + remoteFile);FileOutputStream fos = new FileOutputStream(newFile);byte[] b = new byte[4096];int i;while ((i = sis.read(b)) != -1){fos.write(b,0, i);}fos.flush();fos.close();sis.close();connection.close();System.out.println("download ok");}else{System.out.println("连接建立失败");}} catch (IOException e) {e.printStackTrace();}}/***  获取服务器上相应文件的流* @param remoteFile 文件名* @param remoteTargetDirectory 文件路径* @return* @throws IOException*/public SCPInputStream getStream(String remoteFile, String remoteTargetDirectory) throws IOException {Connection connection = new Connection(ip,port);connection.connect();boolean isAuthenticated = connection.authenticateWithPassword(name,password);if(!isAuthenticated){System.out.println("连接建立失败");return null;}SCPClient scpClient = connection.createSCPClient();return scpClient.get(remoteTargetDirectory + "/" + remoteFile);}/*** 上传文件到服务器* @param f 文件对象* @param length 文件大小* @param remoteTargetDirectory 上传路径* @param mode 默认为null*/public void uploadFile(File f, long length, String remoteTargetDirectory, String mode) {Connection connection = new Connection(ip,port);try {connection.connect();boolean isAuthenticated = connection.authenticateWithPassword(name,password);if(!isAuthenticated){System.out.println("连接建立失败");return ;}SCPClient scpClient = new SCPClient(connection);SCPOutputStream os = scpClient.put(f.getName(),length,remoteTargetDirectory,mode);byte[] b = new byte[4096];FileInputStream fis = new FileInputStream(f);int i;while ((i = fis.read(b)) != -1) {os.write(b, 0, i);}os.flush();fis.close();os.close();connection.close();System.out.println("upload ok");} catch (IOException e) {e.printStackTrace();}}/*** 单例模式* 懒汉式* 线程安全* @return*/public static ScpClient getInstance(){if(null == instance){synchronized (ScpClient.class){if(null == instance){instance = new ScpClient();}}}return instance;}public static ScpClient getInstance(String ip,int port,String name,String password){if(null == instance){synchronized (ScpClient.class){if(null == instance){instance = new ScpClient(ip,port,name,password);}}}return instance;}public String getIp() {return ip;}public void setIp(String ip) {this.ip = ip;}public int getPort() {return port;}public void setPort(int port) {this.port = port;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}
}

demo:https://github.com/running17/scp

Java服务器通过SCP连接Linux服务器上传、下载文件相关推荐

  1. SecureCRTSecureFX Portable远程连接Linux;上传下载文件

    SecureCRT和SecureFX都是由VanDyke出品的SSH传输工具. SecureCRT是一款非常好用的.支持多标签的SSH客户端,极大方便了管理多个SSH会话. SecureFX则是一款专 ...

  2. 在Windows上使用终端模拟程序连接操作Linux以及上传下载文件

    在Windows上使用终端模拟程序连接操作Linux以及上传下载文件 [很简单,就是一个工具的使用而已,放这里是做个笔记.] 刚买的云主机,或者是虚拟机里安装的Linux系统,可能会涉及到在windo ...

  3. linux 传文件夹,linux下上传下载文件夹的方法

    Linux下目录复制:本机->远程服务器 scp -r /home/shaoxiaohu/test1 zhidao@192.168.0.1:/home/test2 test1为源目录,test2 ...

  4. Ubuntu SSH连接、远程上传下载文件

    Ubuntu SSH连接.远程上传下载文件 目录 SSH 概念 安装 SSH(Secure Shell) 服务以提供远程管理服务 将文件/文件夹从远程 Ubuntu 机拷至本地(scp) 将文件/文件 ...

  5. 关于FileZilla连接FTP站点上传下载文件

    关于FileZilla连接FTP站点上传下载文件 浏览器搜索FileZilla官网:https://www.filezilla.cn/download 根据自己操作系统安装 安装完成之后的启动界面是这 ...

  6. Xftp7对Linux远程上传/下载文件

    1:打开软件: 2:新建会话: 文件->打开: 接着点击新建: 然后输入信息,名称可以自定义,主机填Linux的ip地址,协议为SFTP,端口号为22, 下方的用户名和密码填上Linux端的用户 ...

  7. linux快捷上传下载文件

    借助securtCRT,使用linux命令sz可以很方便的将服务器上的文件下载到本地,使用rz命令则是把本地文件上传到服务器 其中,对于sz和rz的理解与记忆我用了如下的方法(因为很多时候容易搞混): ...

  8. linux ftp上传下载文件,Linux下ftp命令上传下载文件

    命令行下连接ftp服务器 方式一: 默认端口 ftp hostname 方式二: 指定端口 [ec2-user@ip-99-240-80-144 ~]$ ftp ftp> open 99.240 ...

  9. 使用scp命令,远程上传下载文件/文件夹

    1.从服务器下载文件 scp username@servername:/path/filename /local/path 例如: scp ubuntu@117.50.20.56:/ygf/data/ ...

  10. linux ssh上传下载文件命令SCP使用方法

    在linux环境里,我们从服务器上下载或者从本地上传文件到服务器上可以通过SCP命令来实现. SCP即Security Copy,是基于SSH登录实现的远程文件拷贝命令. 命令参数: -r: 递归复制 ...

最新文章

  1. python之dict基础类型
  2. java中正则表达式截取字符串
  3. 计算机网络简历自我认识,计算机网络专业简历的自我评价
  4. 常来长安——西安游记(我愿称之为博物馆七日游)
  5. D2RQ 的安装和基本使用
  6. 修改Cisco交换机ntp服务器,Cisco交换机NTP的配置
  7. python天眼查爬虫_普通用户的天眼查爬取
  8. 《用事实说话》阅读心得
  9. mysql动态ip域名连接_用本地动态IP连接本地mysql
  10. 提取 Excel 指定单元格数据
  11. 硬件相关技术资料分享
  12. C51单片机1.点亮一个LED灯
  13. Java中求集合交集、并集、差集
  14. 如何掌握程序语言(转自王垠Blog)
  15. 数据结构课设--用B树实现图书管理系统
  16. java vips_Java IConfigManager.getAllVIPs方法代碼示例
  17. 《cell》最新动态2021年4月
  18. 滚动轴承退化趋势预测
  19. XDOJ 1-1 车牌限行
  20. 阿里、京东等大厂年薪50w的测试都是什么水平?

热门文章

  1. 这两天用鸿蒙开发板做了个“自动门锁”
  2. 网页设计1-1李清照人物简介
  3. javaweb JSP JAVA 酒店预订系统j酒店管理系统民宿预订)酒店客房预订系统宾馆客房预订系统
  4. 重读“发展Linux,中日两国之比较”有感
  5. 明日之后服务器维护到几点,明日之后维护最新资讯
  6. 华为鸿蒙爆出惊天骗局,华为鸿蒙系统爆出惊天骗局!
  7. 知识兔课程揭秘2021抖音卖货代运营的新骗局,你中招了吗?
  8. Word转成PDF后目录出现未定义书签是怎么回事
  9. MLX90614系列 ——红外温度计
  10. 画二元函数即三维图像的函数及matlab代码