从文件服务器获取文件名称及内容(base64)

一、需要jar包 commons-net-3.8.0.jar

二、部分实现代码

// 连接远程ftp文件服务器
Map<String,String> filesMap = new HashMap<>();
FtpFile ftpFile = new FtpFile();
ftpFile.connectServer(ip, port, userName, userPwd, path);
List<String> files = ftpFile.getFileList(path);
for (int i = 0; i < files.size(); i++) {
String fileName = files.get(i);
String result = ftpFile.readFile(path + File.separatorChar +fileName);
filesMap.put(fileName,result);
}
ftpFile.closeServer();
if (filesMap != null && filesMap.size() > 0){
String contractFileName = "";
for(String key : filesMap.keySet()){
String value = filesMap.get(key);
contractFileName = contractFileName + key + ";";
log.info("文件名:"+key+" 内容:"+value);
}
}

三、ftp工具类

/**
* Created by LQ on 2021/8/31.
*/

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;

/**
* 从FTP读取文件
* @author
*
*/
public class FtpFile {
private FTPClient ftpClient;
private static Logger log = LoggerFactory.getLogger(FtpFile.class);
/**
* @param ip
* @param port
* @param userName
* @param userPwd
* @param path
* @throws SocketException
* @throws IOException function:连接到服务器
*/
public void connectServer(String ip, int port, String userName, String userPwd, String path) {
ftpClient = new FTPClient();
ftpClient.setControlEncoding("GBK");
try {
ftpClient.connect(ip, port); //连接
ftpClient.login(userName, userPwd); // 登录
// if (path != null && path.length() > 0) {
// // 跳转到指定目录
// ftpClient.changeWorkingDirectory(path);
// }
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* @throws IOException function:关闭连接
*/
public void closeServer() {
if (ftpClient.isConnected()) {
try {
ftpClient.logout();
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}

/**
* @param path
* @return function:读取指定目录下的文件名
* @throws IOException
*/
public List<String> getFileList(String path) {
List<String> fileLists = new ArrayList<String>();
// 获得指定目录下所有文件名
FTPFile[] ftpFiles = null;
try {
ftpFiles = ftpClient.listFiles(path);
} catch (IOException e) {
e.printStackTrace();
}
for (int i = 0; ftpFiles != null && i < ftpFiles.length; i++) {
FTPFile file = ftpFiles[i];
if (file.isFile()) {
fileLists.add(file.getName());
}
}
return fileLists;
}


/**
* 返回一个文件流
*
* @param fileName
* @return
*/
public String readFile(String fileName) {
String result = "";
InputStream ins = null;
try {
ins = ftpClient.retrieveFileStream(fileName);
byte[] data = null;
data = new byte[ins.available()];
ins.read(data);
result = Base64Util.convertByteArrayToBase64String(data);
if (ins != null) {
ins.close();
ftpClient.completePendingCommand();
}
// 主动调用一次getReply()把接下来的226消费掉. 这样做是可以解决这个返回null问题
// ftpClient.getReply();
} catch (Exception e) {
log.error("文件名称:"+fileName);
e.printStackTrace();
}
return result;
}

/**
* @param fileName function:删除文件
*/
public void deleteFile(String fileName) {
try {
ftpClient.deleteFile(fileName);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @param fileName
* @param sourceFile
* @return
* @throws IOException function:下载文件
*/
public boolean unloadFile(String fileName, String sourceFile) {
boolean flag = false;
try {
FileOutputStream fos = new FileOutputStream(fileName);
flag = ftpClient.retrieveFile(sourceFile, fos);
fos.flush();
fos.close();
} catch (Exception e) {
flag = false;
e.printStackTrace();
}
return flag;
}
}

posted @ 2021-09-02 11:01  十二lq  阅读(1040)  评论(0)    收藏  举报