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("成功");
	}
}

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Karma's a Bitch

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值