import ch.qos.logback.core.net.SyslogOutputStream;
import com.jcraft.jsch.*;
import lombok.extern.slf4j.Slf4j;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.*;
import java.util.Random;
/**
* FTP服务器工具类
* <!-- https://mvnrepository.com/artifact/com.jcraft/jsch -->
* <dependency>
* <groupId>com.jcraft</groupId>
* <artifactId>jsch</artifactId>
* <version>0.1.55</version>
* </dependency>
*
* @author yaoguanquan
* @creote 2020-07-17-18:03
*/
@Slf4j
public class FTPUtils {
/** 主机 */
private final static String host = "xxxx";
/** 端口 */
private final static int port = 22;
/** 用户名 */
private final static String username = "xxx";
/** 密码 */
private final static String password = "xxxxx";
/** 目录 */
private final static String directory = "xxxxx";
/**
* 上传单个文件
* @param remoteFolder
* 上传到SFTP服务器的路径
* @param sourceFile
* 上传的文件地址
* @param remoteFileName
* 上传到SFTP服务器后的文件名
*
* @throws Exception
*/
public static Boolean uploadFile(String remoteFolder,File[] sourceFile, String remoteFileName){
// 返回参数
Boolean isTrue = true;
try{
long start = System.currentTimeMillis();
remoteFolder = directory + remoteFolder;
ChannelSftp sftp = connect();
if(sourceFile.length == 0){
log.error("上传文件数量不能为0!" );
return false;
}
if(sourceFile.length == 1){
// 上传单个文件
InputStream fileStream = new FileInputStream(sourceFile[0]); // 提升作用域
try{
//如果文件夹不存在,则创建文件夹
if(sftp.ls(remoteFolder) == null){
sftp.mkdir(remoteFolder);
}
//切换到指定文件夹
sftp.cd(remoteFolder);
}catch (SftpException e){
//创建不存在的文件夹,并切换到文件夹
sftp.mkdir(remoteFolder);
sftp.cd(remoteFolder);
}
sftp.put(fileStream, remoteFileName);
}else {
// 上传多个文件
// 记录所有已经上传的文件
List<String> pathList = new LinkedList<>();
// 判断是否有文件传输失败, 如果存在失败的, 将所有已经上传的文件删除
Boolean bo = false;
for(File f : sourceFile){
InputStream fileStream = new FileInputStream(f); // 提升作用域
try{
//如果文件夹不存在,则创建文件夹
if(sftp.ls(remoteFolder) == null){
sftp.mkdir(remoteFolder);
}
//切换到指定文件夹
sftp.cd(remoteFolder);
}catch (SftpException e){
//创建不存在的文件夹,并切换到文件夹
sftp.mkdir(remoteFolder);
sftp.cd(remoteFolder);
}
try{
// 传输文件
sftp.put(fileStream, f.getName());
pathList.add(remoteFolder + "/" + f.getName());
}catch (Exception e){
bo = true;
}
}
if(bo){
isTrue = false;
for(String strPath : pathList){
try{
new FTPUtils().delete(strPath);
}catch (Exception e){
isTrue = false;
log.error("上传多个文件失败, 删除已经上传成功的数据!" + e.getMessage());
}
}
}
}
// 关闭连接
disconnect(sftp);
log.info("文件上传成功!! 耗时:{"+(System.currentTimeMillis() - start)+"}ms");
}catch (Exception e){
log.error("文件上传失败!" );
isTrue = false;
return isTrue;
}
return isTrue;
}
/**
* 删除文件
* @param deleteFile
* 要删除的文件
*
* @throws Exception
*/
public void delete(String deleteFile) throws Exception {
ChannelSftp sftp = connect();
sftp.cd(directory);
sftp.rm(deleteFile);
disconnect(sftp);
}
/**
* 连接sftp服务器
*
* @throws Exception
*/
public static ChannelSftp connect() throws Exception {
JSch jsch = new JSch();
try{
//采用指定的端口连接服务器
Session session = jsch.getSession(username,host,port);
if(password != null){
//设置登陆主机的密码
session.setPassword(password);
}
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
session.setConfig(sshConfig);
session.connect();
//创建sftp通信通道
Channel channel = session.openChannel("sftp");
channel.connect();
log.info("连接成功");
return (ChannelSftp) channel;
}catch (JSchException e){
log.error("SFTP服务器连接异常!!");
throw new Exception("SFTP服务器连接异常!!",e);
}
}
/**
* Disconnect with server
*
* @throws Exception
*/
public static void disconnect(ChannelSftp sftp) throws Exception {
if (sftp != null) {
if (sftp.isConnected()) {
sftp.getSession().disconnect();
sftp.quit();
sftp.disconnect();
log.info("连接已关闭");
} else if (sftp.isClosed()) {
log.info("没有可以关闭的连接");
}
}
}
public static void main(String[] args)throws Exception{
// 数据文件名
// String dataFileName = "TEST_20200718.txt";
// String dyw_key1 = "D:\\Desktop\\优链时代\\获取当前登录人ID.txt";
// String dyw_key2 = "D:\\Desktop\\优链时代\\功能修改优化0526.xlsx";
// String dyw_key3= "D:\\Desktop\\优链时代\\优链时代--数据库表.md";
// String dyw_key4 = "D:\\Desktop\\优链时代\\修改功能.md";
// File file = new File(dyw_key1);
// File file2 = new File(dyw_key2);
// File file3 = new File(dyw_key3);
// File file4 = new File(dyw_key4);
// File[] files = {file,file2,file3,file4};
// Boolean resultDto1 = null;
// try {
//
// resultDto1 = new FTPUtils().uploadFile("测试上传文件夹", files, dataFileName);
// } catch (Exception e) {
// e.printStackTrace();
// }
// System.out.println("--上送ZQQK文件-----成功, 文件key = " + resultDto1);
}
}
上传文件到指定服务器 工具类
发布于 2020-07-18 540 次阅读
叨叨几句... NOTHING