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;
	}

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