1. 程式人生 > >JAVA ftp連接池功能實現

JAVA ftp連接池功能實現

java ftp ble bin sdi try ice remote access xxx

抽象類:

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連接池功能實現