FtpUtil.java

8013 단어 자바

package com.ailk.ess.webapp2.servermng.net;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPConnectionClosedException;

public class FtpUtil {
	private FTPClient ftpClient = null;

	// ftp     
	private String hostName;

	// ftp       
	public static int defaultport = 21;

	//    
	private String userName;

	//     
	private String password;

	//          
	private String remoteDir;

	/** */
	/**
	 * @param hostName
	 *                
	 * @param port
	 *               
	 * @param userName
	 *               
	 * @param password
	 *              
	 * @param remoteDir
	 *                  
	 * @param is_zhTimeZone
	 *                 FTP Server 
	 * @return
	 */
	public FtpUtil(String hostName, int port, String userName, String password,
			String remoteDir, boolean is_zhTimeZone) {
		this.hostName = hostName;
		this.userName = userName;
		this.password = password;
		this.remoteDir = remoteDir == null ? "" : remoteDir;
		this.ftpClient = new FTPClient();
		if (is_zhTimeZone) {
			this.ftpClient.configure( FtpUtil.Config() );
			this.ftpClient.setControlEncoding("GBK");
		}
		//  
		this.login();
		//    
		this.changeDir(this.remoteDir);
		this.setFileType(FTPClient.ASCII_FILE_TYPE);
		
		ftpClient.setDefaultPort(port);
	}

	/**
	 *   FTP   
	 */
	public void login() {
		try {
			ftpClient.connect(this.hostName);
			ftpClient.login(this.userName, this.password);
		} catch (FTPConnectionClosedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("   ftp   :" + this.hostName + "   ..    ");
	}

	private static FTPClientConfig Config() {
		FTPClientConfig conf = new FTPClientConfig( FTPClientConfig.SYST_UNIX );
		conf.setRecentDateFormatStr("MM dd  HH:mm");
		// conf.setRecentDateFormatStr("(YYYY )?MM dd ( HH:mm)?");
		return conf;
	}

	/**
	 *       
	 * 
	 * @param remoteDir
	 *  
	 */
	public void changeDir(String remoteDir) {
		try {
			this.remoteDir = remoteDir;
			ftpClient.changeWorkingDirectory(remoteDir);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("       :" + remoteDir);
	}

	/**
	 *        (   )
	 */
	public void toParentDir() {
		try {
			ftpClient.changeToParentDirectory();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 *              
	 */
	public String[] ListAllFiles() {
		String[] names = this.ListFiles("*");
		return this.sort(names);
	}

	/**
	 *               
	 * 
	 * @param dir
	 *            exp: /cim/
	 * @param file_regEx
	 *                *
	 */
	public String[] ListAllFiles(String dir, String file_regEx) {
		String[] names = this.ListFiles( dir + file_regEx );
		return this.sort(names);
	}

	/**
	 *       
	 * 
	 * @param file_regEx
	 *                ,    *
	 */
	public String[] ListFiles(String file_regEx) {
		try {
			/**//*
				 * FTPFile[] remoteFiles = ftpClient.listFiles(file_regEx);
				 * //System.out.println(remoteFiles.length); String[] name = new
				 * String[remoteFiles.length]; if(remoteFiles != null) { for(int
				 * i=0;i<remoteFiles.length;i++) { if(remoteFiles[i] == null)
				 * name[i] = ""; else
				 * if(remoteFiles[i].getName()==null||remoteFiles
				 * [i].getName().equals
				 * (".")||remoteFiles[i].getName().equals("..")) { name[i] = "";
				 * } else name[i] = remoteFiles[i].getName();
				 * System.out.println(name[i]); } }
				 */
			String[] name = ftpClient.listNames( file_regEx );
			if (name == null)
				return new String[0];
			
			return this.sort(name);
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new String[0];
	}

	public void Lists(String reg) {
		try {
			String[] a = ftpClient.listNames( reg );
			if( a!=null ){
				for (String b : a) {
					System.out.println(b);
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 *          [           ]
	 * 
	 * @param fileType
	 *            --BINARY_FILE_TYPE,ASCII_FILE_TYPE
	 */
	public void setFileType(int fileType) {
		try {
			ftpClient.setFileType(fileType);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 *     
	 * 
	 * @param localFilePath
	 *            --      +   
	 * @param newFileName
	 *            --     
	 */
	public void uploadFile(String localFilePath, String newFileName) {
		//     
		BufferedInputStream buffIn = null;
		try {
			buffIn = new BufferedInputStream(new FileInputStream(localFilePath));
			ftpClient.storeFile(newFileName, buffIn);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (buffIn != null)
					buffIn.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 *     (  )
	 * 
	 * @param remoteFileName
	 *            --        
	 * @param localFileName
	 *            --     
	 */
	public String downloadFile(String remoteFileName, String localFileName) {
		BufferedOutputStream buffOut = null;
		try {
			buffOut = new BufferedOutputStream(new FileOutputStream(
					localFileName));
			ftpClient.retrieveFile(remoteFileName, buffOut);
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		} finally {
			try {
				if (buffOut != null)
					buffOut.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return localFileName;
	}

	/**
	 *   FTP  
	 */
	public void close() {
		try {
			if (ftpClient != null) {
				ftpClient.logout();
				ftpClient.disconnect();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 *        (    )
	 */
	public String[] sort(String[] str_Array) {
		if (str_Array == null) {
			throw new NullPointerException("The str_Array can not be null!");
		}
		String tmp = "";
		for (int i = 0; i < str_Array.length; i++) {
			for (int j = 0; j < str_Array.length - i - 1; j++) {
				if (str_Array[j].compareTo(str_Array[j + 1]) < 0) {
					tmp = str_Array[j];
					str_Array[j] = str_Array[j + 1];
					str_Array[j + 1] = tmp;
				}
			}
		}
		return str_Array;
	}

}



좋은 웹페이지 즐겨찾기