java远程操作ftp服务器上传下载

13 篇文章 0 订阅

 注意里面的文件编码,连接过程编码与服务器编码不一致的话会导致上传中文乱码情况。

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.net.ftp.*;
import org.apache.log4j.Logger;

public class FtpUtil {
    static Logger Log = Logger.getLogger(FtpUtil.class);
    private  FTPClient ftpClient;
    private static String ENCODING = "GBK";
    FTPClientConfig ftpConfig = new FTPClientConfig("UNIX");


    public boolean connectServer(String address,int port,String name,String passwd) {
        boolean flag = false;
        try {
            ftpClient = new FTPClient();
            ftpConfig.setServerLanguageCode("ISO-8859-1");
            ftpClient.connect(address, port);
            if(ftpClient.login(name,passwd)){
                if (FTPReply.isPositiveCompletion(ftpClient.sendCommand("OPTS UTF8", "ON"))) {
                    ENCODING = "UTF-8";
                }
                ftpClient.setControlEncoding(ENCODING);
                ftpClient.enterLocalPassiveMode();
                ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
                ftpClient.setFileTransferMode(FTP.STREAM_TRANSFER_MODE);
                ftpClient.setDataTimeout(120000);
            }
            int reply = ftpClient.getReplyCode();
            if (FTPReply.isPositiveCompletion(reply)) {
                flag = true;
                Log.info("FTP connect success!");
            } else {
                Log.warn("FTP refused to connect!");
                ftpClient.disconnect();
            }
        } catch (Exception e) {
            Log.error("Failed to login ftp " + address + ":" + port, e);
        }
        return flag;
    }

    public boolean existFile(String path) throws IOException {
        boolean flag = false;
        FTPFile[] ftpFileArr = ftpClient.listFiles(path);
        if (ftpFileArr.length > 0) {
            flag = true;
        }
        return flag;
    }


    public boolean makeDirectory(String dir) {
        boolean flag = true;
        try {
            String newdir = new String(dir.getBytes(ENCODING), "ISO-8859-1");
            flag = ftpClient.makeDirectory(newdir);
            if (flag) {
                Log.info("make directory " + newdir + " successfully !");
            } else {
                Log.info("make directory " + newdir + " failed !");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    public boolean uploadFile(String remoteFilePath, File uploadFile) throws IOException {
        boolean flag = false;
        InputStream input = null;
        try {
            input = new FileInputStream(uploadFile);
            String remote = new String(remoteFilePath.getBytes(ENCODING), "ISO-8859-1");
            if (ftpClient.appendFile(remote, input)) {
                flag = true;
            }
        } finally {
            input.close();
        }
        Log.info("push file (" + uploadFile.getCanonicalPath() + ") => " + (flag ? "SUCCESS" : "FAILED"));
        return flag;
    }

    public void closeConnect() {
        try {
            if (ftpClient != null) {
                ftpClient.logout();
                ftpClient.disconnect();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) throws Exception{
        FtpUtil ftpClient = new FtpUtil();
        //截取最后一个"/",获取文件目录
        String fileAllName = "/home/cld123/springbootFtpTest/linux节145.txt";
        File uploadFile = new File("C:\\Users\\tyx123\\Desktop\\txt\\linux节点配置.txt");
        String uploadFileDir = fileAllName.substring(0,fileAllName.lastIndexOf("/")+1);
        try {
            if(ftpClient.connectServer("192.168.0.223",21,"cld123","cld123")){
                if(!ftpClient.existFile(uploadFileDir)){
                    ftpClient.makeDirectory(uploadFileDir);
                }
                boolean flag = ftpClient.uploadFile(fileAllName, uploadFile);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        ftpClient.closeConnect();
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值