package com.sun.test.Jsch;
import com.jcraft.jsch.SftpProgressMonitor;
import java.text.NumberFormat;
import java.util.concurrent.ScheduledExecutorService;
public class UploadMonitor implements SftpProgressMonitor {
/**
* 文件的总大小
*/
private long maxCount = 0;
private long uploaded = 0;
long startTime = 0L;
//private boolean isScheduled = false;
ScheduledExecutorService executorService;
/*
public UploadMonitor(long maxCount) {
this.maxCount = maxCount;
}*/
/**
* 当文件开始传输时,调用init方法
*
* @param op
* @param src
* @param dest
* @param max
*/
@Override
public void init(int op, String src, String dest, long max) {
this.maxCount = max;
System.out.println("开始上传文件:" + src + "至远程:" + dest + "文件总大小:" + maxCount / 1024 + "KB");
startTime = System.currentTimeMillis();
}
/**
* 当每次传输了一个数据块后,调用count方法,count方法的参数为这一次传输的数据块大小
*
* @param count
* @return
*/
@Override
public boolean count(long count) {
/* if (!isScheduled) {
createTread();
}*/
uploaded += count;
System.out.println("本次上传大小:" + count / 1024 + "KB,");
NumberFormat format = NumberFormat.getPercentInstance();
format.setMaximumFractionDigits(2);
format.setMinimumFractionDigits(2);
String value = format.format((uploaded / (double) maxCount));
System.out.println("已传输:" + uploaded / 1024 + "KB,传输进度:" + value);
if (count > 0) {
return true;
}
return false;
}
/**
* 当传输结束时,调用end方法
*/
@Override
public void end() {
long endTime = System.currentTimeMillis();
System.out.println(endTime);
System.out.println("传输完成!用时:" + (endTime - startTime) / 1000 + "s");
}
/* @Override
public void run() {
if (uploaded == maxCount) {
stop1();
}
}*/
/* *//**
* 创建一个线程每隔一定时间,输出一下上传进度
*//*
public void createTread() {
executorService = Executors.newSingleThreadScheduledExecutor();
//1秒钟后开始执行,每2杪钟执行一次
executorService.scheduleWithFixedDelay(this, 1, 2, TimeUnit.SECONDS);
isScheduled = true;
}
public void stop1() {
boolean isShutdown = executorService.isShutdown();
if (!isShutdown) {
executorService.shutdown();
}
}
*/
}
package com.sun.test.Jsch;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
public class demo {
public static void main(String[] args) throws FileNotFoundException{
/* FileInputStream is = new FileInputStream
(new File("C:/Users/80965/Desktop/aaa.txt"));*/
String winpath="C:/Users/80965/Desktop/模型管理-修正后1214.rar";
String url="";
int port=22;
String username="";
String password="";
String path="/root/a";
String fileName="模型管理-修正后1214.rar";
storeFile(url, port, username, password, path, fileName, winpath);
}
public static boolean storeFile(String url,int port,String username,String password,
String path,String fileName,String winpath){
boolean result=false;
ChannelSftp ftp=null;
Session sshSession=null;
FileInputStream is=null;
try {
is = new FileInputStream
(new File(winpath));
JSch jsch = new JSch();
sshSession =
jsch.getSession(username, url, port);
sshSession.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
// 开启sshSession链接
sshSession.connect();
// 获取sftp通道
ftp = (ChannelSftp) sshSession.openChannel("sftp");
// 开启
ftp.connect();
System.out.println(ftp.pwd());
ftp.cd("/");
System.out.println(ftp.pwd());
createDir(path, ftp);
ftp.cd(path);
UploadMonitor monitor =
new UploadMonitor();
ftp.put(winpath, fileName, monitor, ChannelSftp.OVERWRITE);
/*ftp.put(is, fileName);*/
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// 判断输出流是否存在
if (null != is) {
// 关闭输出流
is.close();
}
// 判断连接是否存在
if (null!=ftp&&ftp.isConnected()) {
// 断开连接
ftp.disconnect();
}
if(null!=sshSession&& sshSession.isConnected()){
sshSession.disconnect();
}
result=true;
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
public static boolean isDirExist(String directory, ChannelSftp sftp) {
boolean is=false;
try {
if(sftp.ls(directory) != null){
is=true;
}
} catch (SftpException e) {
System.out.println(e.getMessage());
}
return is;
}
public static void createDir(String createpath, ChannelSftp sftp) throws SftpException {
if (isDirExist(createpath, sftp)) {
sftp.cd(createpath);
}
String pathArr[] = createpath.split("/");
StringBuffer filePath = new StringBuffer("/");
for (String path : pathArr) {
if (path.equals("")) {
continue;
}
filePath.append(path + "/");
if (isDirExist(filePath.toString(), sftp)) {
sftp.cd(filePath.toString());
} else {
// 建立目录
sftp.mkdir(filePath.toString());
// 进入并设置为当前目录
sftp.cd(filePath.toString());
}
}
sftp.cd(createpath);
}
}