• FTPS 客户端 demo,



    package han; import java.util.Vector; /** * Created by han on 2017/8/30. */ public class FtpTest { private static FtpClient ftpClient; public static void main (String[] args) throws Exception { ftpClient =new FtpClient( true); ftpClient.connect("127.0.0.1","a","a",990); ftpClient.getWorkingDirectory(); ftpClient.changeDir("/"); Vector<String> dirs= ftpClient.listSubDirInDir("/"); for (String dir :dirs){ Vector<String> files= ftpClient.listFileInDir("/"+dir); for (String file :files) System.out.println("/"+dir+"/"+new String(file.getBytes("ISO-8859-1"),"utf-8")); } ftpClient.uploadFile("F:\xampp\htdocs\test\www.php","/www.php"); } }
    package han;
    
    /**
     * Created by han on 2017/8/30.
     */
    import org.apache.commons.net.ftp.FTPClient;
    import org.apache.commons.net.ftp.FTPFile;
    import org.apache.commons.net.ftp.FTPReply;
    import org.apache.commons.net.ftp.FTPSClient;
    import org.apache.log4j.Logger;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Vector;
    
    public class FtpClient{
    
        private static final Logger logger = Logger.getLogger(FtpClient.class);
    
        private FTPClient client;
    
        public FtpClient(boolean ftps) {
            if (ftps) {
                client = new FTPSClient(true);
            } else {
                client = new FTPClient();
            }
        }
    
        public boolean changeDir(String remotePath) throws Exception {
            return client.changeWorkingDirectory(remotePath);
        }
    
        public boolean connect(String host, String login, String password, int port) throws Exception {
            logger.debug("FTP request connect to " + host + ":" + port);
            client.connect(host, port);
            int reply = client.getReplyCode();
            if (FTPReply.isPositiveCompletion(reply)) {
                logger.debug("FTP request login as " + login);
                if (client.login(login, password)) {
                    client.enterLocalPassiveMode();
                    return true;
                }
            }
            disconnect();
            return false;
        }
    
        public void disconnect() throws Exception {
            logger.debug("FTP request disconnect");
            client.disconnect();
        }
    
    
        protected boolean downloadFileAfterCheck(String remotePath, String localFile) throws IOException {
    
            boolean rst;
            FileOutputStream out = null;
            try {
                File file = new File(localFile);
                if (!file.exists()) {
                    out = new FileOutputStream(localFile);
                    rst = client.retrieveFile(remotePath, out);
                } else {
                    rst = true;
                }
            } finally {
                if (out != null) {
                    out.close();
                }
            }
            if (out != null) {
                out.close();
            }
            return rst;
        }
    
        protected boolean downloadFile(String remotePath, String localFile) throws IOException {
    
            boolean rst;
            FileOutputStream out = null;
            try {
                out = new FileOutputStream(localFile);
                rst = client.retrieveFile(remotePath, out);
            } finally {
                if (out != null) {
                    out.close();
                }
            }
            return rst;
        }
    
        public Vector<String> listFileInDir(String remoteDir) throws Exception {
            if (changeDir(remoteDir)) {
                FTPFile[] files = client.listFiles();
                Vector<String> v = new Vector<String>();
                for (FTPFile file : files) {
                    if (!file.isDirectory()) {
                        v.addElement(file.getName());
                    }
                }
                return v;
            } else {
                return null;
            }
        }
    
        public boolean uploadFile(String localFile, String remotePath) throws IOException {
            FileInputStream in = new FileInputStream(localFile);
            boolean rst;
            try {
                rst = client.storeFile(remotePath, in);
            } finally {
                in.close();
            }
            return rst;
        }
    
    
        public Vector<String> listSubDirInDir(String remoteDir) throws Exception {
            if (changeDir(remoteDir)) {
                FTPFile[] files = client.listFiles();
                Vector<String> v = new Vector<String>();
                for (FTPFile file : files) {
                    if (file.isDirectory()) {
                        v.addElement(file.getName());
                    }
                }
                return v;
            } else {
                return null;
            }
        }
    
        protected boolean createDirectory(String dirName) {
            try {
                return client.makeDirectory(dirName);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return false;
        }
    
    
        public boolean isARemoteDirectory(String path) {
            String cache = "/";
            try {
                cache = client.printWorkingDirectory();
            } catch (NullPointerException e) {
                //nop
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                boolean isDir = changeDir(path);
                changeDir(cache);
                return isDir;
            } catch (IOException e) {
                //e.printStackTrace();
            } catch (Exception e) {
                //e.printStackTrace();
            }
            return false;
        }
    
        public String getWorkingDirectory() {
            try {
                return client.printWorkingDirectory();
            } catch (IOException e) {
            }
            return null;
        }
    
    }
    

      

      

  • 相关阅读:
    wrap,wrapall,wrapinner的区别:
    jqueryappend和appendTo的区别
    《Nagios系统监控实践》一书出版
    基于Python的密码生成程序的优化
    Puppet学习:pp文件权限问题
    依然看不清
    免费编程中文书籍索引
    Linux批量修改指定目录下的文件或文件夹权限
    puppet学习:文件夹权限的问题
    Zabbix探索:Zabbix API使用时的错误1
  • 原文地址:https://www.cnblogs.com/cndavy/p/7452658.html
Copyright © 2020-2023  润新知