package com.zway.lcs.an.util;
import cn.hutool.core.io.FileUtil;
import com.jcraft.jsch.SftpException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.Charsets;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import java.io.*;
import java.net.SocketException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Path;
import java.util.*;
import org.apache.commons.io.FileUtils;
@Slf4j
public class FtpUtils {
public static void main(String[] args) throws Exception {
FtpUtils ftpTools = new FtpUtils("192.168.182.222", 21, "ftpuser", "zway@123456");
ftpTools.connectClient();
File file = ftpTools.readFtpforFile("/home/test", "menu.sql");
List<String> strings = ftpTools.readConfigFileForFTP("/home/test", "menu.sql");
ftpTools.closeServer();
}
private String ftpHost;
private int ftpPort;
private String ftpUserName;
private String ftpPassword;
private FTPClient ftpClient;
public FtpUtils(String ftpHost, int ftpPort, String ftpUserName, String ftpPassword) {
this.ftpHost = ftpHost;
this.ftpPort = ftpPort;
this.ftpUserName = ftpUserName;
this.ftpPassword = ftpPassword;
}
public FTPClient connectClient1() {
log.info("***********************登录FTP***********************");
ftpClient = new FTPClient();
try {
ftpClient.setConnectTimeout(1000 * 30);
ftpClient.setControlEncoding("utf-8");
ftpClient.enterLocalPassiveMode();
ftpClient.setBufferSize(8096);
ftpClient.connect(ftpHost, ftpPort);
if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
log.info("未连接到FTP,用户名或密码错误。");
ftpClient.disconnect();
} else {
log.info("***********************登录FTP成功***********************");
}
} catch (SocketException e) {
log.error("FTP的IP地址可能错误,请正确配置。message={}", e);
} catch (IOException e) {
log.error("FTP的端口错误,请正确配置。message={}", e);
} catch (Exception e) {
log.error("FTP连接失败。message={}", e);
}
return ftpClient;
}
public boolean connectClient() {
log.info("***********************登录FTP***********************");
ftpClient = new FTPClient();
boolean connected = false;
try {
ftpClient.setConnectTimeout(1000 * 30);
ftpClient.setControlEncoding("utf-8");
ftpClient.enterLocalPassiveMode();
ftpClient.setBufferSize(8096);
ftpClient.connect(ftpHost, ftpPort);
connected = ftpClient.login(ftpUserName, ftpPassword);
if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
log.info("未连接到FTP,用户名或密码错误。");
ftpClient.disconnect();
} else {
log.info("***********************登录FTP成功***********************");
}
} catch (SocketException e) {
log.error("FTP的IP地址可能错误,请正确配置。message={}", e);
} catch (IOException e) {
log.error("FTP的端口错误,请正确配置。message={}", e);
} catch (Exception e) {
log.error("FTP连接失败。message={}", e);
}
return connected;
}
public boolean isFTPFileExist(String filePath) {
Boolean isexist = false;
try {
filePath = filePath.replace("ftp://" + ftpHost + ":" + ftpPort + "/", "");
String dir = filePath.substring(0, filePath.lastIndexOf("/"));
String file = filePath.substring(filePath.lastIndexOf("/") + 1);
ftpClient.changeWorkingDirectory(new String(dir.getBytes("GBK")));
InputStream is = ftpClient.retrieveFileStream(new String(file.getBytes("GBK")));
if (is == null || ftpClient.getReplyCode() == FTPReply.FILE_UNAVAILABLE) {
return isexist;
}
if (is != null) {
isexist = true;
is.close();
ftpClient.completePendingCommand();
}
return isexist;
} catch (Exception e) {
log.error("检验ftp指定路径的文件是否存在失败,message={}", e);
}
return isexist;
}
public List<String> downFile(String directory, String downloadFile, String saveFileDirectory) throws IOException {
File localFile = new File(saveFileDirectory);
if (!localFile.exists()) {
localFile.mkdirs();
}
OutputStream os = null;
os = new FileOutputStream(localFile);
ftpClient.retrieveFile(new String(downloadFile.getBytes(), Charsets.UTF_8), os);
os.close();
log.info("下载成功--------");
String saveFile = saveFileDirectory + "//" + downloadFile;
File file = new File(saveFile);
return FileUtil.readLines(file, Charsets.UTF_8);
}
private void readFiles(FTPClient ftpClient) throws IOException {
ftpClient.changeWorkingDirectory("/aaa/bbb");
final FTPFile[] ftpFiles = ftpClient.listFiles();
for (FTPFile ftpFile : ftpFiles) {
if (ftpFile.isFile()) {
System.out.println("********文件" + ftpFile.getName() + "的内容如下**********");
InputStream inputStream = ftpClient.retrieveFileStream(ftpFile.getName());
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
while (true) {
final String s = reader.readLine();
if (s == null) {
break;
}
System.out.println(s);
}
}
inputStream.close();
ftpClient.completePendingCommand();
}
}
}
public static void mappedFile(Path filename) {
try (FileChannel fileChannel = FileChannel.open(filename)) {
long size = fileChannel.size();
MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, size);
for (int i = 0; i < size; i++) {
mappedByteBuffer.get(i);
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
public File readFtpforFile( String ftpPath, String fileName) {
InputStream in = null;
log.info("开始读取绝对路径" + ftpPath + "文件!");
String remoteFile = ftpPath + "/" + fileName;
File newFile = new File(remoteFile);
try {
ftpClient.setControlEncoding("UTF-8");
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
ftpClient.changeWorkingDirectory(ftpPath);
in = ftpClient.retrieveFileStream(ftpPath+"/"+fileName);
FileUtils.copyToFile(in, newFile);
in.close();
ftpClient.completePendingCommand();
} catch (FileNotFoundException e) {
log.error("没有找到" + ftpPath + "文件");
e.printStackTrace();
} catch (SocketException e) {
log.error("连接FTP失败.");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
log.error("文件读取错误。");
e.printStackTrace();
}
return newFile;
}
public List<String> readConfigFileForFTP(String ftpPath, String fileName) {
StringBuffer resultBuffer = new StringBuffer();
List<String> strings = new ArrayList<>();
InputStream in = null;
log.info("开始读取绝对路径" + ftpPath + "文件!");
String remoteFile = ftpPath + "/" + fileName;
File newFile = new File(remoteFile);
try {
ftpClient.setControlEncoding("UTF-8");
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
ftpClient.changeWorkingDirectory(ftpPath);
in = ftpClient.retrieveFileStream(ftpPath+"/"+fileName);
} catch (FileNotFoundException e) {
log.error("没有找到" + ftpPath + "文件");
e.printStackTrace();
} catch (SocketException e) {
log.error("连接FTP失败.");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
log.error("文件读取错误。");
e.printStackTrace();
}
if (in != null) {
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String data = null;
try {
while ((data = br.readLine()) != null) {
resultBuffer.append(data + "\n");
strings.add(data);
System.out.println(data);
}
in.close();
ftpClient.completePendingCommand();
} catch (IOException e) {
log.error("文件读取错误。");
e.printStackTrace();
}
} else {
log.error("in为空,不能读取。");
}
return strings;
}
public static Map<String, Long> ftpsortMap(Map<String, Long> k_v) {
List<Map.Entry<String, Long>> list = new ArrayList<Map.Entry<String, Long>>(k_v.entrySet());
Collections.sort(list, new Comparator<Map.Entry<String, Long>>() {
@Override
public int compare(Map.Entry<String, Long> o1,
Map.Entry<String, Long> o2) {
return (int) (o2.getValue() - o1.getValue());
}
});
Map result = new LinkedHashMap();
for (Map.Entry<String, Long> entry : list) {
result.put(entry.getKey(), entry.getValue());
}
return result;
}
public Map<String, Long> ftpFilesAndTime(String dir) throws SftpException, IOException {
Calendar calendar = new GregorianCalendar();
Map<String, Long> map1 = new HashMap<>();
FTPFile[] files = ftpClient.listFiles(dir);
for (FTPFile f : files) {
map1.put(f.getName(), f.getTimestamp().getTimeInMillis());
}
return map1;
}
public List<String> downloadFtpFile(String ftpPath, String localPath, String fileName) throws Exception {
makeDirs(localPath);
ftpClient.setControlEncoding("GBK");
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
ftpClient.changeWorkingDirectory(StringUtils.trim(ftpPath));
File file = new File(localPath + "/" + fileName);
OutputStream os = new FileOutputStream(file);
ftpClient.retrieveFile(new String(file.getName().getBytes(), "ISO-8859-1"), os);
List<String> strings = FileUtil.readLines(file, Charsets.UTF_8);
ftpClient.deleteFile(localPath + "/" + fileName);
os.close();
return strings;
}
public void closeServer() {
try {
if (ftpClient != null) {
ftpClient.disconnect();
}
} catch (IOException e) {
log.error("ftpl连接关闭失败,message={}", e);
}
}
public void makeDirs(String localSavePath) {
File localFile = new File(localSavePath);
if (!localFile.exists()) {
localFile.mkdirs();
}
}
}
package com.zway.lcs.an.util;
import cn.hutool.core.io.FileUtil;
import com.jcraft.jsch.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.Charsets;
import org.apache.commons.io.FileUtils;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
@Slf4j
public class SftpUtils {
public static void main(String[] args) throws FileNotFoundException,SftpException {
SftpUtils fiel = new SftpUtils("root","zway@123456","192.168.182.222",22);
ChannelSftp sftp = fiel.sftp;
Boolean login = fiel.login();
fiel.download("/home/test","role.sql","C:\\Users\\DELL\\Desktop\\testFtp");
ArrayList<String> strings = fiel.listFiles("/home/test");
Map<Integer,String > maps = fiel.FilesAndTime("/home/test");
Map<Integer, String> integerStringMap = SftpUtils.sortMap(maps);
String lastModifiedTime = fiel.getLastModifiedTime("/home/test/nsa_info.2022-03-11-0.log");
File file = new File("C:\\Users\\DELL\\Desktop\\testFtp\\test.csv");
InputStream is = new FileInputStream(file);
fiel.upload("/home/test","","1.csv",is);
try {
fiel.download("/home/test","1.csv","C:\\Users\\DELL\\Desktop\\testFtp");
fiel.delete("C:\\Users\\DELL\\Desktop\\testFtp\\1.csv");
} catch (SftpException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
private String username;
private String password;
private String host;
private int port;
private String privateKey;
ChannelSftp sftp = null;
public SftpUtils(String username, String password, String host, int port){
this.username = username;
this.password = password;
this.host = host;
this.port = port;
}
public SftpUtils(String username, String host, int port, String privateKey){
this.username = username;
this.host = host;
this.port = port;
this.privateKey = privateKey;
}
public ChannelSftp loginChannelSftp(){
try {
JSch jSch = new JSch();
if(privateKey != null){
jSch.addIdentity(privateKey);
}
Session session = jSch.getSession(username,host,port);
if(password != null){
session.setPassword(password);
}
session.setTimeout(100000);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
log.info("登录成功--------");
} catch (JSchException e) {
log.info("登录失败--------");
}
return sftp;
}
public Boolean login(){
boolean connected=false;
try {
JSch jSch = new JSch();
if(privateKey != null){
jSch.addIdentity(privateKey);
}
Session session = jSch.getSession(username,host,port);
if(password != null){
session.setPassword(password);
}
session.setTimeout(100000);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
connected=sftp.isConnected();
log.info("登录成功--------");
} catch (JSchException e) {
log.info("登录失败--------");
}
return connected;
}
public void upload(String basePath, String direcotry, String sftpFileName, InputStream inputStream) throws SftpException{
try {
sftp.cd(basePath);
sftp.cd(direcotry);
} catch (SftpException e) {
String[] dirs = direcotry.split("/");
String temPath = basePath;
for (String dir: dirs
) {
if( null == dir || "".equals(dir)){ continue;};
temPath +="/" + dir;
try {
sftp.cd(temPath);
} catch (SftpException ex) {
sftp.mkdir(temPath);
sftp.cd(temPath);
}
}
}
sftp.put(inputStream,sftpFileName);
log.info("上传成功--------");
}
public void delete(String saveFile){
File file = new File(saveFile);
FileUtil.del(file);
log.info("删除成功--------");
}
public List<String> download(String directory, String downloadFile, String saveFileDirectory) throws SftpException, FileNotFoundException {
File localFile = new File(saveFileDirectory);
if (!localFile.exists()) {
localFile.mkdirs();
}
if(directory != null && !"".equals(directory)){
sftp.cd(directory);
}
String saveFile = saveFileDirectory + "//" + downloadFile;
File file = new File(saveFile);
sftp.get(downloadFile, new FileOutputStream(file));
log.info("下载成功--------");
return FileUtil.readLines(file, Charsets.UTF_8);
}
public File readSFTPforFile( ChannelSftp sftp,String remoteFile) {
File newFile = new File(remoteFile);
InputStream in = null;
log.info("开始读取绝对路径" + remoteFile + "文件!");
try {
in=sftp.get(remoteFile);
FileUtils.copyToFile(in, newFile);
} catch (SftpException | IOException e) {
e.printStackTrace();
log.error("文件读取错误。");
}
if (in==null){
log.error("in为空,不能读取。");
}
return newFile;
}
public List<String> readConfigFileForsFTP(String sftpPath, String fileName) {
StringBuffer resultBuffer = new StringBuffer();
List<String> strings=new ArrayList<>();
InputStream in = null;
log.info("开始读取绝对路径" + sftpPath + "文件!");
try {
String remoteFile = sftpPath +"/"+ fileName;
in=sftp.get(remoteFile);
} catch (SftpException e) {
e.printStackTrace();
log.error("文件读取错误。");
}
if (in != null) {
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String data = null;
try {
while ((data = br.readLine()) != null) {
resultBuffer.append(data + "\n");
strings.add(data);
}
in.close();
} catch (IOException e) {
log.error("文件读取错误。");
e.printStackTrace();
}
}else{
log.error("in为空,不能读取。");
}
return strings;
}
public String getLastModifiedTime(String srcSftpFilePath){
try {
SftpATTRS sftpATTRS = sftp.lstat(srcSftpFilePath);
int mTime = sftpATTRS.getMTime();
Date lastModified = new Date(mTime * 1000L);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = format.format(lastModified);
return time;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static Map<Integer, String> sortMap(Map<Integer, String> k_v) {
List<Map.Entry<Integer, String>> list = new ArrayList<Map.Entry<Integer, String>>(k_v.entrySet());
Collections.sort(list, new Comparator<Map.Entry<Integer, String>>() {
@Override
public int compare(Map.Entry<Integer, String> o1,
Map.Entry<Integer, String> o2) {
return o2.getKey()- o1.getKey();
}
});
Map result = new LinkedHashMap();
for (Map.Entry<Integer, String> entry : list) {
result.put(entry.getKey(), entry.getValue());
}
return result;
}
public Map<Integer, String> FilesAndTime(String dir) throws SftpException{
Map<Integer, String> map1=new HashMap<>();
sftp.cd(dir);
Vector<String> lss = sftp.ls("*");
for (int i = 0; i < lss.size(); i++) {
Object obj = lss.elementAt(i);
if (obj instanceof ChannelSftp.LsEntry) {
ChannelSftp.LsEntry entry = (ChannelSftp.LsEntry) obj;
if (true && !entry.getAttrs().isDir()) {
String filename=entry.getFilename();
SftpATTRS sftpATTRS = sftp.lstat(dir+"/"+filename);
int mTime = sftpATTRS.getMTime();
map1.put(mTime,filename);
}
if (true && entry.getAttrs().isDir()) {
if (!entry.getFilename().equals(".") && !entry.getFilename().equals("..")) {
String filename=entry.getFilename();
SftpATTRS sftpATTRS = sftp.lstat(dir+"/"+filename);
int mTime = sftpATTRS.getMTime();
map1.put(mTime,filename);
}
}
}
}
return map1;
}
public ArrayList<String> listFiles(String dir) throws SftpException{
ArrayList<String> files = new ArrayList<String>();
sftp.cd(dir);
Vector<String> lss = sftp.ls("*");
for (int i = 0; i < lss.size(); i++) {
Object obj = lss.elementAt(i);
if (obj instanceof ChannelSftp.LsEntry) {
ChannelSftp.LsEntry entry = (ChannelSftp.LsEntry) obj;
if (true && !entry.getAttrs().isDir()) {
files.add(entry.getFilename());
}
if (true && entry.getAttrs().isDir()) {
if (!entry.getFilename().equals(".") && !entry.getFilename().equals("..")) {
files.add(entry.getFilename());
}
}
}
}
return files;
}
public void destroy(ChannelSftp sftp){
try {
if (sftp != null) {
sftp.disconnect();
}
log.info("ftp conn destroyed");
} catch (Exception e) {
log.error("ftp conn destroy failed ", e);
}
}
}