java ftp 读取文件读不出来,多文件,获取不到文件内容注意事项
一、连接ftp的方法和读取文件内容
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
/**
* @Author:
* @Contact:
* @Date: 2019/11/6 16:41
* @Version: 1.0
* @Description:
*/
@Slf4j
public class FTPListAllFiles {
public FTPClient ftp;
public ArrayList<String> arFiles;
/**
* 重载构造函数
*
* @param isPrintCommmand 是否打印与FTPServer的交互命令
*/
public FTPListAllFiles(boolean isPrintCommmand) {
ftp = new FTPClient();
arFiles = new ArrayList<String>();
if (isPrintCommmand) {
ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
}
}
/**
* 登陆FTP服务器
*
* @param host FTPServer IP地址
* @param port FTPServer 端口
* @param username FTPServer 登陆用户名
* @param password FTPServer 登陆密码
* @return 是否登录成功
* @throws IOException
*/
public boolean login(String host, int port, String username, String password) throws IOException {
this.ftp.connect(host, port);
if (FTPReply.isPositiveCompletion(this.ftp.getReplyCode())) {
if (this.ftp.login(username, password)) {
this.ftp.setControlEncoding("GBK");
return true;
}
}
if (this.ftp.isConnected()) {
this.ftp.disconnect();
}
return false;
}
/**
* 关闭数据链接
*
* @throws IOException
*/
public void disConnection() throws IOException {
if (this.ftp.isConnected()) {
this.ftp.disconnect();
}
}
/**
* 获取文件内的内容
* fileName 文件名称
* file 文件所在的文件夹
*/
public List<String> getCSVContent(String fileName,String file) {
List<String> list = new ArrayList<>();
try {
FtpInterface ftpInterface = new Ftp();
/
this.ftp.changeWorkingDirectory(file);//设置文件读取路径
// 从ftp上获取ggw目录下的文件
//FTPFile[] file1 = ftp.listFiles();
log.info("文件名{}。名称{}!", file, fileName);
ftp.enterLocalPassiveMode();//设置主动
InputStream in = ftp.retrieveFileStream(new String(fileName.getBytes("UTF-8"), "ISO-8859-1"));//读取文件内容转换
ftp.completePendingCommand();//多个文件设置处理
ArrayList<String[]> csvList = ftpInterface.praseCSV(in);
in.close();
// 将csv文件里的数据打印出来
// 遍历每一行
for (int row = 0; row < csvList.size(); row++) {
// 遍历每一行中的每一列
if (row == 0) {
continue;
}
for (int j = 0; j < csvList.get(row).length; j++) {
list.add(csvList.get(row)[j]);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
/**
* 递归遍历目录下面指定的文件名
*
* @param pathName 需要遍历的目录,必须以"/"开始和结束
* @param ext 文件的扩展名
* @throws IOException
*/
public List<String> fileList(String pathName, String ext) throws IOException {
List<String> list = new ArrayList<>();
if (pathName.startsWith("/") && pathName.endsWith("/")) {
//更换目录到当前目录
this.ftp.changeWorkingDirectory(pathName);
ftp.enterLocalPassiveMode();//设置ftp为主动
FTPFile[] files = this.ftp.listFiles();
for (FTPFile file : files) {
if (file.isFile()) {
if (file.getName().contains(ext)) {
arFiles.add(pathName + file.getName());
list.add(file.getName());
}
} else if (file.isDirectory()) {
if (!".".equals(file.getName()) && !"..".equals(file.getName())) {
fileList(pathName + file.getName() + "/", ext);
}
}
}
}
return list;
}
}
在这里插入代码片
把字节流转换为字符串
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import com.csvreader.CsvReader;
/**
* @Author:
* @Contact:
* @Date: 2019/11/6 18:49
* @Version: 1.0
* @Description:
*/
public class Ftp implements FtpInterface {
/**
* <b>将一个IO流解析,转化数组形式的集合<b>
*
* @param in 文件inputStream流
*/
@Override
public ArrayList<String[]> praseCSV(InputStream in) {
ArrayList<String[]> csvList = new ArrayList<String[]>();
if (null != in) {
CsvReader reader = new CsvReader(in, ',', Charset.forName("UTF-8"));
try {
//遍历每一行,若有#注释部分,则不处理,若没有,则加入csvList
while (reader.readRecord()) {
if (!reader.getValues()[0].contains("#"))// 清除注释部分
{
//获取的为每一行的信息,以数组的形式
csvList.add(reader.getValues());
}
}
} catch (IOException e) {
e.printStackTrace();
}
reader.close();
}
return csvList;
}
}
在这里插入代码片
import java.util.ArrayList;
/**
* @Author:
* @Contact:
* @Date: 2019/11/6 18:48
* @Version: 1.0
* @Description:
*/
public interface FtpInterface {
ArrayList<String[]> praseCSV(InputStream in);
}
在这里插入代码片
注意事项:
-
在读取文件时读不出文件时
在读取文件前加: FTPClient.enterLocalPassiveMode() ;//把ftp设置为主动模式 -
读取多文件时
下面文件读不出内容:FTPClient…completePendingCommand();设置多多文件的处理