FTPClient 上传下载文件

来源:互联网 发布:美股软件 编辑:程序博客网 时间:2024/06/05 18:30

FTPClient  是基于commons-net.-1.4.jar的,利用FTPClient还需要几个jar,最好把common-*.jar都加上 呵呵,如果遇到NOClassFoundException,就百度吧

下载

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;

import org.apache.commons.net.ftp.FTP;
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 sun.net.TelnetInputStream;
import sun.net.ftp.FtpClient;

import com.telecom.js.cep.cepmanager.common.CommonConstants;
import com.telecom.js.cep.exception.BMOException;

public class FtpDownLoad {

    public static void main(String[] args) throws BMOException, IOException {
        FtpDownLoad manager = new FtpDownLoad();
        FTPClient ftpClient = manager.initConnectFtpServ(CommonConstants.prop);
        TelnetInputStream is = null;
        if (ftpClient.isConnected()) {
            // 设置文件类型(二进制)
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            int reply = ftpClient.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftpClient.disconnect();
            }
            ftpClient.changeWorkingDirectory("/usr/sfm/upload/crm/back/2011-07-20");// 转移到FTP服务器目录
            FTPFile[] fs = ftpClient.listFiles();
            String filename = fs[0].getName();//取其中第一个文件
            System.out.println(filename);
            OutputStream fos = new FileOutputStream("c:\\test\\"+filename);
            ftpClient.retrieveFile(filename, fos);
            if(fos!=null){
                fos.close();
            }
        }
         ftpClient.disconnect();
    }

    // 初始化FTP连接
    public FTPClient initConnectFtpServ(Properties prop) throws BMOException {
        FTPClient ftpClient = new FTPClient();
        try {
            ftpClient.connect(prop.getProperty("FTP_SERVER_IP"));
            ftpClient.login(prop.getProperty("FTP_USER_NAME"), prop
                    .getProperty("FTP_PWD"));
            ftpClient.setBufferSize(CommonConstants.BUFFER_SIZE);
            ftpClient.setControlEncoding("UTF-8");
            FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
            conf.setServerLanguageCode("zh");
        } catch (IOException e) {
            throw new BMOException("初始化FTP连接异常:" + e);
        }
        return ftpClient;
    }
    
     
         
}


上传

    // 初始化FTP连接
    public FTPClient initConnectFtpServ(Properties prop) throws BMOException {
        FTPClient ftpClient = new FTPClient();
        try {
            ftpClient.connect(prop.getProperty("FTP_SERVER_IP"));
            ftpClient.login(prop.getProperty("FTP_USER_NAME"), prop
                    .getProperty("FTP_PWD"));
            ftpClient.setBufferSize(CommonConstants.BUFFER_SIZE);
            ftpClient.setControlEncoding("UTF-8");
            // 设置文件类型(二进制)
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
        } catch (IOException e) {
            throw new BMOException("初始化FTP连接异常:" + e);
        }
        return ftpClient;
    }

    // 上传数据至集团接口,不往上抛异常
    public boolean uploadFileToFtpServ(Properties prop,
            String remoteUploadPath, String fileName) {
        FileInputStream fis = null;
        FTPClient ftpClient = null;
        boolean success = false;
        File srcFile = new File(fileName);
        try {
            ftpClient = initConnectFtpServ(prop);
            if (ftpClient.isConnected()) {
                logger.debug("localUploadPath + fileName:" + fileName);
                if (srcFile.exists()) {
                    fis = new FileInputStream(srcFile);
                    ftpClient.changeWorkingDirectory(remoteUploadPath);
                    ftpClient.storeFile(srcFile.getName(), fis);
                    success = true;
                }
            }
        } catch (IOException e) {
            logger.error("上传异常", e);
        } catch (BMOException e) {
            logger.error("FTP初始化异常", e);
        } finally {
            try {
                closeFtpConnection(ftpClient);
                if (fis != null) {
                    fis.close();
                }
            } catch (Exception e) {
                // 此时文件已经上传成功
                success = true;
                logger.error("IO关闭异常", e);
            }
        }
        return success;
    }


原创粉丝点击