java实现通过FTP下载文件




import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import com.iflytek.dicom.config.GlobalConfig;

/**
 *
 * ClassName: FtpUtil
 * @Description: ftp工具类
 * @author wyl_0728
 * @date 2016年8月22日
 */
public class FtpUtil {

 private static Logger logger = LogManager.getLogger(FtpUtil.class.getName());
 
    private FTPClient ftp;
    private boolean is_connected;

    public FtpUtil() {
        ftp = new FTPClient();
        ftp.setControlEncoding("UTF-8");
        ftp.enterLocalPassiveMode();
        is_connected = false;
    }
    /*

*构造器 设置连接超时时间

*/
    public FtpUtil(int defaultTimeoutSecond, int connectTimeoutSecond, int dataTimeoutSecond){
        ftp = new FTPClient();
        is_connected = false;
        ftp.setControlEncoding("UTF-8");
        ftp.enterLocalPassiveMode();
        ftp.setDefaultTimeout(defaultTimeoutSecond * 1000);
        ftp.setConnectTimeout(connectTimeoutSecond * 1000);
        ftp.setDataTimeout(dataTimeoutSecond * 1000);
    }

    /**
     * Connects to FTP server.
     * host  port  username  password 通过配置文件读取
     */
    public FTPClient connect() throws IOException {
        // Connect to server.
        try {
         ftp.connect(GlobalConfig.getString("FTP_SERVER"),Integer.parseInt(GlobalConfig.getString("FTP_PORT")));
        } catch (UnknownHostException ex) {
            throw new IOException("Can't find FTP server '" + GlobalConfig.getString("FTP_SERVER") + "'");
        }

        // Check rsponse after connection attempt.
        int reply = ftp.getReplyCode();
        logger.info("------login -reply ="+reply); 
        if (!FTPReply.isPositiveCompletion(reply)) {
         logger.debug("---------------------##############################");
            disconnect();
            throw new IOException("Can't connect to server '" + GlobalConfig.getString("FTP_SERVER") + "'");
        }
        // Login.
        boolean flag =ftp.login(GlobalConfig.getString("FTP_USER"), GlobalConfig.getString("FTP_PWD"));
        logger.debug("------login -flag ="+flag);
        if (!flag) {
            is_connected = false;
            disconnect();
            throw new IOException("Can't login to server '" + GlobalConfig.getString("FTP_SERVER") + "'");
        } else {
            is_connected = true;
        }

        // Set data transfer mode.
        ftp.setFileType(FTP.BINARY_FILE_TYPE);
        ftp.enterLocalPassiveMode();
        logger.debug("------login -is_connected ="+is_connected);    
       return ftp;
    }

    /**

    * 上传实现
     * Uploads the file to the FTP server.
     *
     * @param ftpFileName
     *            server file name (with absolute path)
     * @param localFile
     *            local file to upload
     * @throws IOException
     *        

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值