zoukankan      html  css  js  c++  java
  • java FTP各种操作

    package com.ailk.qw.util;

    import it.sauronsoftware.ftp4j.*;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;

    import java.io.File;
    import java.io.IOException;



    public class FtpUtil {
    final static Logger log = LoggerFactory.getLogger(FtpUtil.class);

    public static FTPClient getFtp(String ip, int port, String username, String password, String charSet) throws FTPException, IOException, FTPIllegalReplyException {
    FTPClient client = new FTPClient();
    client.connect(ip, port);
    client.login(username, password);
    client.setCharset(charSet);
    client.setType(FTPClient.TYPE_BINARY);//二进制
    return client;
    }


    /**
    * 远程移动文件
    *
    * @param ip
    * @param port
    * @param username
    * @param password
    * @param fromPath
    * @param fromFileName
    * @param toPath
    * @param toFileName
    * @return
    */
    public static boolean remoteMove(String ip, int port, String username, String password, String fromPath, String fromFileName, String toPath, String toFileName) {
    boolean result = false;
    FTPClient client = null;
    log.info("开始将" + ip + "服务器目录为" + fromPath + "文件名为" + fromFileName + "移动到" + toPath + "目录文件名为" + toFileName);
    try {
    client = getFtp(ip, port, username, password, "UTF-8");
    //client.changeDirectory(fromPath);
    client.rename(fromPath + "/" + fromFileName, toPath + "/" + toFileName);
    client.disconnect(true);
    if (client.isConnected()) {
    client.disconnect(false);
    }
    result = true;
    } catch (FTPException e) {
    log.error("将" + ip + "服务器目录为" + fromPath + "文件名为" + fromFileName + "移动到" + toPath + "目录文件名为" + toFileName + "出现异常", e);
    } catch (IOException e) {
    log.error("将" + ip + "服务器目录为" + fromPath + "文件名为" + fromFileName + "移动到" + toPath + "目录文件名为" + toFileName + "出现异常", e);
    } catch (FTPIllegalReplyException e) {
    log.error("将" + ip + "服务器目录为" + fromPath + "文件名为" + fromFileName + "移动到" + toPath + "目录文件名为" + toFileName + "出现异常", e);
    } finally {
    if (client.isConnected()) {
    try {
    client.disconnect(false);
    } catch (IOException e) {
    log.error("关闭ftp连接时出现异常", e);
    } catch (FTPIllegalReplyException e) {
    log.error("关闭ftp连接时出现异常", e);
    } catch (FTPException e) {
    log.error("关闭ftp连接时出现异常", e);
    }
    }
    log.info("将" + ip + "服务器目录为" + fromPath + "文件名为" + fromFileName + "移动到" + toPath + "目录文件名为" + toFileName + "结果为:" + result);
    return result;
    }

    }

    /**
    * 远程移动文件
    *
    * @param ip
    * @param port
    * @param username
    * @param password
    * @param fromPath
    * @param fromFileName
    * @param toPath
    * @param toFileName
    * @param charSet
    * @return
    */
    public static boolean remoteMove(String ip, int port, String username, String password, String fromPath, String fromFileName, String toPath, String toFileName, String charSet) {
    boolean result = false;
    FTPClient client = null;
    log.info("开始将" + ip + "服务器目录为" + fromPath + "文件名为" + fromFileName + "移动到" + toPath + "目录文件名为" + toFileName);
    try {
    client = getFtp(ip, port, username, password, charSet);
    //client.changeDirectory(fromPath);
    client.rename(fromPath + "/" + fromFileName, toPath + "/" + toFileName);
    client.disconnect(true);
    if (client.isConnected()) {
    client.disconnect(false);
    }
    result = true;
    } catch (FTPException e) {
    log.error("将" + ip + "服务器目录为" + fromPath + "文件名为" + fromFileName + "移动到" + toPath + "目录文件名为" + toFileName + "出现异常", e);
    } catch (IOException e) {
    log.error("将" + ip + "服务器目录为" + fromPath + "文件名为" + fromFileName + "移动到" + toPath + "目录文件名为" + toFileName + "出现异常", e);
    } catch (FTPIllegalReplyException e) {
    log.error("将" + ip + "服务器目录为" + fromPath + "文件名为" + fromFileName + "移动到" + toPath + "目录文件名为" + toFileName + "出现异常", e);
    } finally {
    if (client.isConnected()) {
    try {
    client.disconnect(false);
    } catch (IOException e) {
    log.error("关闭ftp连接时出现异常", e);
    } catch (FTPIllegalReplyException e) {
    log.error("关闭ftp连接时出现异常", e);
    } catch (FTPException e) {
    log.error("关闭ftp连接时出现异常", e);
    }
    }
    log.info("将" + ip + "服务器目录为" + fromPath + "文件名为" + fromFileName + "移动到" + toPath + "目录文件名为" + toFileName + "结果为:" + result);
    return result;
    }

    }

    /**
    * 下载文件
    *
    * @param ip
    * @param port
    * @param username
    * @param password
    * @param remotePath
    * @param remoteFileName
    * @param localPath
    * @param localFileName
    * @return
    */
    public static boolean download(String ip, int port, String username, String password, String remotePath, String remoteFileName, String localPath, String localFileName) {
    boolean result = false;
    FTPClient client = null;
    log.info("开始从" + ip + "服务器目录为" + remotePath + "下载文件" + remoteFileName + "到本机" + localPath + "目录文件名为" + localFileName);
    try {
    client = getFtp(ip, port, username, password, "UTF-8");
    client.changeDirectory(remotePath);
    client.download(remoteFileName, new File(localPath + File.separatorChar + localFileName));
    client.disconnect(true);
    if (client.isConnected()) {
    client.disconnect(false);
    }
    result = true;
    } catch (FTPException e) {
    log.info("从" + ip + "服务器目录为" + remotePath + "下载文件" + remoteFileName + "到本机" + localPath + "目录文件名为" + localFileName + "出现异常!", e);
    } catch (IOException e) {
    log.info("从" + ip + "服务器目录为" + remotePath + "下载文件" + remoteFileName + "到本机" + localPath + "目录文件名为" + localFileName + "出现异常!", e);
    } catch (FTPIllegalReplyException e) {
    log.info("从" + ip + "服务器目录为" + remotePath + "下载文件" + remoteFileName + "到本机" + localPath + "目录文件名为" + localFileName + "出现异常!", e);
    } catch (FTPAbortedException e) {
    log.info("从" + ip + "服务器目录为" + remotePath + "下载文件" + remoteFileName + "到本机" + localPath + "目录文件名为" + localFileName + "出现异常!", e);
    } catch (FTPDataTransferException e) {
    log.info("从" + ip + "服务器传目录为" + remotePath + "下载文件" + remoteFileName + "到本机" + localPath + "目录文件名为" + localFileName + "出现异常!", e);
    } finally {
    if (client.isConnected()) {
    try {
    client.disconnect(false);
    } catch (IOException e) {
    log.error("关闭ftp连接时出现异常", e);
    } catch (FTPIllegalReplyException e) {
    log.error("关闭ftp连接时出现异常", e);
    } catch (FTPException e) {
    log.error("关闭ftp连接时出现异常", e);
    }
    }
    log.info("从" + ip + "服务器目录为" + remotePath + "下载文件" + remoteFileName + "到本机" + localPath + "目录文件名为" + localFileName + "结果为:" + result);
    return result;
    }
    }

    /**
    * 上传文件
    *
    * @param ip
    * @param port
    * @param username
    * @param password
    * @param remotePath
    * @param file
    * @return
    */
    public static boolean upload(String ip, int port, String username, String password, String remotePath, File file, String oldFileName, String fileName) {
    FTPClient client = null;
    boolean result = false;
    try {
    log.info("开始向" + ip + "服务器传目录为" + remotePath + "上传文件" + fileName);
    client = getFtp(ip, port, username, password, "UTF-8");
    client.changeDirectory(remotePath);
    client.changeDirectoryUp();
    client.changeDirectory("temp");
    String fromPath = client.currentDirectory();
    String fromFileName = oldFileName;
    client.upload(file);
    client.disconnect(true);
    if (client.isConnected()) {
    client.disconnect(false);
    }
    result = remoteMove(ip, port, username, password, fromPath, fromFileName, remotePath, fileName);
    } catch (FTPException e) {
    log.error("向" + ip + "服务器传目录为" + remotePath + "上传文件" + fileName + "时出现异常!", e);
    } catch (IOException e) {
    log.error("向" + ip + "服务器传目录为" + remotePath + "上传文件" + fileName + "时出现异常!", e);
    } catch (FTPIllegalReplyException e) {
    log.error("向" + ip + "服务器传目录为" + remotePath + "上传文件" + fileName + "时出现异常!", e);
    } catch (FTPAbortedException e) {
    log.error("向" + ip + "服务器传目录为" + remotePath + "上传文件" + fileName + "时出现异常!", e);
    } catch (FTPDataTransferException e) {
    log.error("向" + ip + "服务器传目录为" + remotePath + "上传文件" + fileName + "时出现异常!", e);
    } finally {
    if (client.isConnected()) {
    try {
    client.disconnect(false);
    } catch (IOException e) {
    log.error("关闭ftp连接时出现异常", e);
    } catch (FTPIllegalReplyException e) {
    log.error("关闭ftp连接时出现异常", e);
    } catch (FTPException e) {
    log.error("关闭ftp连接时出现异常", e);
    }
    }
    log.info("向" + ip + "服务器传目录为" + remotePath + "上传文件" + fileName + "结果为:" + result);
    return result;
    }
    }

    /**
    * 上传文件
    *
    * @param ip
    * @param port
    * @param username
    * @param password
    * @param remotePath
    * @param file
    * @param charSet
    * @return
    */
    public static boolean upload(String ip, int port, String username, String password, String remotePath, File file, String oldFileName, String fileName, String charSet) {
    FTPClient client = null;
    boolean result = false;
    try {
    log.info("开始向" + ip + "服务器传目录为" + remotePath + "上传文件" + fileName);
    client = getFtp(ip, port, username, password, charSet);
    client.changeDirectory(remotePath);
    client.changeDirectoryUp();
    client.changeDirectory("temp");
    String fromPath = client.currentDirectory();
    String fromFileName = oldFileName;
    client.upload(file);
    client.disconnect(true);
    if (client.isConnected()) {
    client.disconnect(false);
    }
    result = remoteMove(ip, port, username, password, fromPath, fromFileName, remotePath, fileName, charSet);
    } catch (FTPException e) {
    log.error("向" + ip + "服务器传目录为" + remotePath + "上传文件" + fileName + "时出现异常!", e);
    } catch (IOException e) {
    log.error("向" + ip + "服务器传目录为" + remotePath + "上传文件" + fileName + "时出现异常!", e);
    } catch (FTPIllegalReplyException e) {
    log.error("向" + ip + "服务器传目录为" + remotePath + "上传文件" + fileName + "时出现异常!", e);
    } catch (FTPAbortedException e) {
    log.error("向" + ip + "服务器传目录为" + remotePath + "上传文件" + fileName + "时出现异常!", e);
    } catch (FTPDataTransferException e) {
    log.error("向" + ip + "服务器传目录为" + remotePath + "上传文件" + fileName + "时出现异常!", e);
    } finally {
    if (client.isConnected()) {
    try {
    client.disconnect(false);
    } catch (IOException e) {
    log.error("关闭ftp连接时出现异常", e);
    } catch (FTPIllegalReplyException e) {
    log.error("关闭ftp连接时出现异常", e);
    } catch (FTPException e) {
    log.error("关闭ftp连接时出现异常", e);
    }
    }
    log.info("向" + ip + "服务器传目录为" + remotePath + "上传文件" + fileName + "结果为:" + result);
    return result;
    }
    }


    }
  • 相关阅读:
    【Gerrit】重磅! 2.x 版本升级到 3.x 版本
    【Linux】参数传递之xargs
    Sqlserver账号对应数据库
    限流:计数器、漏桶、令牌桶 三大算法的原理与实战(史上最全)
    C# 运行在ubuntu, linux系统,在linux系统使用HslCommunication组件,.net core发布到ubuntu系统
    使用nmap命令监控远程服务器指定端口状态
    MySQL使用脚本进行整库数据备份【表(结构+数据)、视图、函数、事件】
    MySQL自定义函数与存储过程的创建、使用、删除
    vue响应式的原理
    浏览器渲染机制
  • 原文地址:https://www.cnblogs.com/xiaoyu1994/p/9110803.html
Copyright © 2011-2022 走看看