ftp下载文件到本地

本文介绍了如何利用Java的FTPClient库从FTP服务器下载文件到本地计算机,详细讲解了连接服务器、登录验证、文件传输及断点续传的步骤,为Java开发者提供了一套完整的FTP文件下载解决方案。
摘要由CSDN通过智能技术生成
  public static void main(String[] args) {
        Map map = new HashMap();
        map.put("ip", "88888");
        map.put("port", 21);
        map.put("username", "888888");
        map.put("password", "8888888");
        map.put("dir","test");
        try {
            FTPClient ftpClient = createFTPClient(map);
            List<FTPFile> ftpFiles = listFiles(ftpClient,null);
            for (FTPFile ftpFile:ftpFiles){
                System.out.println(ftpFile.getName());
                ftpClient.setFileType(FTP.BINARY_FILE_TYPE);//文件下载要使用二进制模式
                File localFile = new File("D:"+ "/test" + ftpFile.getName());
                OutputStream outputStream = new FileOutputStream(localFile);
                Boolean b=ftpClient.retrieveFile(ftpFile.getName(),outputStream);
                System.out.println(b);
                outputStream.close();
            }
            ftpClient.logout();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
       public static FTPClient createFTPClient(Map cfg) throws Exception {
        if (!cfg.containsKey("ip"))
            throw new Exception("缺少ip配置");
        else if (!cfg.containsKey("username"))
            throw new Exception("缺少username配置");
        else if (!cfg.containsKey("password"))
            throw new Exception("缺少password配置");

        FtpCfg ftpCfg = new FtpCfg();
        ftpCfg.setIp(cfg.get("ip").toString());
        ftpCfg.setPort(cfg.get("port") == null ? null : Integer.valueOf(cfg.get("port").toString()));
        ftpCfg.setUsername(cfg.get("username").toString());
        ftpCfg.setPassword(cfg.get("password").toString());
        ftpCfg.setWorkingDir(cfg.get("dir") == null ? null : cfg.get("dir").toString());
        return createFTPClient(ftpCfg);
    }
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值