java ftp 文件下载
来源:互联网 发布:广东省网络医院待遇 编辑:程序博客网 时间:2024/06/05 11:13
import java.io.BufferedReader;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.SocketException;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.apache.commons.net.ftp.FTPClient;import org.apache.commons.net.ftp.FTPReply;import com.eastcom.common.utils.StringUtils;public class FtpFileUtils { private static Log logger = LogFactory.getLog(FtpFileUtils.class); // private static String ftpHost = "172.16.100.90"; // private static String ftpPassword = "Ivr123!@#"; // private static String ftpHost = "192.168.37.27"; // private static String ftpPassword = "ipnet"; // private static String ftpUserName = "ipnet"; // private static int ftpPort = 21; /** * 获取FTPClient对象 * * @param ftpHost * @param ftpPassword * @param ftpUserName * @param ftpPort * FTP端口 默认为21 * @return */ private static FTPClient getFTPClient(String filePath, Map<String, String> ftpParams) { FTPClient ftpClient = null; String ftpHost = ftpParams.get("ftpHost"); String ftpPassword = ftpParams.get("ftpPassword"); String ftpUserName = ftpParams.get("ftpUserName"); int ftpPort = Integer.valueOf(ftpParams.get("ftpPort")); try { ftpClient = new FTPClient(); ftpClient.connect(ftpHost, ftpPort); ftpClient.login(ftpUserName, ftpPassword); ftpClient.setControlEncoding("UTF-8"); ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); ftpClient.changeWorkingDirectory(filePath); ftpClient.enterLocalPassiveMode(); if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) { logger.info(" can not connect to ftp ----------"); ftpClient.disconnect(); } else { logger.info("FTP successful----------------"); } } catch (SocketException e) { e.printStackTrace(); logger.info("check ftp ip "); } catch (IOException e) { e.printStackTrace(); logger.info("check ftp ftpPort"); } return ftpClient; } /** * 去 服务器的FTP路径下上读取文件 * * @param ftpUserName * @param ftpPassword * @param ftpPath * @param FTPServer * @return */ public static List<String> readFtpFile(String fileName, String filePath, Map<String, String> ftpParams) { BufferedReader reader = null; InputStream ins = null; List<String> list = new ArrayList<String>(); FTPClient ftpClient = getFTPClient(filePath, ftpParams); logger.info("begin to read " + filePath + " " + fileName + " file"); try { ins = ftpClient.retrieveFileStream(fileName); reader = new BufferedReader(new InputStreamReader(ins, "GBK")); } catch (FileNotFoundException e) { logger.error("can not find " + filePath + "file"); e.printStackTrace(); } catch (SocketException e) { logger.error("connect ftp failed "); e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); logger.error("read file failed"); } finally { // 关闭FTP连接服务 if (ftpClient != null && ftpClient.isConnected()) try { ftpClient.disconnect(); } catch (IOException e) { logger.error("close the connection to FTP SERVER failed"); e.printStackTrace(); } } if (reader != null) { String data = null; try { while (StringUtils.isNotBlank(data = reader.readLine())) { list.add(data); } } catch (IOException e) { e.printStackTrace(); logger.error("read file failed"); } finally { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } else { logger.error("in is null ,can not read file"); } if (ins != null) { try { ins.close(); } catch (IOException e) { e.printStackTrace(); } } logger.info(" read " + filePath + " " + fileName + " file end "); return list; } /** * 返回服务器路径下所有存在的数据文件名 * * @param filePath * 服务器路径 * @param fileNameBegin * 文件名前缀 * @return */ public static List<String> listNames(String filePath, String fileNameBegin, Map<String, String> ftpParams) { List<String> list = new ArrayList<String>(); FTPClient ftpClient = getFTPClient(filePath, ftpParams); String[] names = null; try { names = ftpClient.listNames(); } catch (IOException e) { logger.info("No data file exit in the path : " + filePath); e.printStackTrace(); } finally { // 关闭FTP连接服务 if (ftpClient != null && ftpClient.isConnected()) try { ftpClient.logout(); ftpClient.disconnect(); } catch (IOException e) { logger.error("close the connection to FTP SERVER failed"); e.printStackTrace(); } } if (names != null) { for (String name : names) if (name.startsWith(fileNameBegin)) list.add(name); } else { logger.info("no file exit in the path : " + filePath); } return list; } public static void main(String[] args) { String filePath = "/home/ipnet/15mi/"; Map<String, String> ftpParams = new HashMap<String, String>(); ftpParams.put("ftpHost", "172.16.100.90"); ftpParams.put("ftpPort", "21"); ftpParams.put("ftpUserName", "ipnet"); ftpParams.put("ftpPassword", "Ivr123!@#"); List<String> fileNames = listNames(filePath, "page_detail_data_", ftpParams); for (String fileName : fileNames) readFtpFile(fileName, filePath, ftpParams); }}
阅读全文
0 0
- JAVA FTP 下载文件
- java ftp 文件下载
- java实现ftp下载文件
- java读取ftp文件下载
- java中FTP下载文件
- java关于ftp文件下载
- java中FTP下载文件
- java通过apache ftp实现ftp上传文件,下载文件
- java实现ftp下载文件【转】
- java 中使用FTP 下载 上传文件
- Java FTP(Apache)文件上传、下载
- FTP文件上传与下载(JAVA)
- java Ftp文件上传和下载
- JAVA Ftp实现文件上传于下载
- Java FTP(Apache)文件上传、下载
- java在ftp服务器下载文件
- java上传、下载、删除ftp文件
- Java实现FTP文件上传与下载
- 读取文件内容
- Android-应用启动过程
- 简述装饰模式实现思想
- Android控件三(ProgressBar 进度条)
- Android—常用控件(一)文本控件
- java ftp 文件下载
- cookie和session的思考
- 6月读书《有效的管理者》笔记
- Mac 配置angular-cli环境
- idea之工作流activiti报错is unrecognized or represents more than one time zone.解决方案
- Leetcode 49 Group Anagrams
- 自定义类加载器-从.class和.jar中读取
- 时序图读法
- 渗透测试标准--转载好文