FTP文件下载

FTP文件下载

package com.example.Test;

import com.example.util.ftp.FTPConfig;
import com.example.util.ftp.FTPFileDownload;

import java.io.IOException;

public class DownloadTest {
    public static void main(String[] args) throws IOException {
        FTPFileDownload ftpFileDownload = new FTPFileDownload();
        FTPConfig ftpConfig = new FTPConfig();
        ftpConfig.setIpAddr("39.106.227.63");
        ftpConfig.setUserName("jcsc");
        ftpConfig.setPwd("202433@@qq.com");
        ftpConfig.setRemotePath("/PM10/20191227/0900/");
        ftpConfig.setFileName("NC_H08_20191227_0100_R21_FLDK.06001_06001.zip");
        ftpConfig.setLocalPath("D:/");
        Boolean flag = ftpFileDownload.downloadFile(ftpConfig);
        if (flag){
            System.out.println("下载成功");
        }else{
            System.out.println("下载失败");
        }
    }
}

package com.example.util.ftp;

public class FTPConfig {
     private String ipAddr;//ip地址

     private Integer port;//端口号

     private String userName;//用户名

     private String pwd;//密码

     private String remotePath;//aaa路径

     private String fileName;//要下载的文件名

     private String localPath;//保存文件本地路径

    public String getLocalPath() {
        return localPath;
    }

    public void setLocalPath(String localPath) {
        this.localPath = localPath;
    }

    public String getIpAddr() {
        return ipAddr;
    }

    public void setIpAddr(String ipAddr) {
        this.ipAddr = ipAddr;
    }

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public String getRemotePath() {
        return remotePath;
    }

    public void setRemotePath(String remotePath) {
        this.remotePath = remotePath;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }
}

package com.example.util.ftp;

import com.example.util.ftp.FTPConfig;
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 java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class FTPFileDownload {

    public Boolean downloadFile(FTPConfig ftpConfig){

        Boolean success = false;
        FTPClient ftp = new FTPClient();
        try {
            ftp.connect(ftpConfig.getIpAddr());
            ftp.setControlEncoding("GBK");
            FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
            conf.setServerLanguageCode("zh");

            ftp.login(ftpConfig.getUserName(), ftpConfig.getPwd());
            int reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
                return success;
            }
            ftp.changeWorkingDirectory(ftpConfig.getRemotePath());
            FTPFile[] files = ftp.listFiles();
            for (int i = 0; i < files.length; i++) {
                FTPFile ff = files[i];
                System.out.println(ff.getName());
                System.out.println(ftpConfig.getFileName());
                System.out.println(ff.getName().equals(ftpConfig.getFileName()));
                if (ff.getName().equals(ftpConfig.getFileName())) {
                    File localfile = new File(ftpConfig.getLocalPath()+File.separator+ ff.getName());
                    OutputStream is = new FileOutputStream(localfile);
                    ftp.retrieveFile(new String(ff.getName().getBytes("GBK"), "ISO-8859-1"), is);
                    is.close();
                }
            }
            ftp.logout();
            success = true;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (ftp.isConnected()){
                try{
                    ftp.disconnect();
                }catch (IOException IOe){

                }

            }
        }
        return success;
    }
}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值