中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

基于FTP4J組件的FTP操作客戶端

2018-07-20    來(lái)源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用
基于FTP4J組件的FTP操作客戶端,支持上傳、下載,兩臺(tái)FTP服務(wù)器文件流同步。
package com.matol.utils
 
import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.FTPDataTransferListener;
import it.sauronsoftware.ftp4j.FTPFile;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
 
/** 
* 基于FTP4J組件的FTP操作客戶端
* ww
*/ 
public final class FTPToolkit { 
    //單例模型
    private FTPToolkit() { } 
 
    /** 
     * 創(chuàng)建FTP連接 
     * @param host     主機(jī)名或IP 
     * @param port     ftp端口 
     * @param username ftp用戶名 
     * @param password ftp密碼 
     * @return 一個(gè)單例客戶端 
     */ 
    public static FTPClient ftpConn(String host, int port, String username, String password) { 
        FTPClient client = new FTPClient(); 
        try { 
            client.connect(host, port); 
            client.login(username, password);
            client.setCharset("GBK");
            client.setType(FTPClient.TYPE_BINARY); 
        } catch (Exception e) { 
            System.out.println("###[Error] FTPToolkit.ftpConn() "+e.getMessage());
        } 
        return client; 
    } 
     
    /**
     * 取得目錄下所有文件列表
     * @param path
     * @return
     */
    public static List<FTPFile> getlistFiles(FTPClient client,String path) {
        List<FTPFile> filesList = new ArrayList<FTPFile>();
        try {
            client.changeDirectory(path);
            FTPFile[] fileNames = client.list(); //.listNames();
            if (null != fileNames) {
                for (FTPFile file : fileNames) {
                    if(file.getType() == FTPFile.TYPE_FILE){
                        filesList.add(file);
                        //System.out.println(file.getName()+" | "+file.getSize()+" | "+DateUtil.converDateToString(file.getModifiedDate(),DateUtil.defaultDatePattern));
                    }
                }
            }
        } catch (Exception e) {
            System.out.println("###[Error] FTPToolkit.getlistFiles()"+ e.getMessage());
        }
        return filesList;
    }
    /**
     * 取得目錄下所有文件列表
     * @param path
     * @return
     */
    public static List<String> getlistFileNames(FTPClient client,String path) {
        List<String> filesList = new ArrayList<String>();
        try {
            client.changeDirectory(path);
            String[] fileNames = client.listNames();
            if (null != fileNames) {
                for (String file : fileNames) {
                    filesList.add(path+"/"+file);
                    //System.out.println(path+"/"+file);
                }
            }
        } catch (Exception e) {
            System.out.println("###[Error] FTPToolkit.getlistFileNames()"+ e.getMessage());
        }
        return filesList;
    }    
     
    /**
     * 根據(jù)文件路徑獲取文件流
     * @param path
     * @return
    */
    public static InputStream fetchInputStream(FTPClient client,String ftpFilePath,String ftpPath) {
        InputStream is = null;
        try {
            String localFilePath = getFileName("ftptmp/"+ftpFilePath) + "_ftp.tmp";
            File tempLocalFile = new File(localFilePath); 
            if (!tempLocalFile.exists()) {
                tempLocalFile.createNewFile();
            }
             
            client.download(ftpPath, tempLocalFile);
            is = new FileInputStream(tempLocalFile);
 
            //刪除臨時(shí)文件(由于不能關(guān)閉文件流,刪除臨時(shí)文件無(wú)效,所以最好使用deleteOnExit,外層程序使用文件流結(jié)束后,關(guān)閉流,自動(dòng)刪除)
            //tempLocalFile.deleteOnExit();
        } catch (Exception e) {
            System.out.println("###[Error] FTPToolkit.fetchInputStream()"+e.getMessage());
        }
        return is;
    }
    
