SftpUtil.java
package uoload;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import java.util.Vector;
import org.apache.commons.io.IOUtils;
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;
import com.jcraft.jsch.SftpException;
/**
* Created by xianggongtao on 2018/5/12
*/
public class SftpUtil {
//private transient Logger log = LoggerFactory.getLogger(this.getClass());
public static String installFilePath = "/opt/yfjz";
public static String toolServerIpAddr = "192.168.3.132";
public static int serverPort = 22;
public ChannelSftp sftp ;
//private Session session = null;
/** FTP 登录用户名*/
private String username;
/** FTP 登录密码*/
private String password;
/** 私钥 */
private String privateKey;
/** FTP 服务器地址IP地址*/
private String host;
/** FTP 端口*/
private int port;
//private Channel channel = null;
/**
* 构造基于密码认证的sftp对象
* @param username
* @param password
* @param host
* @param port
*/
public SftpUtil(String username, String password, String host, int port) {
this.username = username;
this.password = password;
this.host = host;
this.port = port;
}
/**
* 构造基于秘钥认证的sftp对象
* @param username
* @param host
* @param port
* @param privateKey
*/
public SftpUtil(String username, String host, int port, String privateKey) {
this.username = username;
this.host = host;
this.port = port;
this.privateKey = privateKey;
}
public SftpUtil(){}
/**
* 连接sftp服务器
*
* @throws Exception
*/
public ChannelSftp login(){
try {
JSch jsch = new JSch();
/*if (privateKey != null) {
jsch.addIdentity(privateKey);// 设置私钥
//log.info("sftp connect,path of private key file:{}" , privateKey);
}*/
//log.info("sftp connect by host:{} username:{}",host,username);
Session session = jsch.getSession(username, host, port);
//log.info("Session is build");
if (password != null) {
session.setPassword(password);
}
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
//log.info("Session is connected");
Channel channel = session.openChannel("sftp");
channel.connect();
//log.info("channel is connected");
sftp = (ChannelSftp) channel;
//log.info(String.format("sftp server host:[%s] port:[%s] is connect successfull", host, port));
System.out.println("登录成功!!");
} catch (JSchException e) {
e.printStackTrace();
//log.error("Cannot connect to specified sftp server : {}:{} \n Exception message is: {}", new Object[]{host, port, e.getMessage()});
}
return sftp;
}
/**
* 关闭连接 server
*/
public void logout(ChannelSftp sftp){
if (sftp != null) {
if (sftp.isConnected()) {
//sftp.disconnect();
sftp.exit();
//log.info("sftp is closed already");
}
}
/*if (channel != null) {
if (channel.isConnected()) {
channel.disconnect();
//log.info("sshSession is closed already");
}
}
if (session != null) {
if (session.isConnected()) {
session.disconnect();
//log.info("sshSession is closed already");
}
}*/
System.out.println("退出登录成功!!");
}
/**
* 将输入流的数据上传到sftp作为文件
*
* @param directory
* 上传到该目录
* @param sftpFileName
* sftp端文件名
* @param input
* 输入流
* @throws SftpException
* @throws Exception
*/
public void upload(String directory, String sftpFileName, InputStream input) throws SftpException{
try {
sftp.cd(directory);
} catch (SftpException e) {
//log.warn("directory is not exist");
sftp.mkdir(directory);
sftp.cd(directory);
}
sftp.put(input, sftpFileName);
try {
input.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//log.info("file:{} is upload successful" , sftpFileName);
}
/**
* 上传单个文件
*
* @param directory
* 上传到sftp目录
* @param uploadFile
* 要上传的文件,包括路径
* @throws FileNotFoundException
* @throws SftpException
* @throws Exception
*/
public void upload(String directory, String uploadFile) throws FileNotFoundException, SftpException{
File file = new File(uploadFile);
upload(directory, file.getName(), new FileInputStream(file));
}
/**
* 将byte[]上传到sftp,作为文件。注意:从String生成byte[]是,要指定字符集。
*
* @param directory
* 上传到sftp目录
* @param sftpFileName
* 文件在sftp端的命名
* @param byteArr
* 要上传的字节数组
* @throws SftpException
* @throws Exception
*/
public void upload(String directory, String sftpFileName, byte[] byteArr) throws SftpException{
upload(directory, sftpFileName, new ByteArrayInputStream(byteArr));
}
/**
* 将字符串按照指定的字符编码上传到sftp
*
* @param directory
* 上传到sftp目录
* @param sftpFileName
* 文件在sftp端的命名
* @param dataStr
* 待上传的数据
* @param charsetName
* sftp上的文件,按该字符编码保存
* @throws UnsupportedEncodingException
* @throws SftpException
* @throws Exception
*/
public void upload(String directory, String sftpFileName, String dataStr, String charsetName) throws UnsupportedEncodingException, SftpException{
upload(directory, sftpFileName, new ByteArrayInputStream(dataStr.getBytes(charsetName)));
}
/**
* 下载文件
*
* @param directory
* 下载目录
* @param downloadFile
* 下载的文件
* @param saveFile
* 存在本地的路径
* @throws SftpException
* @throws FileNotFoundException
* @throws Exception
*/
public void download(String directory, String downloadFile, String saveFile) throws SftpException, FileNotFoundException{
if (directory != null && !"".equals(directory)) {
sftp.cd(directory);
}
File file = new File(saveFile);
sftp.get(downloadFile, new FileOutputStream(file));
//log.info("file:{} is download successful" , downloadFile);
}
/**
* 下载文件
* @param directory 下载目录
* @param downloadFile 下载的文件名
* @return 字节数组
* @throws SftpException
* @throws IOException
* @throws Exception
*/
public byte[] download(String directory, String downloadFile) throws SftpException, IOException{
if (directory != null && !"".equals(directory)) {
sftp.cd(directory);
}
InputStream is = sftp.get(downloadFile);
byte[] fileData = IOUtils.toByteArray(is);
//log.info("file:{} is download successful" , downloadFile);
return fileData;
}
/**
* 删除文件
*
* @param directory
* 要删除文件所在目录
* @param deleteFile
* 要删除的文件
* @throws SftpException
* @throws Exception
*/
public void delete(String directory, String deleteFile) throws SftpException{
sftp.cd(directory);
sftp.rm(deleteFile);
}
/**
* 列出目录下的文件
*
* @param directory
* 要列出的目录
* @return
* @throws SftpException
*/
public Vector> listFiles(String directory) throws SftpException {
return sftp.ls(directory);
}
public static void main(String[] args) throws SftpException, IOException {
/*SftpUtil sftp = new SftpUtil("root", "agent", "192.168.3.148", 22);
sftp.login();
sftp.logout();
System.out.println("---------------");*/
/*while (true) {
int i= (int) (Math.random()*10);
int j = 0;
switch (i) {
case 0:
while (j++ < 4) {
downloadFrom148();
delete47FromLinuxOver3G("D:\\zabboxTestData\\downloafFrom148");
}
j = 0;
break;
case 1:
while (j++ < 4) {
downloadFrom149();
delete47FromLinuxOver3G("D:\\zabboxTestData\\downloafFrom149");
}
j = 0;
break;
case 2:
while (j++ < 4) {
downloadFrom241();
delete47FromLinuxOver3G("D:\\zabboxTestData\\downloafFrom241");
}
j = 0;
break;
case 3:
while (j++ < 4) {
downloadFrom242();
delete47FromLinuxOver3G("D:\\zabboxTestData\\downloafFrom242");
}
j = 0;
break;
case 4:
while (j++ < 4) {
downloadFrom243();
delete47FromLinuxOver3G("D:\\zabboxTestData\\downloafFrom243");
}
j = 0;
break;
case 5:
while (j++ < 4) {
downloadFrom244();
delete47FromLinuxOver3G("D:\\zabboxTestData\\downloafFrom244");
}
j = 0;
break;
case 6:
while (j++ < 4) {
downloadFrom245();
delete47FromLinuxOver3G("D:\\zabboxTestData\\downloafFrom245");
}
j = 0;
break;
case 7:
while (j++ < 4) {
downloadFrom246();
delete47FromLinuxOver3G("D:\\zabboxTestData\\downloafFrom246");
}
j = 0;
break;
case 8:
while (j++ < 4) {
downloadFrom247();
delete47FromLinuxOver3G("D:\\zabboxTestData\\downloafFrom247");
}
j = 0;
break;
case 9:
while (j++ < 4) {
downloadFrom248();
delete47FromLinuxOver3G("D:\\zabboxTestData\\downloafFrom248");
}
j = 0;
break;
default:
break;
}
}*/
/*SftpUtil sftp = new SftpUtil("root", "agent", "192.168.3.148", 22);
sftp.uploadTo148(sftp);
SftpUtil sftp2 = new SftpUtil("root", "agent", "192.168.3.149", 22);
sftp2.uploadTo149(sftp2)*/;
//sftp.uploadTo149();
//uploadTo241();
//uploadTo242();
//uploadTo243();
//uploadTo244();
//uploadTo245();
//uploadTo246();
//uploadTo247();
//uploadTo248();
//downloadFrom148();
//downloadFrom149();
//downloadFrom241();
//downloadFrom242();
//downloadFrom243();
//downloadFrom244();
//downloadFrom245();
//downloadFrom246();
//downloadFrom247();
//downloadFrom248();
}
}
uploadAndDownloadData.java
package uoload;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import java.util.Vector;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;
import com.jcraft.jsch.SftpException;
public class uploadAndDownloadData {
public void uploadTo148(SftpUtil sftp) throws FileNotFoundException, SftpException {
//SftpUtil sftp = new SftpUtil("root", "agent", "192.168.3.148", 22);
ChannelSftp channelSftp = sftp.login();
for (int i = 0; i <= 9; i++) {
FileInputStream inputStream = new FileInputStream(new File("D:\\zabboxTestData\\dataBase"+i+".pdf"));
sftp.upload("/opt/yfjz/getDateFrom47", "dataBase"+i+".pdf", inputStream);
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("成功上传文件到192.168.3.148主机!!!!");
//System.out.println("192.168.3.148开始删除啦--------");
deleteLinuxFrom47(sftp, "/opt/yfjz/getDateFrom47", false);
sftp.logout(channelSftp);
}
public void downloadFrom148(SftpUtil sftp) throws FileNotFoundException, SftpException {
//SftpUtil sftp = new SftpUtil("root", "agent", "192.168.3.148", 22);
ChannelSftp channelSftp = sftp.login();
for (int i = 0; i <= 9; i++) {
sftp.download("/opt/yfjz/datastatic", "dataBase"+i+".pdf", "D:\\zabboxTestData\\downloafFrom148\\dataBase"+i+".pdf");
}
System.out.println("成功从192.168.3.148主机下载文件到本地47!!!!");
//System.out.println("192.168.3.47开始删除啦--------");
//delete47FromLinux("D:\\zabboxTestData\\downloafFrom148");
sftp.logout(channelSftp);
}
public void uploadTo149(SftpUtil sftp) throws FileNotFoundException, SftpException {
//SftpUtil sftp = new SftpUtil("root", "agent", "192.168.3.149", 22);
ChannelSftp channelSftp = sftp.login();
for (int i = 0; i <= 9; i++) {
sftp.upload("/opt/yfjz/getDateFrom47", "dataBase"+i+".pdf", new FileInputStream(new File("D:\\zabboxTestData\\dataBase"+i+".pdf")));
}
System.out.println("成功上传文件到192.168.3.149主机!!!!");
//System.out.println("192.168.3.149开始删除啦--------");
deleteLinuxFrom47(sftp, "/opt/yfjz/getDateFrom47", false);
sftp.logout(channelSftp);
}
public void downloadFrom149(SftpUtil sftp) throws FileNotFoundException, SftpException {
//SftpUtil sftp = new SftpUtil("root", "agent", "192.168.3.149", 22);
ChannelSftp channelSftp = sftp.login();
for (int i = 0; i <= 9; i++) {
sftp.download("/opt/yfjz/datastatic", "dataBase"+i+".pdf", "D:\\zabboxTestData\\downloafFrom149\\dataBase"+i+".pdf");
}
System.out.println("成功从192.168.3.149主机下载文件到本地47!!!!");
//System.out.println("192.168.3.47开始删除啦--------");
//delete47FromLinux("D:\\zabboxTestData\\downloafFrom149");
sftp.logout(channelSftp);
}
public void uploadTo241(SftpUtil sftp) throws FileNotFoundException, SftpException {
//SftpUtil sftp = new SftpUtil("root", "agent", "192.168.3.241", 22);
ChannelSftp channelSftp = sftp.login();
for (int i = 0; i <= 9; i++) {
sftp.upload("/opt/yfjz/getDateFrom47", "dataBase"+i+".pdf", new FileInputStream(new File("D:\\zabboxTestData\\dataBase"+i+".pdf")));
}
System.out.println("成功上传文件到192.168.3.241主机!!!!");
//System.out.println("192.168.3.241开始删除啦--------");
deleteLinuxFrom47(sftp, "/opt/yfjz/getDateFrom47", false);
sftp.logout(channelSftp);
}
public void downloadFrom241(SftpUtil sftp) throws FileNotFoundException, SftpException {
//SftpUtil sftp = new SftpUtil("root", "agent", "192.168.3.241", 22);
ChannelSftp channelSftp = sftp.login();
for (int i = 0; i <= 9; i++) {
sftp.download("/opt/yfjz/datastatic", "dataBase"+i+".pdf", "D:\\zabboxTestData\\downloafFrom241\\dataBase"+i+".pdf");
}
System.out.println("成功从192.168.3.241主机下载文件到本地47!!!!");
//System.out.println("192.168.3.47开始删除啦--------");
//delete47FromLinux("D:\\zabboxTestData\\downloafFrom241");
sftp.logout(channelSftp);
}
public void uploadTo242(SftpUtil sftp) throws FileNotFoundException, SftpException {
//SftpUtil sftp = new SftpUtil("root", "agent", "192.168.3.242", 22);
ChannelSftp channelSftp = sftp.login();
for (int i = 0; i <= 9; i++) {
sftp.upload("/opt/yfjz/getDateFrom47", "dataBase"+i+".pdf", new FileInputStream(new File("D:\\zabboxTestData\\dataBase"+i+".pdf")));
}
System.out.println("成功上传文件到192.168.3.242主机!!!!");
//System.out.println("192.168.3.242开始删除啦--------");
deleteLinuxFrom47(sftp, "/opt/yfjz/getDateFrom47", false);
sftp.logout(channelSftp);
}
public void downloadFrom242(SftpUtil sftp) throws FileNotFoundException, SftpException {
//SftpUtil sftp = new SftpUtil("root", "agent", "192.168.3.242", 22);
ChannelSftp channelSftp = sftp.login();
for (int i = 0; i <= 9; i++) {
sftp.download("/opt/yfjz/datastatic", "dataBase"+i+".pdf", "D:\\zabboxTestData\\downloafFrom242\\dataBase"+i+".pdf");
}
System.out.println("成功从192.168.3.242主机下载文件到本地47!!!!");
//System.out.println("192.168.3.47开始删除啦--------");
//delete47FromLinux("D:\\zabboxTestData\\downloafFrom242");
sftp.logout(channelSftp);
}
public void uploadTo243(SftpUtil sftp) throws FileNotFoundException, SftpException {
//SftpUtil sftp = new SftpUtil("root", "agent", "192.168.3.243", 22);
ChannelSftp channelSftp = sftp.login();
for (int i = 0; i <= 9; i++) {
sftp.upload("/opt/yfjz/getDateFrom47", "dataBase"+i+".pdf", new FileInputStream(new File("D:\\zabboxTestData\\dataBase"+i+".pdf")));
}
System.out.println("成功上传文件到192.168.3.243主机!!!!");
//System.out.println("192.168.3.243开始删除啦--------");
deleteLinuxFrom47(sftp, "/opt/yfjz/getDateFrom47", false);
sftp.logout(channelSftp);
}
public void downloadFrom243(SftpUtil sftp) throws FileNotFoundException, SftpException {
//SftpUtil sftp = new SftpUtil("root", "agent", "192.168.3.243", 22);
ChannelSftp channelSftp = sftp.login();
for (int i = 0; i <= 9; i++) {
sftp.download("/opt/yfjz/datastatic", "dataBase"+i+".pdf", "D:\\zabboxTestData\\downloafFrom243\\dataBase"+i+".pdf");
}
System.out.println("成功从192.168.3.243主机下载文件到本地47!!!!");
//System.out.println("192.168.3.47开始删除啦--------");
//delete47FromLinux("D:\\zabboxTestData\\downloafFrom243");
sftp.logout(channelSftp);
}
public void uploadTo244(SftpUtil sftp) throws FileNotFoundException, SftpException {
//SftpUtil sftp = new SftpUtil("root", "agent", "192.168.3.244", 22);
ChannelSftp channelSftp = sftp.login();
for (int i = 0; i <= 9; i++) {
sftp.upload("/opt/yfjz/getDateFrom47", "dataBase"+i+".pdf", new FileInputStream(new File("D:\\zabboxTestData\\dataBase"+i+".pdf")));
}
System.out.println("成功上传文件到192.168.3.244主机!!!!");
//System.out.println("192.168.3.244开始删除啦--------");
deleteLinuxFrom47(sftp, "/opt/yfjz/getDateFrom47", false);
sftp.logout(channelSftp);
}
public void downloadFrom244(SftpUtil sftp) throws FileNotFoundException, SftpException {
//SftpUtil sftp = new SftpUtil("root", "agent", "192.168.3.244", 22);
ChannelSftp channelSftp = sftp.login();
for (int i = 0; i <= 9; i++) {
sftp.download("/opt/yfjz/datastatic", "dataBase"+i+".pdf", "D:\\zabboxTestData\\downloafFrom244\\dataBase"+i+".pdf");
}
System.out.println("成功从192.168.3.244主机下载文件到本地47!!!!");
//System.out.println("192.168.3.47开始删除啦--------");
//delete47FromLinux("D:\\zabboxTestData\\downloafFrom244");
sftp.logout(channelSftp);
}
public void uploadTo245(SftpUtil sftp) throws FileNotFoundException, SftpException {
//SftpUtil sftp = new SftpUtil("root", "agent", "192.168.3.245", 22);
ChannelSftp channelSftp = sftp.login();
for (int i = 0; i <= 9; i++) {
sftp.upload("/opt/yfjz/getDateFrom47", "dataBase"+i+".pdf", new FileInputStream(new File("D:\\zabboxTestData\\dataBase"+i+".pdf")));
}
System.out.println("成功上传文件到192.168.3.245主机!!!!");
//System.out.println("192.168.3.245开始删除啦--------");
deleteLinuxFrom47(sftp, "/opt/yfjz/getDateFrom47", false);
sftp.logout(channelSftp);
}
public void downloadFrom245(SftpUtil sftp) throws FileNotFoundException, SftpException {
//SftpUtil sftp = new SftpUtil("root", "agent", "192.168.3.245", 22);
ChannelSftp channelSftp = sftp.login();
for (int i = 0; i <= 9; i++) {
sftp.download("/opt/yfjz/datastatic", "dataBase"+i+".pdf", "D:\\zabboxTestData\\downloafFrom245\\dataBase"+i+".pdf");
}
System.out.println("成功从192.168.3.245主机下载文件到本地47!!!!");
//System.out.println("192.168.3.47开始删除啦--------");
//delete47FromLinux("D:\\zabboxTestData\\downloafFrom245");
sftp.logout(channelSftp);
}
public void uploadTo246(SftpUtil sftp) throws FileNotFoundException, SftpException {
//SftpUtil sftp = new SftpUtil("root", "agent", "192.168.3.246", 22);
ChannelSftp channelSftp = sftp.login();
for (int i = 0; i <= 9; i++) {
sftp.upload("/opt/yfjz/getDateFrom47", "dataBase"+i+".pdf", new FileInputStream(new File("D:\\zabboxTestData\\dataBase"+i+".pdf")));
}
System.out.println("成功上传文件到192.168.3.246主机!!!!");
//System.out.println("192.168.3.246开始删除啦--------");
deleteLinuxFrom47(sftp, "/opt/yfjz/getDateFrom47", false);
sftp.logout(channelSftp);
}
public void downloadFrom246(SftpUtil sftp) throws FileNotFoundException, SftpException {
//SftpUtil sftp = new SftpUtil("root", "agent", "192.168.3.246", 22);
ChannelSftp channelSftp = sftp.login();
for (int i = 0; i <= 9; i++) {
sftp.download("/opt/yfjz/datastatic", "dataBase"+i+".pdf", "D:\\zabboxTestData\\downloafFrom246\\dataBase"+i+".pdf");
}
System.out.println("成功从192.168.3.246主机下载文件到本地47!!!!");
//System.out.println("192.168.3.47开始删除啦--------");
//delete47FromLinux("D:\\zabboxTestData\\downloafFrom246");
sftp.logout(channelSftp);
}
public void uploadTo247(SftpUtil sftp) throws FileNotFoundException, SftpException {
//SftpUtil sftp = new SftpUtil("root", "agent", "192.168.3.247", 22);
ChannelSftp channelSftp = sftp.login();
for (int i = 0; i <= 9; i++) {
sftp.upload("/opt/yfjz/getDateFrom47", "dataBase"+i+".pdf", new FileInputStream(new File("D:\\zabboxTestData\\dataBase"+i+".pdf")));
}
System.out.println("成功上传文件到192.168.3.247主机!!!!");
//System.out.println("192.168.3.247开始删除啦--------");
deleteLinuxFrom47(sftp, "/opt/yfjz/getDateFrom47", false);
sftp.logout(channelSftp);
}
public void downloadFrom247(SftpUtil sftp) throws FileNotFoundException, SftpException {
//SftpUtil sftp = new SftpUtil("root", "agent", "192.168.3.247", 22);
ChannelSftp channelSftp = sftp.login();
for (int i = 0; i <= 9; i++) {
sftp.download("/opt/yfjz/datastatic", "dataBase"+i+".pdf", "D:\\zabboxTestData\\downloafFrom247\\dataBase"+i+".pdf");
}
System.out.println("成功从192.168.3.247主机下载文件到本地47!!!!");
//System.out.println("192.168.3.47开始删除啦--------");
//delete47FromLinux("D:\\zabboxTestData\\downloafFrom247");
sftp.logout(channelSftp);
}
public void uploadTo248(SftpUtil sftp) throws FileNotFoundException, SftpException {
//SftpUtil sftp = new SftpUtil("root", "agent", "192.168.3.248", 22);
ChannelSftp channelSftp = sftp.login();
for (int i = 0; i <= 9; i++) {
sftp.upload("/opt/yfjz/getDateFrom47", "dataBase"+i+".pdf", new FileInputStream(new File("D:\\zabboxTestData\\dataBase"+i+".pdf")));
//File[] files = sftp.listFiles("/opt/yfjz/getDateFrom47");
//sftp.delete("/opt/yfjz/getDateFrom47", deleteFile);
}
System.out.println("成功上传文件到192.168.3.248主机!!!!");
//System.out.println("192.168.3.248开始删除啦--------");
deleteLinuxFrom47(sftp, "/opt/yfjz/getDateFrom47", false);
sftp.logout(channelSftp);
}
public void downloadFrom248(SftpUtil sftp) throws FileNotFoundException, SftpException {
//SftpUtil sftp = new SftpUtil("root", "agent", "192.168.3.248", 22);
ChannelSftp channelSftp = sftp.login();
for (int i = 0; i <= 9; i++) {
sftp.download("/opt/yfjz/datastatic", "dataBase"+i+".pdf", "D:\\zabboxTestData\\downloafFrom248\\dataBase"+i+".pdf");
}
System.out.println("成功从192.168.3.248主机下载文件到本地47!!!!");
//System.out.println("192.168.3.47开始删除啦--------");
//delete47FromLinux("D:\\zabboxTestData\\downloafFrom248");
sftp.logout(channelSftp);
}
//把本地文件上传到linux上时,执行随机删除操作
public void deleteLinuxFrom47(SftpUtil sftp, String directory, boolean isClear ) throws SftpException {
if (!isClear) {
System.out.println(directory+"执行Linux随机删除操作--------");
}
ChannelSftp channelSftp = null;
int i = (int) (Math.random()*10);
if(isClear){
channelSftp = sftp.login();
System.out.println(directory+"执行Linux清空操作--------");
i = 10;
}
int j = 0;
//i=1;
while(j < i){
try {
sftp.delete(directory, "dataBase"+j+".pdf");
} catch (Exception e) {
// TODO: handle exception
}finally{
j++;
continue;
}
}
if(isClear){
sftp.logout(channelSftp);
}
}
//从linux下载数据到本地后,执行随机删除操作
public void delete47FromLinux(String directory) throws SftpException {
System.out.println(directory+"执行本地随机删除操作--------");
File dir = new File(directory);
File[] children = dir.listFiles();
int length = children.length;
if(length == 0){
return;
}
int i = (int) (Math.random()*length);
int j = 0;
//i=1;
while(j < i){
children[j].delete();
j++;
}
}
public static void main(String[] args) throws SftpException, IOException {
/*SftpUtil sftp = new SftpUtil("root", "agent", "192.168.3.148", 22);
sftp.login();
sftp.logout();
System.out.println("---------------");*/
int num = 60;
//int num = 1;
int type = 30;
int j = 0;
uploadAndDownloadData uploadAndDownloadData = new uploadAndDownloadData();
String directory = "/opt/yfjz/getDateFrom47";
while (j < num) {
int i= (int) (Math.random()*type);
//int i= (int) (Math.random()*1)-1;
switch (i) {
case -1:
SftpUtil sftp1_clear = new SftpUtil("root", "agent", "192.168.3.148", 22);
uploadAndDownloadData.deleteLinuxFrom47(sftp1_clear, directory, true);
SftpUtil sftp2_clear = new SftpUtil("root", "agent", "192.168.3.149", 22);
uploadAndDownloadData.deleteLinuxFrom47(sftp2_clear, directory, true);
SftpUtil sftp3_clear = new SftpUtil("root", "agent", "192.168.3.241", 22);
uploadAndDownloadData.deleteLinuxFrom47(sftp3_clear, directory, true);
SftpUtil sftp4_clear = new SftpUtil("root", "agent", "192.168.3.242", 22);
uploadAndDownloadData.deleteLinuxFrom47(sftp4_clear, directory, true);
SftpUtil sftp5_clear = new SftpUtil("root", "agent", "192.168.3.243", 22);
uploadAndDownloadData.deleteLinuxFrom47(sftp5_clear, directory, true);
SftpUtil sftp6_clear = new SftpUtil("root", "agent", "192.168.3.244", 22);
uploadAndDownloadData.deleteLinuxFrom47(sftp6_clear, directory, true);
SftpUtil sftp7_clear = new SftpUtil("root", "agent", "192.168.3.245", 22);
uploadAndDownloadData.deleteLinuxFrom47(sftp7_clear, directory, true);
SftpUtil sftp8_clear = new SftpUtil("root", "agent", "192.168.3.246", 22);
uploadAndDownloadData.deleteLinuxFrom47(sftp8_clear, directory, true);
SftpUtil sftp9_clear = new SftpUtil("root", "agent", "192.168.3.247", 22);
uploadAndDownloadData.deleteLinuxFrom47(sftp9_clear, directory, true);
SftpUtil sftp10_clear = new SftpUtil("root", "agent", "192.168.3.248", 22);
uploadAndDownloadData.deleteLinuxFrom47(sftp10_clear, directory, true);
j++;
System.out.println("------10台Linux的清空操作已完成-----");
break;
case 0:
SftpUtil sftp1 = new SftpUtil("root", "agent", "192.168.3.148", 22);
uploadAndDownloadData.uploadTo148(sftp1);
j++;
break;
case 1:
SftpUtil sftp2 = new SftpUtil("root", "agent", "192.168.3.149", 22);
uploadAndDownloadData.uploadTo149(sftp2);
j++;
break;
case 2:
SftpUtil sftp3 = new SftpUtil("root", "agent", "192.168.3.241", 22);
uploadAndDownloadData.uploadTo241(sftp3);
j++;
break;
case 3:
SftpUtil sftp4 = new SftpUtil("root", "agent", "192.168.3.242", 22);
uploadAndDownloadData.uploadTo242(sftp4);
j++;
break;
case 4:
SftpUtil sftp5 = new SftpUtil("root", "agent", "192.168.3.243", 22);
uploadAndDownloadData.uploadTo243(sftp5);
j++;
break;
case 5:
SftpUtil sftp6 = new SftpUtil("root", "agent", "192.168.3.244", 22);
uploadAndDownloadData.uploadTo244(sftp6);
j++;
break;
case 6:
SftpUtil sftp7 = new SftpUtil("root", "agent", "192.168.3.245", 22);
uploadAndDownloadData.uploadTo245(sftp7);
j++;
break;
case 7:
SftpUtil sftp8 = new SftpUtil("root", "agent", "192.168.3.246", 22);
uploadAndDownloadData.uploadTo246(sftp8);
j++;
break;
case 8:
SftpUtil sftp9 = new SftpUtil("root", "agent", "192.168.3.247", 22);
uploadAndDownloadData.uploadTo247(sftp9);
j++;
break;
case 9:
SftpUtil sftp10 = new SftpUtil("root", "agent", "192.168.3.248", 22);
uploadAndDownloadData.uploadTo248(sftp10);
j++;
break;
case 10:
SftpUtil sftp11 = new SftpUtil("root", "agent", "192.168.3.148", 22);
uploadAndDownloadData.downloadFrom148(sftp11);
j++;
break;
case 11:
SftpUtil sftp12 = new SftpUtil("root", "agent", "192.168.3.149", 22);
uploadAndDownloadData.downloadFrom149(sftp12);
j++;
break;
case 12:
SftpUtil sftp13 = new SftpUtil("root", "agent", "192.168.3.241", 22);
uploadAndDownloadData.downloadFrom241(sftp13);
j++;
break;
case 13:
SftpUtil sftp14 = new SftpUtil("root", "agent", "192.168.3.242", 22);
uploadAndDownloadData.downloadFrom242(sftp14);
j++;
break;
case 14:
SftpUtil sftp15 = new SftpUtil("root", "agent", "192.168.3.243", 22);
uploadAndDownloadData.downloadFrom243(sftp15);
j++;
break;
case 15:
SftpUtil sftp16 = new SftpUtil("root", "agent", "192.168.3.244", 22);
uploadAndDownloadData.downloadFrom244(sftp16);
j++;
break;
case 16:
SftpUtil sftp17 = new SftpUtil("root", "agent", "192.168.3.245", 22);
uploadAndDownloadData.downloadFrom245(sftp17);
j++;
break;
case 17:
SftpUtil sftp18 = new SftpUtil("root", "agent", "192.168.3.246", 22);
uploadAndDownloadData.downloadFrom246(sftp18);
j++;
break;
case 18:
SftpUtil sftp19 = new SftpUtil("root", "agent", "192.168.3.247", 22);
uploadAndDownloadData.downloadFrom247(sftp19);
j++;
break;
case 19:
SftpUtil sftp20 = new SftpUtil("root", "agent", "192.168.3.248", 22);
uploadAndDownloadData.downloadFrom248(sftp20);
j++;
break;
case 20:
uploadAndDownloadData.delete47FromLinux("D:\\zabboxTestData\\downloafFrom148");
j++;
break;
case 21:
uploadAndDownloadData.delete47FromLinux("D:\\zabboxTestData\\downloafFrom149");
j++;
break;
case 22:
uploadAndDownloadData.delete47FromLinux("D:\\zabboxTestData\\downloafFrom241");
j++;
break;
case 23:
uploadAndDownloadData.delete47FromLinux("D:\\zabboxTestData\\downloafFrom242");
j++;
break;
case 24:
uploadAndDownloadData.delete47FromLinux("D:\\zabboxTestData\\downloafFrom243");
j++;
break;
case 25:
uploadAndDownloadData.delete47FromLinux("D:\\zabboxTestData\\downloafFrom244");
j++;
break;
case 26:
uploadAndDownloadData.delete47FromLinux("D:\\zabboxTestData\\downloafFrom245");
j++;
break;
case 27:
uploadAndDownloadData.delete47FromLinux("D:\\zabboxTestData\\downloafFrom246");
j++;
break;
case 28:
uploadAndDownloadData.delete47FromLinux("D:\\zabboxTestData\\downloafFrom247");
j++;
break;
case 29:
uploadAndDownloadData.delete47FromLinux("D:\\zabboxTestData\\downloafFrom248");
j++;
break;
default:
break;
}
System.out.println("---数据上传及下载模拟过程(第"+j+"次)已完成---");
}
if(j >= num){
System.out.println("------数据上传及下载模拟过程("+j+"次)已完成------");
//return;
}
/*uploadAndDownloadData uploadAndDownloadData = new uploadAndDownloadData();
SftpUtil sftp1 = new SftpUtil("root", "agent", "192.168.3.148", 22);
uploadAndDownloadData.uploadTo148(sftp1);
SftpUtil sftp2 = new SftpUtil("root", "agent", "192.168.3.149", 22);
uploadAndDownloadData.uploadTo149(sftp2);
SftpUtil sftp3 = new SftpUtil("root", "agent", "192.168.3.241", 22);
uploadAndDownloadData.uploadTo241(sftp3);
SftpUtil sftp4 = new SftpUtil("root", "agent", "192.168.3.242", 22);
uploadAndDownloadData.uploadTo242(sftp4);
SftpUtil sftp5 = new SftpUtil("root", "agent", "192.168.3.243", 22);
uploadAndDownloadData.uploadTo243(sftp5);
SftpUtil sftp6 = new SftpUtil("root", "agent", "192.168.3.244", 22);
uploadAndDownloadData.uploadTo244(sftp6);
SftpUtil sftp7 = new SftpUtil("root", "agent", "192.168.3.245", 22);
uploadAndDownloadData.uploadTo245(sftp7);
SftpUtil sftp8 = new SftpUtil("root", "agent", "192.168.3.246", 22);
uploadAndDownloadData.uploadTo246(sftp8);
SftpUtil sftp9 = new SftpUtil("root", "agent", "192.168.3.247", 22);
uploadAndDownloadData.uploadTo247(sftp9);
SftpUtil sftp10 = new SftpUtil("root", "agent", "192.168.3.248", 22);
uploadAndDownloadData.uploadTo248(sftp10);*/
}
}
相关jar包下载地址:https://download.csdn.net/download/u012469528/10603559