Compare commits
2 Commits
1aa6c03e22
...
b76ffa27f6
Author | SHA1 | Date |
---|---|---|
|
b76ffa27f6 | 2 years ago |
|
9571ece801 | 2 years ago |
9 changed files with 219 additions and 26 deletions
@ -0,0 +1,118 @@ |
|||||
|
package com.userinformation.backend.util; |
||||
|
|
||||
|
import cn.hutool.core.text.csv.CsvReadConfig; |
||||
|
import cn.hutool.core.text.csv.CsvReader; |
||||
|
import cn.hutool.core.text.csv.CsvRow; |
||||
|
import com.fasterxml.jackson.databind.ObjectMapper; |
||||
|
import lombok.SneakyThrows; |
||||
|
import lombok.experimental.UtilityClass; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.apache.commons.net.ftp.FTPClient; |
||||
|
import org.apache.commons.net.ftp.FTPReply; |
||||
|
|
||||
|
import java.io.IOException; |
||||
|
import java.io.InputStream; |
||||
|
import java.io.InputStreamReader; |
||||
|
import java.net.SocketException; |
||||
|
import java.nio.charset.Charset; |
||||
|
import java.util.ArrayList; |
||||
|
import java.util.Arrays; |
||||
|
import java.util.List; |
||||
|
import java.util.Objects; |
||||
|
import java.util.stream.Collectors; |
||||
|
|
||||
|
/** |
||||
|
* @version 1.0 |
||||
|
* @author: xiaowuler |
||||
|
* @createTime: 2022-11-07 10:43 |
||||
|
* @description ftp 工具类 |
||||
|
*/ |
||||
|
@UtilityClass |
||||
|
@Slf4j |
||||
|
public class FtpUtil { |
||||
|
public FTPClient connection(String ftpHost, String ftpUsername, String ftpPassword, int ftpPort){ |
||||
|
FTPClient ftpClient = new FTPClient(); |
||||
|
try { |
||||
|
ftpClient.connect(ftpHost, ftpPort); |
||||
|
ftpClient.login(ftpUsername, ftpPassword); |
||||
|
if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) { |
||||
|
log.info("未连接到,用户名或密码错误"); |
||||
|
disconnection(ftpClient); |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
ftpClient.enterLocalPassiveMode(); |
||||
|
} catch (SocketException e) { |
||||
|
e.printStackTrace(); |
||||
|
log.info("FTP的IP地址可能错误,请正确配置"); |
||||
|
} catch (IOException e) { |
||||
|
e.printStackTrace(); |
||||
|
log.info("FTP的端口错误,请正确配置"); |
||||
|
} |
||||
|
return ftpClient; |
||||
|
} |
||||
|
|
||||
|
public boolean isExist(String filename, FTPClient ftpClient) throws IOException { |
||||
|
return ftpClient.listFiles(filename).length > 0; |
||||
|
} |
||||
|
|
||||
|
@SneakyThrows |
||||
|
public List<String[]> getFtpFileContentByCsvType(String fileName, String filePath, FTPClient ftpClient){ |
||||
|
List<String[]> fileContent = new ArrayList<>(); |
||||
|
ftpClient.changeWorkingDirectory(filePath); |
||||
|
// if (!isExist(fileName, ftpClient)){
|
||||
|
// return null;
|
||||
|
// }
|
||||
|
|
||||
|
InputStream in = ftpClient.retrieveFileStream(fileName); |
||||
|
if (Objects.isNull(in)){ |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
fileContent.addAll(readCsv(in)); |
||||
|
in.close(); |
||||
|
ftpClient.completePendingCommand(); |
||||
|
return fileContent; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* <b>将一个IO流解析,转化数组形式的集合<b> |
||||
|
* |
||||
|
* @param in 文件inputStream流 |
||||
|
*/ |
||||
|
@SneakyThrows |
||||
|
public List<String[]> readCsv(InputStream in) { |
||||
|
List<String[]> csvList = new ArrayList<>(); |
||||
|
if (null != in) { |
||||
|
CsvReader reader = new CsvReader(new InputStreamReader(in, "gbk"), CsvReadConfig.defaultConfig()); |
||||
|
reader.stream().forEach((CsvRow csvRow) -> { |
||||
|
csvList.add(csvRow.getRawList().stream().toArray(String[]::new)); |
||||
|
}); |
||||
|
|
||||
|
// CsvData
|
||||
|
// try {
|
||||
|
// // 遍历每一行,若有#注释部分,则不处理,若没有,则加入csvList
|
||||
|
// while (reader.readRecord()) {
|
||||
|
// if (!reader.getValues()[0].contains("#")){// 清除注释部分
|
||||
|
// csvList.add(reader.getValues());
|
||||
|
// log.info(Arrays.toString(csvList.get(csvList.size() - 1)));
|
||||
|
// }
|
||||
|
// }
|
||||
|
// } catch (IOException e) {
|
||||
|
// e.printStackTrace();
|
||||
|
// }
|
||||
|
|
||||
|
reader.close(); |
||||
|
} |
||||
|
return csvList; |
||||
|
} |
||||
|
|
||||
|
@SneakyThrows |
||||
|
public void disconnection(FTPClient ftpClient){ |
||||
|
if (Objects.isNull(ftpClient) || !ftpClient.isConnected()){ |
||||
|
return; |
||||
|
} |
||||
|
ftpClient.disconnect(); |
||||
|
} |
||||
|
|
||||
|
} |
Loading…
Reference in new issue