    /**
    * 上傳文件到FTP
    * @param ftpFilePath 要上傳到FTP的路徑
    * @param source 源數(shù)據(jù)
    */
   public static void uploadInputStream(FTPClient client,String ftpFilePath, InputStream ftpFile) {
       try {
           mkDirs(client,ftpFilePath.substring(0, ftpFilePath.lastIndexOf("/")));
            
           client.upload(ftpFilePath, ftpFile, 0l, 0l, null);
       } catch (Exception e) {
           System.out.println("###[Error] FTPToolkit.uploadInputStream()"+e.getMessage());
       }finally{
           try {
               //關(guān)閉文件流
               ftpFile.close();
            } catch (Exception e) {}
       }
   }
 
    
    /** 
     * FTP下載文件到本地一個(gè)文件夾,如果本地文件夾不存在,則創(chuàng)建必要的目錄結(jié)構(gòu) 
     * @param client          FTP客戶端 
     * @param remoteFileName  FTP文件 
     * @param localFolderPath 存的本地目錄 
     */ 
    public static void download(FTPClient client, String remoteFileName, String localFolderPath) {
        try {
            int x = isExist(client, remoteFileName);
            MyFtpListener listener = MyFtpListener.instance("download"); 
            File localFolder = new File(localFolderPath); 
            if (!localFolder.exists()) {
                localFolder.mkdirs();
            } 
            if (x == FTPFile.TYPE_FILE) { 
                String localfilepath = formatPath4File(localFolderPath+File.separator+new File(remoteFileName).getName()); 
                if (listener != null) {
                    client.download(remoteFileName, new File(localfilepath), listener); 
                }else{ 
                    client.download(remoteFileName, new File(localfilepath));
                } 
            }
        } catch (Exception e) {
            System.out.println("###[Error] FTPToolkit.download()"+e.getMessage());
        } 
    } 
 
     
    //遞歸創(chuàng)建層級(jí)目錄(坑爹的問(wèn)題,原本想著遞歸處理,后來(lái)才發(fā)現(xiàn)如此的簡(jiǎn)單)
    private static void mkDirs(FTPClient client,String p) throws Exception {
        if (null == p) { return;}
 
        if(p != null && !"".equals(p) && !"/".equals(p)){
            String ps = "";
            for(int i=0;i<p.split("/").length;i++){
                ps += p.split("/")[i]+"/";
                if (!isDirExist(client,ps)) {
                    client.createDirectory(ps);// 創(chuàng)建目錄
                    client.changeDirectory(ps);// 進(jìn)入創(chuàng)建的目錄
                    System.out.println(">>>>> create directory:["+i+"]["+ps+"]");
                }else{
                    //System.out.println("select directory:["+i+"]["+ps+"]");
                }
            }
        }
    }
    //檢查目錄是否存在
    private static boolean isDirExist(FTPClient client,String dir) {
        try {
            client.changeDirectory(dir);
        } catch (Exception e) {
            return false;
        }
        return true;
    }
 
    /** 
     * FTP上傳本地文件到FTP的一個(gè)目錄下 
     * @param client           FTP客戶端 
     * @param localfilepath    本地文件路徑 
     * @param remoteFolderPath FTP上傳目錄 
     */ 
    public static void upload(FTPClient client, String localfilepath, String remoteFolderPath) { 
        try {
            mkDirs(client,remoteFolderPath);
             
            File localfile = new File(localfilepath); 
            upload(client, localfile, remoteFolderPath);
        } catch (Exception e) {
            System.out.println("###[Error] FTPToolkit.upload()"+e.getMessage());
        } 
    }    
    /** 
     * FTP上傳本地文件到FTP的一個(gè)目錄下 
     * @param client           FTP客戶端 
     * @param localfile        本地文件 
     * @param remoteFolderPath FTP上傳目錄 
     */ 
    public static void upload(FTPClient client, File localfile, String remoteFolderPath) { 
        remoteFolderPath = formatPath4FTP(remoteFolderPath); 
        MyFtpListener listener = MyFtpListener.instance("upload"); 
        try { 
            client.changeDirectory(remoteFolderPath); 
            if (listener != null) {
                client.upload(localfile, listener); 
            }else{
                client.upload(localfile); 
            }
            client.changeDirectory("/"); 
        } catch (Exception e) { 
            System.out.println("###[Error] FTPToolkit.upload()"+e.getMessage());
        } 
    } 
  
    /** 
     * 批量上傳本地文件到FTP指定目錄上 
     * @param client           FTP客戶端 
     * @param localFilePaths   本地文件路徑列表 
     * @param remoteFolderPath FTP上傳目錄 
     */ 
    public static void uploadListPath(FTPClient client, List<String> localFilePaths, String remoteFolderPath) { 
        try { 
            remoteFolderPath = formatPath4FTP(remoteFolderPath); 
            client.changeDirectory(remoteFolderPath); 
            MyFtpListener listener = MyFtpListener.instance("uploadListPath"); 
            for (String path : localFilePaths) { 
                File file = new File(path);
                if (listener != null) {
                    client.upload(file, listener); 
                }else {
                    client.upload(file); 
                }
            } 
            client.changeDirectory("/"); 
        } catch (Exception e) { 
            System.out.println("###[Error] FTPToolkit.uploadListPath()"+e.getMessage());
        } 
    } 
    /** 
     * 批量上傳本地文件到FTP指定目錄上 
     * @param client           FTP客戶端 
     * @param localFiles       本地文件列表 
     * @param remoteFolderPath FTP上傳目錄 
     */ 
    public static void uploadListFile(FTPClient client, List<File> localFiles, String remoteFolderPath) { 
        try { 
            client.changeDirectory(remoteFolderPath); 
            remoteFolderPath = formatPath4FTP(remoteFolderPath); 
            MyFtpListener listener = MyFtpListener.instance("uploadListFile"); 
            for (File file : localFiles) { 
                if (listener != null){ 
                    client.upload(file, listener); 
                }else{ 
                    client.upload(file); 
                }
            } 
            client.changeDirectory("/"); 
        } catch (Exception e) { 
            System.out.println("###[Error] FTPToolkit.uploadListFile() "+e.getMessage());
        } 
    } 
     
 
 
