springMVC下载FTP上的文件
今天没时间写。先上传 一个工具类
工具类
package com.utils; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import org.apache.commons.lang3.StringUtils; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPClientConfig; import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.net.ftp.FTPReply; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * ftp 工具类 * * @date 2016年1月15日 */ public class FTPUtils { private static final Logger logger = LoggerFactory.getLogger(FTPUtils.class); private static final FTPClientConfig config = new FTPClientConfig(FTPClientConfig.SYST_NT); public static final String DATE_TIME_FORMAT_TWO = "yyyy-MM-dd HH:mm"; private static final int BUFFER_SIZE = 1024; static { config.setDefaultDateFormatStr(DATE_TIME_FORMAT_TWO); config.setServerTimeZoneId("GMT+8"); config.setServerLanguageCode("zh"); } /** * 获取ftp 客户端 * * @param host * @param port * @param username * @param password * * @return FTPClient */ public static FTPClient client(String host, int port, String username, String password) { FTPClient client = new FTPClient(); client.configure(config); try { client.connect(host, port); client.enterLocalPassiveMode(); int replyCode = client.getReplyCode(); if (FTPReply.isPositiveCompletion(replyCode)) { client.login(username, password); replyCode = client.getReplyCode(); if (FTPReply.isPositiveCompletion(replyCode)) { return client; } else { logger.info(String.format("ftp client open faild. replyCode: %d, %s ", replyCode, client.getReplyString())); client.disconnect(); return null; } } else { logger.info(String.format("ftp client open faild. replyCode: %d, %s ", replyCode, client.getReplyString())); client.disconnect(); return null; } } catch (IOException e) { logger.error("ftp client open faild", e); return null; } } /** * ftp 下载远程文件 * * @param client ftp客户端对象 * @param remoteDirPath 远程目录 * @param remoteFile 远程文件 * @param localDir 本地存储目录(空 使用用户当前目录) * @param localFile 本地存储名称(空 使用文件原始名称) * * @return boolean */ public static boolean download(FTPClient client, String remoteDirPath, String remoteFile, String localDir, String localFile) { try { boolean dir = client.changeWorkingDirectory(remoteDirPath); boolean result = false; if (dir) { FTPFile[] files = client.listFiles(remoteFile); if (files.length == 1) { FTPFile file = files[0]; if (file.isFile()) { if (StringUtils.isEmpty(localFile)) localFile = file.getName(); if (StringUtils.isEmpty(localDir)) localDir = System.getProperty("user.home"); File localDirFile = new File(localDir); if (!localDirFile.exists()) localDirFile.mkdirs(); StringBuilder sb = new StringBuilder(localDir); sb.append(File.separator).append(localFile); try (FileOutputStream out = new FileOutputStream(sb.toString())) { client.setBufferSize(BUFFER_SIZE); client.setFileType(FTPClient.BINARY_FILE_TYPE); result = client.retrieveFile(file.getName(), out); } catch (IOException e) { e.printStackTrace(); logger.error("ftp download faild", e); } } } } return result; } catch (IOException e) { e.printStackTrace(); logger.error("ftp download faild", e); return false; } } /** * ftp 连接断开 * * @param client ftp 客户端 */ public static void close(FTPClient client) { try { if (!client.isConnected()) client.disconnect(); } catch (IOException e) { e.printStackTrace(); } } }
FTPClient client = FTPUtils.client(ftpHost, ftpPort, ftpUsername, ftpPassword); FTPUtils.download(client, "/", file + zip, localPath, null); FTPUtils.close(client);