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

版权声明:转载请注明出处 https://blog.csdn.net/weixin_36524613/article/details/81102948

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

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();
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_36524613/article/details/81102948
今日推荐