    /** 
     * 判斷一個(gè)FTP路徑是否存在,如果存在返回類型(FTPFile.TYPE_DIRECTORY=1、FTPFile.TYPE_FILE=0、FTPFile.TYPE_LINK=2)
     * @param client     FTP客戶端 
     * @param remotePath FTP文件或文件夾路徑 
     * @return 存在時(shí)候返回類型值(文件0,文件夾1,連接2),不存在則返回-1 
     */ 
    public static int isExist(FTPClient client, String remotePath) { 
        int x = -1; 
        try {
            remotePath = formatPath4FTP(remotePath); 
             
            FTPFile[] list = client.list(remotePath);
            if (list.length > 1){ 
                x = 1; 
            }else if (list.length == 1) { 
                FTPFile f = list[0]; 
                if (f.getType() == FTPFile.TYPE_DIRECTORY) {
                    x = 1;
                }
                //假設(shè)推理判斷 
                String _path = remotePath + "/" + f.getName(); 
                if(client.list(_path).length == 1){
                    x = 1;
                }else{
                    x = 0;
                }
            } else { 
                client.changeDirectory(remotePath); 
                x = 1; 
            }
        } catch (Exception e) {
            x = -1; 
            System.out.println("###[Error] FTPToolkit.isExist() "+e.getMessage());
        } 
         
        return x;
    } 
 
    /** 
     * 關(guān)閉FTP連接,關(guān)閉時(shí)候像服務(wù)器發(fā)送一條關(guān)閉命令 
     * @param client FTP客戶端 
     * @return 關(guān)閉成功,或者鏈接已斷開(kāi),或者鏈接為null時(shí)候返回true,通過(guò)兩次關(guān)閉都失敗時(shí)候返回false 
     */ 
    public static boolean closeConn(FTPClient client) { 
        if (client == null) return true; 
        if (client.isConnected()) { 
            try { 
                client.disconnect(true); 
                return true; 
            } catch (Exception e) { 
                try { 
                    client.disconnect(false); 
                } catch (Exception e1) {} 
            } 
        } 
        return true; 
    } 
     
     
     
    //###---------------------------------------------------------------------
    /** 
     * 格式化文件路徑,將其中不規(guī)范的分隔轉(zhuǎn)換為標(biāo)準(zhǔn)的分隔符,并且去掉末尾的文件路徑分隔符。 
     * 本方法操作系統(tǒng)自適應(yīng) 
     * @param path 文件路徑 
     * @return 格式化后的文件路徑 
     */ 
    public static String formatPath4File(String path) { 
        String reg0 = "\\\\+"; 
        String reg = "\\\\+|/+"; 
        String temp = path.trim().replaceAll(reg0, "/"); 
        temp = temp.replaceAll(reg, "/"); 
        if (temp.length() > 1 && temp.endsWith("/")) { 
            temp = temp.substring(0, temp.length() - 1); 
        } 
        temp = temp.replace('/', File.separatorChar); 
        return temp; 
    } 
    /** 
     * 格式化文件路徑,將其中不規(guī)范的分隔轉(zhuǎn)換為標(biāo)準(zhǔn)的分隔符 
     * 并且去掉末尾的"/"符號(hào)(適用于FTP遠(yuǎn)程文件路徑或者Web資源的相對(duì)路徑)。 
     * @param path 文件路徑 
     * @return 格式化后的文件路徑 
     */ 
    public static String formatPath4FTP(String path) { 
        String reg0 = "\\\\+"; 
        String reg = "\\\\+|/+"; 
        String temp = path.trim().replaceAll(reg0, "/"); 
        temp = temp.replaceAll(reg, "/"); 
        if (temp.length() > 1 && temp.endsWith("/")) { 
            temp = temp.substring(0, temp.length() - 1); 
        } 
        return temp; 
    } 
    /** 
     * 獲取FTP路徑的父路徑,但不對(duì)路徑有效性做檢查 
     * @param path FTP路徑 
     * @return 父路徑,如果沒(méi)有父路徑,則返回null 
    */ 
    public static String genParentPath4FTP(String path) { 
        String f = new File(path).getParent(); 
        if (f == null) {
            return null; 
        }else {
            return formatPath4FTP(f); 
        }
    } 
     
