channelsftp的put_java实现sftp客户端上传文件以及文件夹的功能代码
1.依赖的jar文件 jsch-0.1.53.jar
2.登录方式有密码登录,和密匙登录
代码:
主函数:
import java.util.Properties;
import com.cloudpower.util.Login;
import com.util.LoadProperties;
public class Ftp {
public static void main(String[] args) {
Properties properties = LoadProperties.getProperties();
Login.login(properties);
}
}
登陆页面的代码:
package com.cloudpower.util;
import java.io.Console;
import java.util.Properties;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
public class Login {
public static void login(Properties properties) {
String ip = properties.getProperty("ip");
String user = properties.getProperty("user");
String pwd = properties.getProperty("pwd");
String port = properties.getProperty("port");
String privateKeyPath = properties.getProperty("privateKeyPath");
String passphrase = properties.getProperty("passphrase");
String sourcePath = properties.getProperty("sourcePath");
String destinationPath = properties.getProperty("destinationPath");
if (ip != null && !ip.equals("") && user != null && !user.equals("")
&& port != null && !port.equals("") && sourcePath != null
&& !sourcePath.equals("") && destinationPath != null
&& !destinationPath.equals("")) {
if (privateKeyPath != null && !privateKeyPath.equals("")) {
sshSftp2(ip, user, Integer.parseInt(port), privateKeyPath,
passphrase, sourcePath, destinationPath);
} else if (pwd != null && !pwd.equals("")) {
sshSftp(ip, user, pwd, Integer.parseInt(port), sourcePath,
destinationPath);
} else {
Console console = System.console();
System.out.print("Enter password:");
char[] readPassword = console.readPassword();
sshSftp(ip, user, new String(readPassword),
Integer.parseInt(port), sourcePath, destinationPath);
}
} else {
System.out.println("请先设置配置文件");
}
}
/**
* 密码方式登录
*
* @param ip
* @param user
* @param psw
* @param port
* @param sPath
* @param dPath
*/
public static void sshSftp(String ip, String user, String psw, int port,
String sPath, String dPath) {
System.out.println("password login");
Session session = null;
JSch jsch = new JSch();
try {
if (port <= 0) {
// 连接服务器,采用默认端口
session = jsch.getSession(user, ip);
} else {
// 采用指定的端口连接服务器
session = jsch.getSession(user, ip, port);
}
// 如果服务器连接不上,则抛出异常
if (session == null) {
throw new Exception("session is null");
}
// 设置登陆主机的密码
session.setPassword(psw);// 设置密码
// 设置第一次登陆的时候提示,可选值:(ask | yes | no)
session.setConfig("StrictHostKeyChecking", "no");
// 设置登陆超时时间
session.connect(300000);
UpLoadFile.upLoadFile(session, sPath, dPath);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("success");
}
/**
* 密匙方式登录
*
* @param ip
* @param user
* @param port
* @param privateKey
* @param passphrase
* @param sPath
* @param dPath
*/
public static void sshSftp2(String ip, String user, int port,
String privateKey, String passphrase, String sPath, String dPath) {
System.out.println("privateKey login");
Session session = null;
JSch jsch = new JSch();
try {
// 设置密钥和密码
// 支持密钥的方式登陆,只需在jsch.getSession之前设置一下密钥的相关信息就可以了
if (privateKey != null && !"".equals(privateKey)) {
if (passphrase != null && "".equals(passphrase)) {
// 设置带口令的密钥
jsch.addIdentity(privateKey, passphrase);
} else {
// 设置不带口令的密钥
jsch.addIdentity(privateKey);
}
}
if (port <= 0) {
// 连接服务器,采用默认端口
session = jsch.getSession(user, ip);
} else {
// 采用指定的端口连接服务器
session = jsch.getSession(user, ip, port);
}
// 如果服务器连接不上,则抛出异常
if (session == null) {
throw new Exception("session is null");
}
// 设置第一次登陆的时候提示,可选值:(ask | yes | no)
session.setConfig("StrictHostKeyChecking", "no");
// 设置登陆超时时间
session.connect(300000);
UpLoadFile.upLoadFile(session, sPath, dPath);
System.out.println("success");
} catch (Exception e) {
e.printStackTrace();
}
}
}
文件上传的代码:
package com.cloudpower.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Scanner;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
public class UpLoadFile {
public static void upLoadFile(Session session, String sPath, String dPath) {
Channel channel = null;
try {
channel = (Channel) session.openChannel("sftp");
channel.connect(10000000);
ChannelSftp sftp = (ChannelSftp) channel;
try {
sftp.cd(dPath);
Scanner scanner = new Scanner(System.in);
System.out.println(dPath + ":此目录已存在,文件可能会被覆盖!是否继续y/n?");
String next = scanner.next();
if (!next.toLowerCase().equals("y")) {
return;
}
} catch (SftpException e) {
sftp.mkdir(dPath);
sftp.cd(dPath);
}
File file = new File(sPath);
copyFile(sftp, file, sftp.pwd());
} catch (Exception e) {
e.printStackTrace();
} finally {
session.disconnect();
channel.disconnect();
}
}
public static void copyFile(ChannelSftp sftp, File file, String pwd) {
if (file.isDirectory()) {
File[] list = file.listFiles();
try {
try {
String fileName = file.getName();
sftp.cd(pwd);
System.out.println("正在创建目录:" + sftp.pwd() + "/" + fileName);
sftp.mkdir(fileName);
System.out.println("目录创建成功:" + sftp.pwd() + "/" + fileName);
} catch (Exception e) {
// TODO: handle exception
}
pwd = pwd + "/" + file.getName();
try {
sftp.cd(file.getName());
} catch (SftpException e) {
// TODO: handle exception
e.printStackTrace();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (int i = 0; i < list.length; i++) {
copyFile(sftp, list[i], pwd);
}
} else {
try {
sftp.cd(pwd);
} catch (SftpException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("正在复制文件:" + file.getAbsolutePath());
InputStream instream = null;
OutputStream outstream = null;
try {
outstream = sftp.put(file.getName());
instream = new FileInputStream(file);
byte b[] = new byte[1024];
int n;
try {
while ((n = instream.read(b)) != -1) {
outstream.write(b, 0, n);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (SftpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
outstream.flush();
outstream.close();
instream.close();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
}
}
}
读取配置文件的代码:
package com.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class LoadProperties {
public static Properties getProperties() {
File file = new File(Class.class.getClass().getResource("/").getPath()
+ "properties.properties");
InputStream inputStream = null;
try {
inputStream = new FileInputStream(file);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Properties properties = new Properties();
try {
properties.load(inputStream);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return properties;
}
}
代码目录结构:
测试运行时配置文件放在项目的bin目录下(打包成可运行jar文件的时候要删除,打包完成后将配置文件和jar包放在同级目录下即可):
properties.properties
ip=
user=
pwd=
port=22
privateKeyPath=
passphrase=
sourcePath=
destinationPath=/home/dbbs/f
打包可运行jar文件:
Export->java->Runnabe JAR file
完成后
在控制台运行java -jar 导出jar包的名字.jar 即可
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
channelsftp的put_java实现sftp客户端上传文件以及文件夹的功能代码相关推荐
- Java从sftp服务器上传与下载文件
一.背景 业务需要从sftp服务器上上传.下载.删除文件等功能,通过查阅资料及手动敲打代码,实现了操作sftp的基本功能,有需求的小伙伴可以看看具体的实现过程. 二.sftp介绍 摘自百度百科:SSH ...
- linux环境下,模拟百度网盘上传、下载文件
目录 1.题目 2.运行截图 3.总体设计 4.详细设计 5.源码 5.1服务端 5.2客户端 1.题目 1)模仿百度网盘实现一个文件上传.下载.浏览的终端网盘; 2)能够实现文件和目录的存储; 3) ...
- Python监控目录文件夹,并使用SFTP上传目录及文件到linux服务器
Python 扫描监控本地文件夹并进行超大文件上传 方案1:WebUploader大文件分块多线程并发上传 方案2:watchdog目录文件夹监控,paramiko STFP上传服务器 方案3:优化2 ...
- Silverlight从客户端上传文件到服务器
这里介绍的是一种利用WebClient手动发送Stream到服务器页面的上传文件方法. 一.服务器接收文件 这里使用一个ASHX页面来接收和保存Silverlight传来的Stream,页面代码如下: ...
- python sftp_Python使用sftp实现上传和下载功能(实例代码)
在Python中可以使用paramiko模块中的sftp登陆远程主机,实现上传和下载功能. 1.功能实现 根据输入参数判断是文件还是目录,进行上传和下载 本地参数local需要与远程参数remote类 ...
- java文件客户端下载_使用Java写一个minio的客户端上传下载文件
标签:color ati tty java system wired format media param 前言: 确保已经安装了minio的服务端 代码: pom.x ...
- JAVA客户端数据传输_java模拟TCP通信实现客户端上传文件到服务器端
java模拟TCP通信实现客户端上传文件到服务器端,供大家参考,具体内容如下 客户端 package com.zr; import java.io.FileInputStream; import ja ...
- 将本地项目上传到Github的两种方式 1.在线上传 2.使用Git客户端上传
文章目录 注册GitHub账号并创建仓库 上传本地项目到Github的方式一:在线上传 上传本地项目到Github的方式二:使用Git客户端上传 Windows下安装Git客户端 Git配置本地用户名 ...
- Struts提供我们方便地将客户端上传的文件处理
以下是本人的一些分享,我热爱编程,希望能多交编程的爱好者,如果你也是其中一名,那么请加好友,大家关注一下,下面的文章是自己觉得一些有用的东西,留下来给自己当笔记,当然也希望能帮助到你,首先感谢你的阅读 ...
最新文章
- BrainNet:用于人与人之间直接协作的多人脑对脑接口
- LeetCode_每日一题今日份_329.矩阵中的最长递增路径(没懂)
- Cloud for Customer UI里点了超链接后的跳转处理
- 百安俱乐部关于“BotNet专题讨论”资料
- ASP.NET中的HTTP模块和处理程序
- 精通PHP的十大要点
- Netsuite Foreign Currency Revaluation 外币评估
- GitHub 给已存在的仓库增加开原协议LICENSE
- CCNA考试题库,考试真题
- Python的常用Matplotlib图形工具
- 阿里云服务器租用报价新鲜出炉(轻量和ECS价格)
- 家用无线路由器服务器,ZOL实测:家用无线路由器WiFi性能排行榜
- oracle单列转行,oracle 两种列转行的方式
- 使用opencv实现通过摄像头自动输入阿里云身份宝验证码
- lr背景虚化_LR调色教程,用LR把夏天的颜色以图片的样式呈现出来
- 【css】纯css实现table表格固定表头,表内容滚动
- win7原版镜像_AMD平台B450主板安装WIN7教程
- 国际惯例,个人流程分享
- 怎么在php中加入换行符,如何在PHP中创建换行符?
- 解决 enclosing instance of type XXX(你的类)(e.g. x.new A() wher x is an instance of XXX(你的类)).
热门文章
- bg感_【0328】BG推文 | 5本我在逃生游戏里养娃娃+岁月缱绻已无你+关于我比女主苏这回事+消失的白月光又回来了等...
- linux查服务器总内存大小,在linux 下怎么查看服务器的cpu和内存的硬件信息
- 【STC15库函数上手笔记】4、USART串口
- 【51单片机快速入门指南】5:软件SPI
- React 项目开发问题积累
- 在MAC下安装Exuberant ctags
- python中使用sys模板和logging模块获取行号和函数名的方法
- 文件逆顺输出到新文件(三种方案)
- vue如何引入ant部分组件
- Windows系统USB转CDC串口驱动限制说明