java ftp 文件下载

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);
    }
}

猜你喜欢

转载自blog.csdn.net/lkw411526/article/details/72905490
今日推荐