前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java FTP下载文件

Java FTP下载文件

作者头像
河岸飞流
发布2019-08-08 17:13:09
4.7K0
发布2019-08-08 17:13:09
举报
文章被收录于专栏:开发杂记开发杂记开发杂记
依赖的包
         <dependency>
            <groupId>commons-net</groupId>
            <artifactId>commons-net</artifactId>
            <version>3.1</version>
        </dependency>
方法示例
package com.xsh.util;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * FTP下载工具
 * @author shuangxie
 * @date 2019/5/24
 */
@Component
public class FTPUtil {
    /**
     * 日志对象
     **/
    private static final Logger logger = LoggerFactory.getLogger(FTPUtil.class);

    /**
     * 该目录不存在
     */
    public static final String DIR_NOT_EXIST = "该目录不存在";

    /**
     * "该目录下没有文件
     */
    public static final String DIR_CONTAINS_NO_FILE = "该目录下没有文件";

    /**
     * FTP地址
     **/
    @Value("${ftp.host}")
    private String ftpAddress;
    /**
     * FTP端口
     **/
    @Value("${ftp.port}")
    private int ftpPort = 521;
    /**
     * FTP用户名
     **/
    @Value("${ftp.username}")
    private String ftpUsername;
    /**
     * FTP密码
     **/
    @Value("${ftp.password}")
    private String ftpPassword;
    /**
     * FTP基础目录
     **/
    @Value("${ftp.basepath}")
    private String basePath;

    /**
     * 本地字符编码
     **/
    private static String localCharset = "GBK";

    /**
     * FTP协议里面,规定文件名编码为iso-8859-1
     **/
    private static String serverCharset = "ISO-8859-1";

    /**
     * UTF-8字符编码
     **/
    private static final String CHARSET_UTF8 = "UTF-8";

    /**
     * OPTS UTF8字符串常量
     **/
    private static final String OPTS_UTF8 = "OPTS UTF8";

    /**
     * 设置缓冲区大小4M
     **/
    private static final int BUFFER_SIZE = 1024 * 1024 * 4;

    /**
     * FTPClient对象
     **/
    private static FTPClient ftpClient = null;


    /**
     * 下载该目录下所有文件到本地
     *
     * @param ftpPath  FTP服务器上的相对路径,例如:test/123
     * @param savePath 保存文件到本地的路径,例如:D:/test
     * @return 成功返回true,否则返回false
     */
    public boolean downloadFiles(String ftpPath, String savePath) {
        // 登录
        login(ftpAddress, ftpPort, ftpUsername, ftpPassword);
        if (ftpClient != null) {
            try {
                String path = changeEncoding(basePath + ftpPath);
                // 判断是否存在该目录
                if (!ftpClient.changeWorkingDirectory(path)) {
                    logger.error(basePath + ftpPath + DIR_NOT_EXIST);
                    return Boolean.FALSE;
                }
                ftpClient.enterLocalPassiveMode();  // 设置被动模式,开通一个端口来传输数据
                String[] fs = ftpClient.listNames();
                // 判断该目录下是否有文件
                if (fs == null || fs.length == 0) {
                    logger.error(basePath + ftpPath + DIR_CONTAINS_NO_FILE);
                    return Boolean.FALSE;
                }
                for (String ff : fs) {
                    String ftpName = new String(ff.getBytes(serverCharset), localCharset);
                    File file = new File(savePath + '/' + ftpName);
                    try (OutputStream os = new FileOutputStream(file)) {
                        ftpClient.retrieveFile(ff, os);
                    } catch (Exception e) {
                        logger.error(e.getMessage(), e);
                    }
                }
            } catch (IOException e) {
                logger.error("下载文件失败", e);
            } finally {
                closeConnect();
            }
        }
        return Boolean.TRUE;
    }

