org.apache.commons.net.ftp.FTPClient----根據文件名,路徑檢索文件 FTP上傳 下載 刪除文件

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

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

/**
 * 文本工具類
 * 
 * 
 */
public class TXTUtils {

	private static FTPClient ftp;

	private TXTUtils() {

	}

	/**
	 * @param ip
	 *            FTP服務器連接地址
	 * @param userName
	 *            用戶名
	 * @param passWord
	 *            密碼
	 * @param filePath
	 *            服務器文件路徑
	 * @param selName
	 *            文件名
	 * @return Map<String,String> key = time; value = fileName
	 * @throws Exception
	 * @author 
	 */
	public static Map<String, String> getFileNames(String ip, String userName,
			String passWord, String filePath, String selName) throws Exception {

		/* key = time; value = fileName */
		Map<String, String> nameMap = new HashMap<String, String>();

		FTPClient ftp = getFtpConn(ip, userName, passWord);
		try {
			String[] path = filePath.split(",");

			for (int j = 0; j < path.length; j++) {
				/* 更改ftp工作路徑 */
				if (ftp.changeWorkingDirectory(path[j])) {

					/* 獲得文件名稱 */
					String[] files = ftp.listNames();
					if (files == null || files.length == 0) {
						colsedFtpConn();
						return nameMap;
					}

					for (int i = 0; i < files.length; i++) {

						/* 文件格式名稱轉換,解決中文亂碼 */
						String name = new String(files[i]
								.getBytes("ISO-8859-1"), "GBK");

						if (name.equals(selName)) {
							continue;
						}

						/* key = time; value = fileName */
						nameMap.put(name.split("_")[0], name);
					}
				}
			}
			colsedFtpConn();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println(e);
			e.printStackTrace();

		}
		return nameMap;
	}

	/**
	 * 獲得連接後的ftp對象
	 * 
	 * @param ip
	 *            FTP服務器鏈接地址
	 * @param userName
	 *            用戶名
	 * @param passWord
	 *            密碼
	 * @return 獲得連接後的ftp對象
	 * @throws Exception
	 * @author 
	 */
	public static FTPClient getFtpConn(String ip, String userName,
			String passWord) throws Exception {
		if (ftp != null) {
			return ftp;
		}
		ftp = new FTPClient();
		ftp.connect(ip);
		ftp.login(userName, passWord);
		return ftp;
	}

	/**
	 * 釋放FTP連接
	 * 
	 * @author 
	 */
	public static void colsedFtpConn() {
		try {
			if (ftp != null) {
				ftp.disconnect();
				ftp = null;
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			ftp = null;
		}
	}

	/**  刪除文件
	 * @param ip
	 *            FTP服務器連接地址
	 * @param userName
	 *            用戶名
	 * @param passWord
	 *            密碼
	 * @param filePath
	 *            服務器文件路徑
	 * @param fileName
	 *            文件名
	 * @return boolean true爲成功
	 * @throws Exception
	 * @author 
	 */
	public static boolean deletefile(String ip, String userName,
			String passWord, String filePath, String filename) throws Exception {

		boolean bo = false;
		FTPClient ftp = getFtpConn(ip, userName, passWord);
		try {
			/* 更改ftp工作路徑 */
			if (ftp.changeWorkingDirectory(filePath)) {
				bo = ftp.deleteFile(filename);
			}
			colsedFtpConn();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println(e);
			e.printStackTrace();

		}
		return bo;
	}
上傳下載
/**
	 * 多文件上傳
	 * @param ip
	 *            FTP服務器連接地址
	 * @param userName
	 *            用戶名
	 * @param passWord
	 *            密碼
	 * @param path
	 *            服務器文件路徑
	 * @param fileName
	 *            文件名
	 * @param filepath
	 *            上傳文件的絕對路徑
	 * @return boolean true爲成功
	 */
	public static String[] uploadfile(String ip, String userName,
			String passWord, String path, String[] filename, String[] filepath) {

		Timestamp d = new Timestamp(System.currentTimeMillis());    //用時間LONG型作爲文件名存儲    防止LUNUX中文亂碼
		String[] uppath = new String[filename.length];
		FTPClient ftp;
		try {

			ftp = getFtpConn(ip, userName, passWord);

			if (ftp.changeWorkingDirectory(path)) {
				for (int i = 0; i < filename.length; i++) {
					File file = new File(filepath[i]);
					InputStream in = new FileInputStream(filepath[i]);
					uppath[i] = d.getTime() + "." + getFileHouZhui(filename[i]);
					ftp.storeFile(uppath[i], in);
				}
			}

			colsedFtpConn();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println(e);
			e.printStackTrace();

		}
		return uppath;
	}

	/**下載文件
	 * @param ip
	 *            FTP服務器連接地址
	 * @param userName
	 *            用戶名
	 * @param passWord
	 *            密碼
	 * @param filePath
	 *            服務器文件路徑
	 * @param fileName
	 *            文件名
	 * @return InputStream 返回文件字符流
	 */
	public static InputStream downfile(String ip, String userName,
			String passWord, String filePath, String filename)  {
		InputStream in = null;
	
		try {
			FTPClient ftp = getFtpConn(ip, userName, passWord);
			/* 更改ftp工作路徑 */
			if (ftp.changeWorkingDirectory(filePath)) {
				ftp.setBufferSize(1024);
				// 設置文件類型(二進制)
				ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
				in = ftp.retrieveFileStream(filename);
			}
			colsedFtpConn();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println(e);
			e.printStackTrace();

		}
		return in;
	}
	
	//獲取文件的文件類型

	public static String getFileHouZhui(String filename) {

		String fn[] = filename.split("[.]");
		String houzhuiname = fn[fn.length - 1];
		return houzhuiname;
	}

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章