基于commons-net实现ftp创建文件夹、上传、下载功能.

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
基于commons-net实现ftp创建文件夹、上传、下载功能.
FtpClientUtil
    package com.demo.ftp;  
      
    import java.io.FileInputStream;  
    import java.io.FileOutputStream;  
    import java.io.IOException;  
    import java.io.InputStream;  
    import java.io.OutputStream;  
    import java.io.PrintWriter;  
    import java.net.ConnectException;  
      
    import org.apache.commons.net.PrintCommandListener;  
    import org.apache.commons.net.ftp.FTP;  
    import org.apache.commons.net.ftp.FTPClient;  
    import org.apache.commons.net.ftp.FTPReply;  
      
    /** 
     * @author wobendiankun 2015-1-8 下午08:11:08 
     */  
    public class FtpClientUtil {  
        /** 
         * ftp服务器地址 
         */  
        private  String server;  
        /** 
         * 端口 
         */  
        private  int port;  
        /** 
         * 用户名 
         */  
        private  String username;  
        /** 
         *  
         */  
        private  String password;  
          
        public FtpClientUtil () {  
            server = "localhost";  
            port = 21;  
            username = "kun";  
            password = "111111";  
        }  
          
        public FtpClientUtil(String server, int port, String username,  
                String password) {  
            this.server = server;  
            this.port = port;  
            this.username = username;  
            this.password = password;  
        }  
        /**上传文件 
         * @param remoteFileName 远程文件名称 
         * @param locaFileName 本地文件名称 
         */  
        public void upload(String remoteFileName,String locaFileName){  
            FTPClient ftp=null;   
            try {  
                ftp = new FTPClient();  
                ftp.addProtocolCommandListener( new PrintCommandListener( new PrintWriter( System.out ), true ) );  
                //连接ftp服务器  
                connect( ftp );  
                //设置属性  
                setProperty( ftp );  
                //上传文件  
                upload( ftp, remoteFileName, locaFileName );  
                //退出  
                logout( ftp );  
            } catch (Exception e) {  
                e.printStackTrace();  
            }finally {  
                if (ftp.isConnected()) {  
                    try {  
                        ftp.disconnect();  
                    } catch (IOException f) {  
                    }  
                }  
            }  
              
        }  
        /**上传文件 
         * @param remoteFileName 远程文件名称 
         * @param locaFileName 本地文件名称 
         */  
        public void download(String remoteFileName,String locaFileName){  
            FTPClient ftp=null;   
            try {  
                ftp = new FTPClient();  
                ftp.addProtocolCommandListener( new PrintCommandListener( new PrintWriter( System.out ), true ) );  
                //连接ftp服务器  
                connect( ftp );  
                //设置属性  
                setProperty( ftp );  
                //下载文件  
                download( ftp, remoteFileName, locaFileName );  
                //退出  
                logout( ftp );  
            } catch (Exception e) {  
                e.printStackTrace();  
            }finally {  
                if (ftp.isConnected()) {  
                    try {  
                        ftp.disconnect();  
                    } catch (IOException f) {  
                    }  
                }  
            }  
        }  
        /**创建文件夹 
         * @param remotePathName 远程文件夹名称 
         */  
        public void mkdir(String remotePathName) {  
            FTPClient ftp=null;   
            try {  
                ftp = new FTPClient();  
                ftp.addProtocolCommandListener( new PrintCommandListener( new PrintWriter( System.out ), true ) );  
                //连接ftp服务器  
                connect( ftp );  
                //设置属性  
                setProperty( ftp );  
                //创建文件夹  
                mkdir( ftp, remotePathName);  
                //退出  
                logout( ftp );  
            } catch (Exception e) {  
                e.printStackTrace();  
            }finally {  
                if (ftp.isConnected()) {  
                    try {  
                        ftp.disconnect();  
                    } catch (IOException f) {  
                    }  
                }  
            }  
        }  
        /** 
         * @param ftp 
         * @param remotePathName 
         */  
        private void mkdir(FTPClient ftp, String remotePathName) throws Exception{  
            ftp.makeDirectory(remotePathName);  
        }  
      
        /** 
         * @param ftp 
         * @param remoteFileName 
         * @param locaFileName 
         */  
        private void download(FTPClient ftp, String remoteFileName,  
                String locaFileName) throws Exception{  
            OutputStream output=null;  
            output = new FileOutputStream(locaFileName);  
            ftp.retrieveFile(remoteFileName, output);   
            output.close();  
        }  
      
        /** 
         * @param ftp 
         * @throws Exception  
         */  
        private void setProperty(FTPClient ftp) throws Exception {  
            ftp.enterLocalPassiveMode();  
            //二进制传输,默认为ASCII  
            ftp.setFileType(FTP.BINARY_FILE_TYPE);  
        }  
      
        /** 
         * @param ftp 
         */  
        private void logout(FTPClient ftp) throws Exception{  
            ftp.noop();  
            ftp.logout();  
        }  
      
        /** 
         * @param ftp 
         * @param remoteFileName 
         * @param locaFileName 
         */  
        private void upload(FTPClient ftp, String remoteFileName,  
                String locaFileName) throws Exception{  
            //上传  
            InputStream input;  
      
            input = new FileInputStream(locaFileName);  
      
            ftp.storeFile(remoteFileName, input);  
      
            input.close();  
        }  
      
        /** 
         * @param ftp 
         */  
        private void connect(FTPClient ftp) throws Exception{  
            //连接服务器  
            ftp.connect( server, port);  
            int reply = ftp.getReplyCode();  
            //是否连接成功  
            if ( !FTPReply.isPositiveCompletion( reply ) )         {  
                throw new ConnectException( server+" 服务器拒绝连接" );  
            }  
            //登陆  
            if (!ftp.login(username, password)) {  
                 throw new ConnectException( "用户名或密码错误" );  
            }  
        }  
    }  

FtpTest
    package com.demo.ftp;  
      
    /** 
     * @author wobendiankun 2015-1-8 下午08:34:36 
     */  
    public class FtpTest {  
      
        public static void main(String[] args) {  
            //上传文件  
            //upload();  
            //下载文件  
            //download();  
            //创建文件夹  
            mkdir();  
        }  
      
        /** 
         *  
         */  
        private static void mkdir() {  
            FtpClientUtil clientUtil=new FtpClientUtil();  
            clientUtil.mkdir("test");  
        }  
      
        /** 
         *  
         */  
        private static void download() {  
            String remoteFileName="./111.txt";  
            String locaFileName="F:\\test\\txt\\3333.txt";  
            FtpClientUtil clientUtil=new FtpClientUtil();  
            clientUtil.download(remoteFileName, locaFileName);  
        }  
      
        /** 
         *  
         */  
        private static void upload() {  
            String remoteFileName="./111.txt";  
            String locaFileName="F:\\test\\txt\\aaa.txt";  
            FtpClientUtil clientUtil=new FtpClientUtil();  
            clientUtil.upload(remoteFileName, locaFileName);  
        }  
    }  

标签: ftp服务器 ftp服务器地址 isp 服务器 服务器地址 连接ftp服务器

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:java zip压缩与解压-支持空目录,保留文件修改时间

下一篇:Java 基于JavaMail实现向QQ邮箱发送邮件