ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

分享JAVA的FTP和SFTP相关操作工具类

2022-06-06 18:32:19  阅读:240  来源: 互联网

标签:FTP JAVA String fileList SFTP static param fileName dir


 1、导入相关jar

<!--FTPClient-->
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.3</version>
</dependency>

<!-- SFTP的依赖-->
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.51</version>
</dependency>



2、工具类实现代码
package com.shsnc.dbtdemo.common.utils;

import com.alibaba.druid.util.StringUtils;
import com.jcraft.jsch.*;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Vector;

/**
* FTP和SFTP相关工具类
*
* @author klp
* @Date 2022-5-31
*/
public class FtpUtil {

private static final String CHECK_FILE_SUFFIX = ".txt,.csv,.docx,.mp4,.png,.jpg,.xlsx,.log,.json"; //文件验证(datax仅支持text和csv)
private static String host = "172.168.48.59";
private static int port = 21;
private static String username = "1klp321";
private static String password = "121klp3f2@d";
private static String sftpHost = "147.165.185.118";
private static int sftpPort = 0;
private static String sftpUsername = "root";
private static String sftpPassword = "1klp@19dar3";

/**
* ftp创建连接
* @param host 主机IP
* @param port 端口
* @param username 用户名
* @param password 密码
* @return
* @throws IOException
*/
public static FTPClient connectByFtp(String host, int port, String username, String password) throws IOException {
FTPClient ftp = new FTPClient();
//防止中文乱码,不能在connect,login之后设置,查看源码得知
// FTPClient继承FTP,FTP继承SocketClient,
// 所以ftpClient调用方法connect()时,会调用_connectAction_()方法,如果还没有没置编码,
// getControlEncoding()会默认使用ios-8859-1,
// 所以必需在connect前完成编码设置
ftp.setControlEncoding("GBK");
// 设置ip和端口
ftp.connect(host, port);
// 设置用户名和密码
ftp.login(username, password);
// 设置文件类型(二进制传输模式)
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
// 设置被动模式
ftp.enterLocalPassiveMode();
if (ftp.getReplyCode() == 200) {
return ftp;
} else {
System.out.println("ftp连接状态:" + ftp.getReplyCode());
return null;
}
}


/**
* ftp获取全部目录路径
*
* @return
* @throws IOException
*/
public static List<String> getFtpDir() throws IOException {
List<String> fileList = new ArrayList<>();
FTPClient ftpClient = connectByFtp(host, port, username, password);
String dirHome = ftpClient.printWorkingDirectory();
fileList.add(dirHome);
getFtpDirList(ftpClient, fileList, dirHome);
System.out.println("ftp dir list:" + fileList);
ftpClient.logout();
ftpClient.disconnect();
return fileList;
}

/**
* ftp根据目录获取目录下全部文件
*
* @param dir 路径
* @return
* @throws IOException
*/
public static List<String> getFtpList(String dir) throws IOException {
List<String> fileList = null;
FTPClient ftpClient = connectByFtp(host, port, username, password);
fileList = getFtpFileList(ftpClient, dir);
System.out.println("ftp file list:" + fileList);
ftpClient.logout();
ftpClient.disconnect();
return fileList;
}


/**
* ftp递归获取目录列表
*
* @param fileList 文件结合
* @param dir 路径
*/
public static void getFtpDirList(FTPClient ftpClient, List<String> fileList, String dir) {
try {
FTPFile[] files = ftpClient.listFiles(dir);
if (files != null && files.length > 0) {
for (FTPFile file : files) {
if (file.isDirectory()) {
String fileDir = dir.concat("/").concat(file.getName());
fileList.add(fileDir);
getFtpDirList(ftpClient, fileList, fileDir);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* ftp获取目录下的文件
*
* @param dir 路径
* @throws IOException
*/
public static List<String> getFtpFileList(FTPClient ftpClient, String dir) throws IOException {
List<String> fileList = new ArrayList<>();
try {
FTPFile[] files = ftpClient.listFiles(dir);
if (files != null && files.length > 0) {
for (FTPFile file : files) {
String fileName = file.getName();
if (!".".equals(fileName) && !"..".equals(fileName) && !file.isDirectory()) {
String fileNameSuffix = fileName.contains(".") ? fileName.substring(fileName.lastIndexOf("."), fileName.length()) : fileName;
if (CHECK_FILE_SUFFIX.contains(fileNameSuffix)) {
fileList.add(fileName);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return fileList;
}


/**
* 获取ftp文件的内容,返回创建表的列集合
*
* @return
*/
public static List<String> getFtpContent(String dir, String splitStr) throws IOException {
List<String> fileList = new ArrayList<>();
FTPClient ftpClient = connectByFtp(host, port, username, password);
try {
FTPFile[] files = ftpClient.listFiles(dir);
if (files != null && files.length > 0) {
for (FTPFile file : files) {
file.isDirectory();
System.out.println(file.getName());
//获取核心表数据
InputStream inputStream = ftpClient.retrieveFileStream(file.getName());
if (inputStream != null) {
read(inputStream, splitStr);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return fileList;
}


/**
* 创建SFTP连接
*
* @param host 主机ip
* @param username 用户名
* @param password 密码
* @return
* @throws Exception
*/
public static ChannelSftp connectBySftp(String host, String username, String password) throws JSchException {
ChannelSftp sftp = null;
Session sshSession = null;
JSch jsch = new JSch();
sshSession = jsch.getSession(username, host);
sshSession.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
sshSession.connect();
Channel channel = sshSession.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
return sftp;
}

/**
* 获取sftp目录
*
* @return
* @throws SftpException
*/
public static List<String> getSftpDirList() throws SftpException, JSchException {

ChannelSftp channelSftp = connectBySftp(sftpHost, sftpUsername, sftpPassword);
String sftpHome = channelSftp.getHome();
List<String> fileList = new ArrayList<>();
fileList.add(sftpHome);
getFileDirWhile(fileList, channelSftp, sftpHome);
System.out.println("home path:" + fileList);
channelSftp.disconnect();
return fileList;
}


/**
* sftp递归获取路径
*
* @param fileList 文件集合
* @param channelSftp sftp对象
* @param dir 路径
* @throws SftpException
*/
public static void getFileDirWhile(List<String> fileList, ChannelSftp channelSftp, String dir) throws SftpException {
Vector<ChannelSftp.LsEntry> vector = channelSftp.ls(dir);
vector.forEach(file -> {
String fileName = file.getFilename();
if (!(".".equals(fileName)) && !("..".equals(fileName)) && file.getAttrs().isDir()) {
String fileDir = dir.concat("/").concat(fileName);
fileList.add(fileDir);
try {
getFileDirWhile(fileList, channelSftp, fileDir);
} catch (SftpException e) {
e.printStackTrace();
}
}
});
}


/**
* 获取sftp目录下所有文件
*
* @param dir 路径
* @return
* @throws SftpException
*/
public static List<String> getSftpFileList(String dir) throws SftpException, JSchException {
ChannelSftp channelSftp = connectBySftp(sftpHost, sftpUsername, sftpPassword);
List<String> fileList = getSftpFileName(channelSftp, dir);
channelSftp.disconnect();
return fileList;
}


/**
* sftp根据目录获取全部过滤文件名文件名
*
* @param channelSftp sftp对象
* @param dir 路径
* @return
* @throws SftpException
*/
public static List<String> getSftpFileName(ChannelSftp channelSftp, String dir) throws SftpException {
List<String> list = new ArrayList<>();
//ls命令获取文件名列表
Vector<ChannelSftp.LsEntry> vector = channelSftp.ls(dir);
vector.forEach(file -> {
//文件名称
String fileName = file.getFilename();
if (!".".equals(fileName) && !"..".equals(fileName) && !file.getAttrs().isDir()) {
String fileNameSuffix = fileName.contains(".") ? fileName.substring(fileName.lastIndexOf("."), fileName.length()) : fileName;
if (CHECK_FILE_SUFFIX.contains(fileNameSuffix)) {
list.add(fileName);
}
}
});
return list;
}

/**
* 读取Sftp内容
*
* @param channelSftp sftp对象
* @param dir 路径
* @param splitStr 分隔符
* @throws SftpException
*/
public void readContent(ChannelSftp channelSftp, String dir, String splitStr) throws SftpException {
List<String> list = new ArrayList<>();
//ls命令获取文件名列表
Vector<ChannelSftp.LsEntry> vector = channelSftp.ls(dir);
vector.forEach(file -> {
//文件名称
String fileName = file.getFilename();
if (!(".".equals(fileName)) && !("..".equals(fileName)) && !file.getAttrs().isDir()) {
list.add(fileName);
try {
InputStream inputStream = channelSftp.get(dir + fileName);
if (inputStream != null) {
read(inputStream, splitStr);
}
} catch (SftpException e) {
e.printStackTrace();
}

}
});

}

/**
* 读取文件第一行,确定多少列,生成建表列
* 读取csv、txt通用
*
* @param inputStream 文件流
* @param splitStr 分隔符
* @return
*/
public static String read(InputStream inputStream, String splitStr) {
StringBuffer sb = new StringBuffer();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));//GBK
String line = null;
if ((line = reader.readLine()) != null) {
int col = line.split(splitStr).length;
}
reader.close();
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}


public static void main(String[] args) throws SftpException, IOException, JSchException {

//ftp获取目录和文件
List<String> list = getSftpDirList();
System.out.println(list);
list.forEach(dir -> {
try {
System.out.println(getSftpFileList(dir));
} catch (SftpException | JSchException e) {
e.printStackTrace();
}
});

//ftp获取目录和文件
List<String> listFtp = getFtpDir();
listFtp.forEach(dir -> {
try {
getFtpList(dir);
} catch (IOException e) {
e.printStackTrace();
}
});
}
}


标签:FTP,JAVA,String,fileList,SFTP,static,param,fileName,dir
来源: https://www.cnblogs.com/haidaogege/p/16349279.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有