Java 连接FTP

连接FTP、获取文件、删除文件

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.log4j.Logger;

/**
 * 读取FTP server下文件
 * 
 */
public class FtpUtils {

	private static final Logger logger = Logger.getLogger(FtpUtils.class);
	/**
	 * @param ip
	 * @param port
	 * @param userName
	 * @param userPwd
	 * @param path
	 * @throws SocketException
	 * @throws IOException
	 *             function:连接到服务器
	 */
	public static FTPClient connectServer(String ip, int port, String userName,
			String userPwd, String path) {
		FTPClient ftpClient = new FTPClient();
		try {
			// 连接
			ftpClient.connect(ip, port);
			// 登录
			ftpClient.login(userName, userPwd);
			ftpClient.enterLocalPassiveMode();
			if (StringUtils.isNotEmpty(path)) {
				// 跳转到指定目录
				ftpClient.changeWorkingDirectory(path);
			}
		} catch (SocketException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		logger.info("--------------ftp连接成功--------------");
		return ftpClient;
	}

	/**
	 * @throws IOException
	 *             function:关闭连接
	 */
	public static void closeServer(FTPClient ftpClient) {
		if (ftpClient.isConnected()) {
			try {
				ftpClient.logout();
				ftpClient.disconnect();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * @param path
	 * @return function:读取指定目录下的文件名
	 * @throws IOException
	 */
	public static List<String> getfilePathList(FTPClient ftpClient,String path) {
		List<String> fileNames = new ArrayList<String>();
		// 获得指定目录下所有文件名
		FTPFile[] ftpFiles = null;
		System.out.println(ftpClient);
		try {
			logger.info("--------下载文件的路径: "+path);
			ftpFiles = ftpClient.listFiles(path);
			logger.info("--------开始读取文件: " + ftpFiles);
		} catch (IOException e) {
			e.printStackTrace();
		}
		logger.info("---------文件个数: " + ftpFiles.length);
		for (int i = 0; ftpFiles != null && i < ftpFiles.length; i++) {
			FTPFile file = ftpFiles[i];
			if (file.isFile()) {
				logger.info("---------文件名称: " + file.getName());
				if (file.getName().endsWith(".tsv.zip")) {
					fileNames.add(file.getName());
				}
			}
		}
		return fileNames;
	}

	/**
	 * @Title: readFile   
	 * @Description: 从服务器上读取指定的文件 
	 * @param: @param ftpClient
	 * @param: @param fileName ftp上的文件名
	 * @param: @param filePath 本地地址
	 * @param: @return
	 * @param: @throws ParseException      
	 * @return: String      
	 * @throws
	 */
	public static boolean readFile(FTPClient ftpClient,String ftpFileName,String localFilePath) throws ParseException {
		boolean flag = false;
		InputStream ins = null;
		try {
			// 从服务器上读取指定的文件
			ins = ftpClient.retrieveFileStream(ftpFileName);
			File localFile = new File(localFilePath + ftpFileName);
			if(!localFile.exists()){
				localFile.getParentFile().mkdirs();
			}
			FileOutputStream fos=new FileOutputStream(localFile);
			byte[] buffer=new byte[10240];
            int size=0;
            while((size=ins.read(buffer))!=-1){
                fos.write(buffer, 0, size);
            }
            ins.close();
            fos.close();
			ftpClient.getReply();
			return true;
		} catch (IOException e) {
			e.printStackTrace();
			logger.info("------下载文件失败--------"+e);
		}
		return flag;
	}

	/**
	 * @param filePath
	 *            function:删除文件
	 */
	public static void deleteFile(FTPClient ftpClient,String filePath) {
		try {
			ftpClient.deleteFile(filePath);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param args
	 * @throws ParseException
	 */
	public static void main(String[] args) throws ParseException {
		//01-cri-albanian_20171113-150000.tsv.gz
//		FtpUtils ftp = new FtpUtils();
//		FTPClient ftpClient = ftp.connectServer("ftp4.omniture.com", 21, "cridf","P.lt1Opn", "/");
//		ftp.getfilePathList(ftpClient, "/");
//		ftp.readFile(ftpClient,"01-cri-albanian_20171113-150000.tsv.gz","D://01-cri-albanian_20171113-150000.tsv.gz");
//		ftp.closeServer(ftpClient);
		System.out.println("成功");
	}
}

你可能感兴趣的:(Java,工具类,java,ftp)