/**
 * 
 */
package org.springblade.etl.util;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;

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

import cn.hutool.core.io.IoUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author wookvn
 *
 */
@Slf4j
public class FtpUtil {

	FTPClient ftpClient = null;
	
	public void login(String username, String password, String host, int port) throws SocketException, IOException {
		ftpClient= new FTPClient();
        ftpClient.connect(host, port);
        ftpClient.login(username, password);
        ftpClient.enterLocalPassiveMode();
	}
	
	/**
	 * 读取文件
	 * @param filePath
	 * @return
	 * @throws IOException
	 */
	public InputStream read(String filePath) throws IOException {
//		ByteArrayInputStream bais = new ByteArrayInputStream(buf)
		return new ByteArrayInputStream(IoUtil.readBytes(ftpClient.retrieveFileStream(filePath)));
	}
	
	/**
	 * 
	 * @throws IOException
	 */
	public void logout() {
		if(ftpClient != null) {
			try {
				ftpClient.disconnect();
			} catch (IOException e) {
				log.error(e.getMessage(), e);
			}
		}
	}
}