    /**
     * 连接FTP服务器
     *
     * @param address  地址,如:127.0.0.1
     * @param port     端口,如:21
     * @param username 用户名,如:root
     * @param password 密码,如:root
     */
    private void login(String address, int port, String username, String password) {
        ftpClient = new FTPClient();
        try {
            ftpClient.connect(address, port);
            ftpClient.login(username, password);
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            //限制缓冲区大小
            ftpClient.setBufferSize(BUFFER_SIZE);
            int reply = ftpClient.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                closeConnect();
                logger.error("FTP服务器连接失败");
            }
        } catch (Exception e) {
            logger.error("FTP登录失败", e);
        }
    }


    /**
     * FTP服务器路径编码转换
     *
     * @param ftpPath FTP服务器路径
     * @return String
     */
    private static String changeEncoding(String ftpPath) {
        String directory = null;
        try {
            if (FTPReply.isPositiveCompletion(ftpClient.sendCommand(OPTS_UTF8, "ON"))) {
                localCharset = CHARSET_UTF8;
            }
            directory = new String(ftpPath.getBytes(localCharset), serverCharset);
        } catch (Exception e) {
            logger.error("路径编码转换失败", e);
        }
        return directory;
    }

    /**
     * 关闭FTP连接
     */
    private void closeConnect() {
        if (ftpClient != null && ftpClient.isConnected()) {
            try {
                ftpClient.logout();
                ftpClient.disconnect();
            } catch (IOException e) {
                logger.error("关闭FTP连接失败", e);
            }
        }
    }

    /**
     * 检查指定目录下是否含有指定文件
     *
     * @param ftpPath  FTP服务器文件相对路径,例如:test/123
     * @param fileName 要下载的文件名,例如:test.txt
     * @return 成功返回true,否则返回false
     */
    public boolean checkFileInFtp(String ftpPath, String fileName) {
        // 登录
        login(ftpAddress, ftpPort, ftpUsername, ftpPassword);
        if (ftpClient != null) {
            try {
                String path = changeEncoding(basePath + ftpPath);
                // 判断是否存在该目录
                if (!ftpClient.changeWorkingDirectory(path)) {
                    logger.error(basePath + ftpPath + DIR_NOT_EXIST);
                    return Boolean.FALSE;
                }
                ftpClient.enterLocalPassiveMode();  // 设置被动模式,开通一个端口来传输数据
                String[] fs = ftpClient.listNames();
                // 判断该目录下是否有文件
                if (fs == null || fs.length == 0) {
                    logger.error(basePath + ftpPath + DIR_CONTAINS_NO_FILE);
                    return Boolean.FALSE;
                }
                for (String ff : fs) {
                    String ftpName = new String(ff.getBytes(serverCharset), localCharset);
                    if (ftpName.equals(fileName)) {
                        return Boolean.TRUE;
                    }
                }
            } catch (IOException e) {
                logger.error("请求出错", e);
            } finally {
                closeConnect();
            }
        }
        return Boolean.TRUE;
    }

    /**
     * 下载该目录下所有文件到本地 根据实际需要修改执行逻辑
     *
     * @param ftpPath  FTP服务器上的相对路径,例如:test/123
     * @param savePath 保存文件到本地的路径,例如:D:/test
     * @return 成功返回true,否则返回false
     */
    public Map<String, Object> downLoadTableFile(String ftpPath, String savePath) {
        // 登录
        login(ftpAddress, ftpPort, ftpUsername, ftpPassword);
        Map<String, Object> resultMap = new HashMap<>();
        if (ftpClient != null) {
            try {
                String path = changeEncoding(basePath + "/" + ftpPath);
                // 判断是否存在该目录
                if (!ftpClient.changeWorkingDirectory(path)) {
                    logger.error(basePath + "/" + ftpPath + DIR_NOT_EXIST);
                    resultMap.put("result", false);
                    return resultMap;
                }
                ftpClient.enterLocalPassiveMode();  // 设置被动模式,开通一个端口来传输数据
                String[] fs = ftpClient.listNames();
                // 判断该目录下是否有文件
                if (fs == null || fs.length == 0) {
                    logger.error(basePath + "/" + ftpPath + DIR_CONTAINS_NO_FILE);
                    resultMap.put("result", false);
                    return resultMap;
                }
                List<String> tableFileNameList = new ArrayList<>();
                //根据表名创建文件夹
                String tableDirName = savePath + "/" + ftpPath;
                File tableDirs=new File(tableDirName);
                if(!tableDirs.exists()){
                    tableDirs.mkdirs();
                }
                for (String ff : fs) {
                    String ftpName = new String(ff.getBytes(serverCharset), localCharset);
                    File file = new File(tableDirName + "/" + ftpName);
                    //存储文件名导入时使用
                    tableFileNameList.add(tableDirName + "/" + ftpName);
                    try (OutputStream os = new FileOutputStream(file)) {
                        ftpClient.retrieveFile(ff, os);
                    } catch (Exception e) {
                        logger.error(e.getMessage(), e);
                    }
                }
                resultMap.put("fileNameList", tableFileNameList);
                resultMap.put("result", true);
                return resultMap;
            } catch (IOException e) {
                logger.error("下载文件失败", e);
            } finally {
                closeConnect();
            }
        }
        resultMap.put("result", false);
        return resultMap;
    }
}
单元测试
    @Autowired
    private FTPUtil ftpUtil;
    /**
     * 本地存储路径
     */
    @Value("${ftp.local.save.basepath}")
    private String localSavePath;

    @Test
    public void ftpDownloadTest() {
        Map<String, Object> result = ftpUtil.downLoadTableFile("dw_account_district", localSavePath);
        TestCase.assertEquals(true, result.get("result"));
    }
需要注意的点
  1. 需要设置缓冲区大小,防止下载过慢或者过快
  2. 需要确认ftp服务器的访问模式,被动模式还是主动模式
  3. 需要注意编码格式

源码下载:https://github.com/xieshuang/util

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-05-24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 依赖的包
  • 方法示例
  • 单元测试
  • 需要注意的点
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档