    //###---------------------------------------------------------------------
  
    //獲取指定目錄下的文件
    public static File[] getPathFiles(String path){
        File file = new File(path);  
        if (file.isDirectory()) {  
            File[] files = file.listFiles();  
            return files;
        }  
        return null;
    }
     
    //刪除指定目錄下的臨時(shí)文件
    public static void deleteFiles(String path){
        File file = new File(path);  
        if (file.isDirectory()) {  
            File[] files = file.listFiles();  
            for (int i = 0; i < files.length; i++) {  
                //String name = files[i].getName();  
                //if(name.trim().toLowerCase().endsWith("_ftp.tmp")) {  
                    //System.out.println(name + "\t");  
                //}
                //清空臨時(shí)文件 _ftp.tmp
                files[i].delete();
            }  
        }  
    }
     
    //截取文件名稱
    public static String getFileName(String fileName){
        String fs = "F"+System.currentTimeMillis();
        try {
            if(fileName != null && !"".equals(fileName)){
                if(fileName.lastIndexOf("/") > 0 && fileName.lastIndexOf(".") > 0){
                    fs = fileName.substring(fileName.lastIndexOf("/")+1);
                }else{
                    fs = fileName;
                }
                 
                if(fs.length() >50){
                    fs = fs.substring(0,50);
                }
                return fs;
            }else{
                return fs;
            }
        } catch (Exception e) {
            System.out.println("###[Error] FtpTools.getFileName()"+e.getMessage());
        }
        return fs;
    }
     
     
     
    public static void main(String args[]) throws Exception { 
        FTPClient f41 = FTPToolkit.ftpConn("129.2.8.31", 21, "root", "root31");
        FTPClient f42 = FTPToolkit.ftpConn("129.2.8.32", 21, "root", "root32");
       
         
        //InputStream in = FTPToolkit.fetchInputStream(f41,"/home/photofile/wenzi-003.jpg");
        //FTPToolkit.uploadInputStream(f41,"/home/photofile/aa.jpg",in);
        //FTPToolkit.upload(f41, "E:\\demo\\av1.jpg", "/home/photofile/10/20"); 
        //FTPToolkit.download(f41, "/home/photofile/wenzi-100.jpg", "E:\\"); 
         
        //FTPToolkit.getlistFiles(f41,"/home/photofile");
         
        //文件流對(duì)流讀取操作
        List<String> ls = null;//FTPToolkit.getlistFileNames(f41,"/home/photofile");
        if(ls != null && ls.size() >0){
            InputStream in=null;
            for(int i =0;i<ls.size();i++){
                //in = FTPToolkit.fetchInputStream(f41,ls.get(i));
                if(in != null && in.available() > 0){
                      FTPToolkit.uploadInputStream(f42,"/home/wasadmin/photoSysTempFTP/"+getFileName(ls.get(i)),in);
                      f41.deleteFile(ls.get(i));
                }
            }
             
            //清空項(xiàng)目下的臨時(shí)文件
            FTPToolkit.deleteFiles("ftptmp");
        }
      
        FTPToolkit.closeConn(f41); 
        FTPToolkit.closeConn(f42); 
    }
}
 
 
/** 
* FTP監(jiān)聽(tīng)器
*/ 
class MyFtpListener implements FTPDataTransferListener { 
    private String tag;
    private MyFtpListener(String tags) { 
            this.tag = tags; 
    } 
     
    public static MyFtpListener instance(String tags) { 
        return new MyFtpListener(tags); 
    } 
 
    public void started() { 
            //System.out.println(tag+ ":FTP啟動(dòng)。。。。。。"); 
    } 
    public void transferred(int length) { 
            //System.out.println(tag+ ":FTP傳輸["+length+"]。。。。。。"); 
    } 
    public void completed() { 
            //System.out.println(tag+ ":FTP完成。。。。。。"); 
    } 
    public void aborted() { 
            //System.out.println(tag+ ":FTP中止。。。。。。"); 
    } 
    public void failed() { 
            //System.out.println(tag+ ":FTP掛掉。。。。。。"); 
    } 
}

標(biāo)簽: ftp服務(wù)器 服務(wù)器

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:java 定時(shí)備份數(shù)據(jù)庫(kù)

下一篇:Linux下實(shí)現(xiàn)tomcat定時(shí)自動(dòng)重啟