Ftp Client 上传 下载 文件

1. 引入jar

	<dependency>
		<groupId>commons-net</groupId>
		<artifactId>commons-net</artifactId>
		<version>3.6</version>
	</dependency>

2. 上传 下载 类

package com.mv.common.ftp;

import com.mv.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

import java.io.*;
import java.net.SocketException;

@Slf4j
public class FtpConfig {

private static FTPClient ftpClientCommon = null;

public static FTPClient ftpClient(){
    if(null != ftpClientCommon){
        return ftpClientCommon;
    }
    FTPClient ftpClient = new FTPClient();
    //设置连接超时时间
    ftpClient.setConnectTimeout(30000);
    ftpClient.setControlEncoding("utf-8");
    //设置被动模式,文件传输端口设置 通知服务器开通给一个端口,防止挂死
    ftpClient.enterLocalPassiveMode();
    try {
        ftpClient.setDefaultPort(21);
        ftpClient.connect("192.168.9.100", FTP.DEFAULT_PORT);
        ftpClient.login("root","root");
        int replyCode = ftpClient.getReplyCode();
        if (!FTPReply.isPositiveCompletion(replyCode)){
            ftpClient.disconnect();
            log.error("connect error");
            return null;
        }else {
            log.info("connect success");
            ftpClientCommon = ftpClient;
            ftpClient.changeWorkingDirectory("/");
            //设置ftp字符集二进制  默认ASCII时 上传文件损坏
            try {
                ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
            return ftpClient;
        }
    } catch (SocketException e) {
        log.error(e.getMessage());
        return null;
    } catch (IOException e) {
        log.error(e.getMessage());
        return null;
    }
}

/**
 * 上传文件
 */
private static void uploadFile() {
    FTPClient ftpClient = ftpClient();
    if (ftpClient == null){
        return;
    }
    try {
        FTPFile[] ftpFiles = ftpClient.listFiles("/");
        Boolean flag = false;
        //遍历当前目录下的文件,判断要读取的文件是否在当前目录下
        for (FTPFile ftpFile:ftpFiles){
            if (ftpFile.getName().equals("LinuxBAMS.db")){
                flag = true;
            }
        }
        if(flag){
            FileOutputStream outputStream = new FileOutputStream(new File("d:/1/" + DateUtils.getCurrentDateyyyyMMddHHmmss() + ".db"));
            ftpClient.retrieveFile(new String("/LinuxBAMS.db".getBytes("utf-8"),"ISO-8859-1"),outputStream);
            outputStream.flush();
            outputStream.close();
            log.info("success");
        }else {
            log.info("error");
        }
        ftpClient.logout();
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        if (ftpClient.isConnected()){
            try {
                ftpClient.disconnect();
            } catch (IOException e) {
                log.error("disconnect fail ------->>>{}",e.getCause());
            }
        }
    }
}

/**
 *  上传文件
 * @param inputStream 输入流
 * @param originName 文件名
 * @param remoteDir  根目录 "/"
 */
private static boolean uploadFile(InputStream inputStream, String originName, String remoteDir){
    FTPClient ftpClient = FtpConfig.ftpClient();
    if (ftpClient == null){
        return false;
    }
    try {
        Boolean isSuccess = ftpClient.storeFile(originName,inputStream);//保存文件
        if(isSuccess){
            log.info("upload success");
        }else {
            log.info("upload error!");
        }
        ftpClient.logout();
        return true;
    } catch (IOException e) {
        log.error(e.getMessage());
        return false;
    }finally {
        if (ftpClient.isConnected()){
            try {
                ftpClient.disconnect();
            } catch (IOException e) {
                log.error(e.getMessage());
            }
        }
    }
}
public static void main(String[] args) throws Exception{
   File f = new File("d:/xx/xx.db");
    FileInputStream inputStream = new FileInputStream(f);
    uploadFile(inputStream,"xx.db","/");
}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值