import java.io.*;
import java.net.SocketException;
import java.net.URLEncoder;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
public class FtpFileUtil {
/**
* 获取FTPClient对象
* @param ftpAddress FTP主机服务器地址
* @param ftpHost 端口号
* @param ftpUsername 用户名
* @param ftpPassword 密码
* @return FTPClient对象
*/
public static FTPClient getFTPClient(String ftpAddress, int ftpHost, String ftpUsername, String ftpPassword) {
FTPClient ftpClient = new FTPClient();
try {
ftpClient.setDataTimeout(60000); //设置传输超时时间为60秒
ftpClient.setConnectTimeout(60000);
ftpClient.connect(ftpAddress, ftpHost);// 连接FTP服务器
ftpClient.login(ftpUsername, ftpPassword);// 登陆FTP服务器
if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
logger.info("未连接到FTP,用户名或密码错误。");
ftpClient.disconnect();
} else {
logger.info("FTP连接成功。");
}
} catch (SocketException e) {
e.printStackTrace();
logger.info("FTP的IP地址可能错误,请正确配置。");
} catch (IOException e) {
e.printStackTrace();
logger.info("FTP的端口错误,请正确配置。");
}
return ftpClient;
}
/**
*实现文件打包压缩下载
* @param FTP_ADDRESS
* @param FTP_PORT
* @param FTP_USERNAME
* @param FTP_PASSWORD
* @param response 服务器响应对象HttpServletResponse
* @param zipname 压缩包的文件名
* @param ftpPath ftp文件路径
* @param ftpFileList 要下载的文件名集合
* @param namelist 压缩后的文件名集合
*/
public static void zipDownloadFile(String FTP_ADDRESS,int FTP_PORT,String FTP_USERNAME,String FTP_PASSWORD,HttpServletResponse response, String zipname, String ftpPath,List<String>ftpFileList, List<String> namelist) {
FTPClient ftp = getFTPClient(FTP_ADDRESS, FTP_PORT, FTP_USERNAME, FTP_PASSWORD);
byte[] buf = new byte[1024];
try {
ftp.setControlEncoding("UTF-8");
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
ftp.enterLocalPassiveMode();
ftp.changeWorkingDirectory(ftpPath);
File zipFile = new File("123");
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
for (int i=0;i< ftpFileList.size();i++) {
ZipEntry entry = new ZipEntry((i+1)+"_"+namelist.get(i));
zipOut.putNextEntry(entry);
InputStream bis = ftp.retrieveFileStream(ftpFileList.get(i));
if (bis != null) {
int readLen = -1;
while ((readLen = bis.read(buf, 0, 1024)) != -1) {
zipOut.write(buf, 0, readLen);
}
zipOut.closeEntry();
bis.close();
ftp.completePendingCommand();
//调用ftp.retrieveFileStream这个接口后,一定要手动close掉返回的InputStream,然后再调用completePendingCommand方法
// ,若不是按照这个顺序,则会导致后面对FTPClient的操作都失败
}
}
zipOut.close();
ftp.logout();
//下载
int len;
FileInputStream zipInput =new FileInputStream(zipFile);
OutputStream out = response.getOutputStream();
response.setContentType("application/octet-stream");
response.addHeader("Content-Disposition", "attachment; filename="
+ URLEncoder.encode(zipname, "UTF-8") + ".zip");
while ((len=zipInput.read(buf))!= -1){
out.write(buf,0,len);
}
zipInput.close();
out.flush();
out.close();
//删除压缩包
zipFile.delete();
} catch (Exception e) {
logger.error("文件打包下载有误: " + e.getLocalizedMessage());
e.printStackTrace();
}
}
}
标签:FTP,读取,ftp,param,FTPClient,import,ftpClient,压缩包
From: https://blog.51cto.com/u_15564034/6429984