连接sftp并实时跟踪上传进度

更新时间:2023-08-01 22:09:17 阅读: 评论:0

连接sftp并实时跟踪上传进度
1.导⼊sftpf的jar包 jsch-0.1.54.jar (jar版本太低会影响测试效果)
2.配置⽂件
host=192.168.132.138
urname=root
password=root
privateKey=/home/webusr/.ssh
passphra=id_rsa
port=22
local=/data/sftp/silver2KF/
movePath=/usr/local/src/test
3.上传主 SftpConfig 类
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.apache.log4j.Logger;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
public class SftpConfig {
private Logger logger = Logger(SftpConfig.class);
private String host = null;// sftp服务器ip
private String urname = null;// ⽤户名
private String password = null;// 密码
private String privateKey = null;// 密钥⽂件路径如何的拼音
private String passphra = null;// 密钥⼝令
private Integer port = null;// 默认的sftp端⼝号是22
private String local = null;
private String movePath = null;
SftpMonitor monitor;
private static ChannelSftp sftp = null;
public String getHost() {
return host;
}破解游戏网站
public void tHost(String host) {
this.host = host;
}
public String getUrname() {
return urname;
}
提防的读音public void tUrname(String urname) {
this.urname = urname;
}
public String getPassword() {
return password;
}
public void tPassword(String password) {
this.password = password;
}
public String getPrivateKey() {
return privateKey;
}
public void tPrivateKey(String privateKey) {
this.privateKey = privateKey;
}
public String getPassphra() {
爱国的古诗词return passphra;
}
public void tPassphra(String passphra) {
this.passphra = passphra;
}
public Integer getPort() {
return port;
}
public void tPort(Integer port) {
this.port = port;
}
public String getLocal() {
return local;
}
public void tLocal(String local) {
this.local = local;
}
public String getMovePath() {
return movePath;
}
public void tMovePath(String movePath) {
}
/**
* 初始化参数
*/
public SftpConfig() {
Properties prop = new Properties();
InputStream in = getClass().getResourceAsStream("/sftpConfig.properties");  System.out.println("读取sftp配置⽂件");
try {
prop.load(in);
} catch (IOException e) {
<("sftp properties file no has", e);
}
this.host = Property("host");
this.urname = Property("urname");
this.password = Property("password");
this.privateKey = Property("privateKey");
this.passphra = Property("passphra");
this.port = Integer.Property("port", ""));
this.local = Property("local");
System.out.println("conn:" + getUrname() + "/" + getPassword() + "@"
+ getHost() + "\n" + getLocal() + "[TO]" + getMovePath());
}
/**
* 连接sftp
*
* @return
*/
public ChannelSftp getConnection() {
System.out.println("连接sftp通道");
JSch jSch = new JSch();
Channel channel = null;
try {
最难的7字绕口令Session ssion = Session(urname, host, port);// 根据⽤户名,主机ip,端⼝获取⼀个Session对象  if (password != null && !"".equals(password)) {
ssion.tPassword(password);// 设置密码
}
Properties config = new Properties();
config.put("urauth.gssapi-with-mic", "no");// SSH连接慢的问题
config.put("StrictHostKeyChecking", "no");// 防⽌远程主机公钥改变导致 SSH 连接失败
ssion.tConfig(config); // 为Session对象设置properties
ssion.tServerAliveInterval(92000);// 请求时长
channel = ssion.openChannel("sftp"); // 指定连接sftp
} catch (JSchException e) {
e.printStackTrace();
}
sftp = (ChannelSftp) channel;
return  sftp;
}
/**
* ⽂件上传
*
* @param directory
4月30日*            上传的⽬录
* @param uploadFile
*            需要上传的⽂件
*/
public void upload(String directory, File uploadFile) {
System.out.println("上传的⽬录:" + directory + ";上传的⽂件的路径:"
+ Path() + "; ⽂件名称 :" + Name());
monitor = new SftpMonitor(uploadFile.length());
try {
sftp.cd(directory);
sftp.put(new FileInputStream(uploadFile), Name(),
monitor);
} catch (Exception e) {
e.printStackTrace();
monitor.stop();
}
}
/
**
* 下载⽂件
*
* @param directory
*            下载⽬录
* @param downloadFile
*            下载的⽂件
*            下载的⽂件
* @param saveFile
*            存在本地的路径
* @param sftp
*/
public void download(String directory, String downloadFile,
String saveFile, ChannelSftp sftp) {
try {
sftp.cd(directory);
<(downloadFile, saveFile);
} catch (Exception e) {
e.printStackTrace();
<(e);
}
}
/
**
* 断开连接
*
* @param sftp
* @throws JSchException
*/
public void disconnected() throws JSchException {
if (sftp != null) {
sftp.disconnect();
}
}
}
4.实现接⼝SftpProgressMonitor, Runnable
SftpProgressMonitor:主要⽬的是获取开上传与上传结束
Runnable:实时获取上传进度
NumberFormat;
import urrent.Executors;
import urrent.ScheduledExecutorService;
import urrent.TimeUnit;
import org.apache.log4j.Logger;
import com.jcraft.jsch.SftpProgressMonitor;
public class SftpMonitor implements SftpProgressMonitor, Runnable {
private long maxCount = 0;// ⽂件的总⼤⼩
public long startTime = 0L;
private long uploaded = 0;
private boolean isScheduled = fal;
private Logger logger = Logger(SftpMonitor.class);
ScheduledExecutorService executorService;
public SftpMonitor(long maxCount) {
this.maxCount = maxCount;
}
@Override
public void run() {
NumberFormat format = PercentInstance();
format.tMaximumFractionDigits(2);
format.tMinimumFractionDigits(2);
日本电影大全String value = format.format((uploaded / (double) maxCount));
System.out.println("已传输:" + uploaded / 1024 + "KB,传输进度:" + value);
if (uploaded == maxCount) {
stop();
long endTime = System.currentTimeMillis();
System.out.println("传输完成!⽤时:" + (endTime - startTime) / 1000 + "s");
}
}
/**
* 输出每个时间段的上传⼤⼩
*/
@Override
public boolean count(long count) {
if (!isScheduled) {
createTread();
尊敬老师的故事}
uploaded += count;
// System.out.println("本次上传⼤⼩:" + count / 1024 + "KB,");
if (count > 0) {
return true;
}
return fal;
}
/**
* ⽂件上传结束时调⽤
*/
@Override
public void end() {
// System.out.println("⽂件传输结束");
}
/**
* ⽂件上传时开始调⽤
*/
@Override
public void init(int op, String src, String dest, long max) {
System.out.println("开始上传⽂件:" + src + "⾄远程:" + dest + "⽂件总⼤⼩:" + maxCount    / 1024 + "KB");
startTime = System.currentTimeMillis();
}
/**
* 创建⼀个线程每隔⼀定时间,输出⼀下上传进度
*/
public void createTread() {
executorService = wSingleThreadScheduledExecutor();
// 1秒钟后开始执⾏,每2杪钟执⾏⼀次
executorService.scheduleWithFixedDelay(this, 1, 2, TimeUnit.SECONDS);
isScheduled = true;
}
/**
* 停⽌⽅法
*/
public void stop() {
boolean isShutdown = executorService.isShutdown();
if (!isShutdown) {
executorService.shutdown();
}
}
}

本文发布于:2023-08-01 22:09:17,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1104832.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:上传   连接   路径   结束   导致   获取
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图