FTP传输文件 Utils

import com.lenovo.vpe.common.utils.ftp.FtpUtil;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPReply;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.TimeZone;
public class FTPUtil {
    private FTPClient ftpClient;
    private boolean isLogin;
    private static Logger logger = LoggerFactory.getLogger(FtpUtil.class);

    /**
     * @Description: 连接 ftp服务器
     * @param url ftp地址
     * @param port ftp端口号
     * @param userName ftp 用户名
     * @param passWord ftp 密码
     * @return
     * @throws IOException
     */
    public boolean connectFTP(String url,int port,String userName,String passWord)throws IOException {
        this.ftpClient=new FTPClient();
        FTPClientConfig ftpClientConfig=new FTPClientConfig();
        ftpClientConfig.setServerTimeZoneId(TimeZone.getDefault().getID());
        this.ftpClient.setControlEncoding("UTF-8");
        this.ftpClient.configure(ftpClientConfig);
        this.ftpClient.connect(url.trim(),port);
        //ftp 连接回答返回码
        int reply=this.ftpClient.getReplyCode();
        if(!FTPReply.isPositiveCompletion(reply)){
            this.ftpClient.disconnect();
            logger.info("连接ftp服务器失败,code:\t{}",reply);
            return this.isLogin;
        }
        this.ftpClient.login(userName.trim(),passWord.trim());
        //设置传输协议
        this.ftpClient.enterLocalPassiveMode();
        this.ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
        logger.info("成功连接FTP服务器,userName:{},URL:{}",userName,url);
        this.isLogin=true;
        this.ftpClient.setBufferSize(1024*1024);
        this.ftpClient.setDataTimeout(60*1000);
        return this.isLogin;
    }

    /**
     * @Description 上传文件
     * @param localFile 上传文件对象
     * @param remoteUploadPath 存储ftp服务器路径
     * @return
     */
    public boolean uploadFile(File localFile,String remoteUploadPath){
        BufferedInputStream inputStream=null;
        boolean success =false;
        try{
            this.ftpClient.changeWorkingDirectory(remoteUploadPath.trim());
            inputStream=new BufferedInputStream(new FileInputStream(localFile));
            logger.info("开始上传文件名称:{}",localFile.getName());
            success=this.ftpClient.storeFile(localFile.getName(),inputStream);
            if(success){
                logger.info("上传成功,文件名称:{}",localFile.getName());
                return success;
            }
        }catch (IOException e){
            logger.error("FTP服务器传输异常:{}",e.getMessage());
        }finally {
            if(null!=inputStream){
                try{
                    inputStream.close();
                }catch (Exception e){
                    logger.error("FTP服务器传输异常:{}",e.getMessage());
                }
            }
        }
        return success;
    }

    /**
     * @Description  退出 / 关闭 FTP 服务器连接
     */
    public void loginOut(){
        if(null!=this.ftpClient && this.ftpClient.isConnected()){
            try{
                boolean logout=this.ftpClient.logout();
                if(logout){
                    logger.info("成功退出FTP服务器");
                }
            }catch (Exception e){
                logger.error("退出FTP服务器异常:{}",e.getMessage());
            }finally {
                try{
                    this.ftpClient.disconnect();// 关闭ftp服务器连接
                }catch (Exception e){
                    logger.error("FTP服务器关闭异常:{}",e.getMessage());
                }
            }

        }
    }

}

=================================================================

调用实例:

 

// 传输ftp 文件
try{
    boolean isLogin=ftpUtil.connectFTP(host,port,userName,password);
    if(isLogin){
        boolean isSuccess=ftpUtil.uploadFile(file,filePath);
    }
}catch (IOException e){

}finally {
    ftpUtil.loginOut();
    if (file.exists()) {
        file.delete();
    }
    logger.info("endTime:{}",DateUtil.getDateFormat(new Date(),"yyyyMMdd_HH:mm:ss"));
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值