FTP工具类


public class FtpUtil {
    private static Logger logger = Logger.getLogger(FtpUtil.class);
    private static String host;    //主机
    private static String username;    //用户名
    private static String password;    //密码
    private static int port;    //端口
    private FtpUtil() {}

 /**
     * 获取ftp实例
     *
     * @param host
     * @param username
     * @param password
     * @param port
     */
    public static FTPClient getFTPClient(String host, String username, String password, String port) {
        host = ftpHost;
        username = ftpUserName;
        password = ftpPassword;
        port = ftpPort;
        FTPClient ftpClient = null;
        try {
            ftpClient = new FTPClient();
            ftpClient.connect(host, port);// 连接FTP服务器
            ftpClient.login(username, password);// 登陆FTP服务器

            if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
                logger.info("未连接到FTP,用户名或密码错误。");
                ftpClient.disconnect();
            } else {
                logger.info("FTP连接成功。");
            }
        } catch (SocketException e) {
            e.printStackTrace();
            logger.error("FTP的IP地址可能错误,请正确配置。");
        } catch (IOException e) {
            e.printStackTrace();
            logger.error("FTP的端口错误,请正确配置。");
        }
        return ftpClient;
    }

    /**
     * ftp下载到本地
     *
     * @param remotePath    ftp路径
     * @param localPath     本地路径
     * @param fileName    文件名
     */
    public static void downloadFile(String remotePath, String localPath,
                                    String fileName) {
        FTPClient ftpClient = null;
        OutputStream os = null;
        try {
            ftpClient = getFTPClient(host, username, password, port);
            ftpClient.setControlEncoding("UTF-8"); // 中文支持
            ftpClient.enterLocalPassiveMode();
            ftpClient.changeWorkingDirectory(remotePath);

            os = new FileOutputStream(new File(localPath + "/" + fileName));
            ftpClient.retrieveFile(fileName, os);

        } catch (FileNotFoundException e) {
            logger.error("没有找到" + remotePath + "文件");
            e.printStackTrace();
        } catch (SocketException e) {
            logger.error("连接FTP失败.");
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
            logger.error("文件读取错误。");
            e.printStackTrace();
        } finally {
            try {
                os.close();
                ftpClient.logout();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 上传文件到ftp
     *
     * @param remotePath    ftp路径
     * @param localPath     本地路径
     * @param fileName    文件名
     */
    public static void uploadFile(String remotePath, String localPath, String filename) {
        InputStream input = null;
        FTPClient ftpClient = null;
        try {
            input = new FileInputStream(localPath + "\\" + filename);
            ftpClient = getFTPClient(host, username, password, port);
            ftpClient.changeWorkingDirectory(remotePath);
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            filename = new String(filename.getBytes("GBK"), "iso-8859-1");
            ftpClient.storeFile(filename, input);
            input.close();
            ftpClient.logout();
            System.out.println("upload succes!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * ftp之间文件传输
     *
     * @param midFtpPath    ftp接收端路径
     * @param destFtpPath    ftp发送端路径
     */
    public static void ftpToFtp(String midFtpPath, String destFtpPath) {
        logger.info("连接ftp");
        FTPClient midFtpClient = getFTPClient(host, username, password, port);
        FTPClient destFtpClient = getFTPClient(host, username, password, port);
        OutputStream os = null;
        try {
            midFtpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            destFtpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            midFtpClient.enterLocalPassiveMode();
            destFtpClient.enterLocalPassiveMode();
            logger.info("获取输出流");
            os = destFtpClient.storeFileStream(destFtpPath);
            logger.info("拷贝文件到目标ftp");
            midFtpClient.retrieveFile(midFtpPath, os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                logger.info("关闭流");
                os.close();
                midFtpClient.logout();
                logger.info("已注销midFtpClient");
                destFtpClient.logout();
                logger.info("已注销destFtpClient");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值