JAVA ftp连接池功能实现

Posted ﹏Sakura

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA ftp连接池功能实现相关的知识,希望对你有一定的参考价值。

抽象类:

package com.echo.store;

import java.util.Enumeration;
import java.util.Hashtable;

abstract class ObjectPool<T> {

    long ttl ;
    Hashtable<T,Long> lock,unlock ;

    public ObjectPool() {
        ttl = 50000;
        lock = new Hashtable<T, Long>();
        unlock = new Hashtable<T,Long>();
    }

    abstract T create();

    abstract boolean valide(T t);

    abstract void destory( T t );

    synchronized public T takeOut(){

        long now = System.currentTimeMillis();
        T t ;
        if(unlock.size() > 0){
            Enumeration<T> e = unlock.keys();
            while ( e.hasMoreElements() ){
                t = e.nextElement();
                if( ( now-unlock.get(t) > ttl) || !valide(t) ){
                    unlock.remove(t);
                    destory(t);
                    t = null;
                }else{
                    unlock.remove(t);
                    lock.put(t, ttl);
                    return t;
                }
            }
        }
        t = create();
        lock.put(t,ttl);
        return t;
    }

    synchronized public void takeIn( T t ){
        lock.remove(t);
        unlock.put( t, ttl);
    }






}

 

Ftp链接池实现类:

package com.echo.store;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;

import java.io.IOException;
import java.util.Hashtable;

public class FtpPool extends ObjectPool<FTPClient> {

    FtpConfig ftpConfig;

    public static Hashtable<FtpConfig, FtpPool> instance = new Hashtable<FtpConfig, FtpPool>();

    public static final FtpPool getInstance( FtpConfig cfg ){
        if (instance.get(cfg) == null) {
            synchronized (FtpPool.class){
                if (instance.get(cfg) == null) {
                    instance.put(cfg, new FtpPool( cfg ));
                }
            }
        }
        return instance.get(cfg);
    }



    public FtpPool( FtpConfig ftpCfg ) {
        ftpConfig = ftpCfg;
    }

    @Override
    FTPClient create( ) {
        FTPClient ftpClient = new FTPClient();
        try {
            ftpClient.connect(ftpConfig.getIp());
            ftpClient.login(ftpConfig.getUser(),ftpConfig.getPasswd());
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return ftpClient;
    }

    @Override
    boolean valide(FTPClient ftpClient) {
        return ftpClient.isConnected();
    }

    @Override
    void destory(FTPClient ftpClient) {
        //ftpClient.;
    }
}

 

调用方法:

   public static void main(String[] args) {

        FtpConfig cfg = new FtpConfig();
        cfg.setIp("xxx");
        cfg.setUser("xxx");
        cfg.setPasswd("vvv");
        FtpPool instance = FtpPool.getInstance(cfg);
        FTPClient ftpClient = instance.takeOut();
        FtpOperation ftpOperation = FtpOperation.getInstance();
        ftpOperation.download(ftpClient,"/remote", "./location");
    }

 

 上传方法实现:

package com.echo.services;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class FtpOperation {

    Logger log = LoggerFactory.getLogger(FtpOperation.class);

    public static FtpOperation instance = null;
    private FtpOperation() {}
    public static FtpOperation getInstance(){
        if( instance == null ){
            synchronized (FtpOperation.class){
                if (instance == null) {
                    instance = new FtpOperation();
                }
            }
        }
        return instance;
    }



    public void download(FTPClient ftpClient, String remote, String loc  ){

        try {
            log.info("remote :"+remote);
            ftpClient.changeWorkingDirectory(remote);
            //生成本地目录
            File locFile = new File(loc);
            if( !locFile.exists() ){
                locFile.mkdir();
            }
            downLoadFile(ftpClient, loc);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    private void downLoadFile( FTPClient ftpClient, String LocDir   ){

        try {
            for (FTPFile ftpFile : ftpClient.listFiles()) {
                String path = LocDir+"/"+ftpFile.getName();
                if( ftpFile.isDirectory() ){
                    //生成本地目录
                    File localFile = new File( path );
                    if( !localFile.exists() ){
                        localFile.mkdir();
                    }
                    //浏览Ftp目录
                    ftpClient.changeWorkingDirectory( ftpFile.getName());
                    downLoadFile(ftpClient, path );
                    ftpClient.changeToParentDirectory();
                }else{
                    if(
                       (   !ftpFile.getName().equals("ualice.access.log") )
                    ){
                        return;
                    }
                    FileOutputStream fostream = new FileOutputStream( path );
                    ftpClient.retrieveFile(ftpFile.getName(), fostream);
                    fostream.flush();
                    fostream.close();
                    log.info("文件:"+LocDir+ftpFile.getName()+" 下载完毕。");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }



}

 

以上是关于JAVA ftp连接池功能实现的主要内容,如果未能解决你的问题,请参考以下文章

使用commons-pool2实现FTP连接池

数据库连接池的Java连接池

怎么用Java实现FTP上传

教你如何快速实现一个连接池?

java Ftp上传创建多层文件的代码片段

java中怎么实现ftp服务器