利用commons-net.jar下载ftp文件到本地或hdfs

 public class SimpleFTPClient {       
        private FTPClient ftpClient = new FTPClient();
	private Logger logger = Logger.getLogger(SimpleFTPClient.class);
	private Properties prop = new Properties();

	public SimpleFTPClient() {
		// 设置将过程中使用到的命令输出到控制台
		try {
			logger.info("new SimpleFTPClient check log in logs/ftp.log");
			String path = new File(".").getAbsolutePath();
			path = path.substring(0, path.length() - 1) + "logs/ftp.log";
			OutputStream os = new FileOutputStream(path, true);
			ftpClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(os)));
			prop.load(SimpleFTPClient.class.getResourceAsStream("ftp.properties"));
			System.out.println(prop);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public boolean connect() throws IOException {
		ftpClient.connect(prop.getProperty("hostname"), Integer.valueOf(prop.getProperty("port")));
		if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
			if (ftpClient.login(prop.getProperty("username"), prop.getProperty("password"))) {
				ftpClient.enterLocalPassiveMode();
				ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
				return true;
			}
		}
		disconnect();
		return false;
	}
	public boolean downloadToLocal(String ftpFileName, String localFileName) throws IOException {
		boolean ret = false;
		File localFile = new File(localFileName);
		FTPFile ftpFile = ftpClient.listFiles(ftpFileName)[0];
		long ftpFileSize = ftpFile.getSize();
		long localFileSize = 0;
		if (!localFile.exists() || (localFileSize = localFile.length()) == 0) {
			logger.info("start new downloading:" + ftpFileName);
			OutputStream os = new FileOutputStream(localFile);
			ret = ftpClient.retrieveFile(ftpFileName, os);
			os.close();
		} else if (localFileSize >= ftpFileSize) {
			logger.info("already exist:" + ftpFileName);
			ret = true;
		} else {
			logger.info("continue downloading:" + ftpFileName + ",position:" + localFileSize);
			OutputStream os = new FileOutputStream(localFile, true);
			ftpClient.setRestartOffset(localFileSize);
			ret = ftpClient.retrieveFile(ftpFileName, os);
			os.close();
		}
		logger.info("download finishd:" + ftpFileName);
		return ret;

	}
	public boolean downloadToHdfs(String ftpFileName, String hdfsFileName, FileSystem fs) throws IOException {

		boolean ret = false;
		//		Configuration conf = new Configuration();
		//		FileSystem fs = FileSystem.get(conf);
		Path hdfsPath = new Path(hdfsFileName);
		FTPFile ftpFile = ftpClient.listFiles(ftpFileName)[0];
		long ftpFileSize = ftpFile.getSize();
		long hdfsFileSize = 0;

		if (!fs.exists(hdfsPath) || (hdfsFileSize = fs.getFileStatus(hdfsPath).getLen()) == 0) {
			logger.info("start new downloading:" + ftpFileName);
			FSDataOutputStream fsdos = fs.create(hdfsPath);
			ret = ftpClient.retrieveFile(ftpFileName, fsdos);
			fsdos.close();
		} else if (hdfsFileSize >= ftpFileSize) {
			logger.info("already exist:" + ftpFileName);
			ret = true;
		} else {
			logger.info("continue downloading:" + ftpFileName + ",position:" + hdfsFileSize);
			FSDataOutputStream fsdos = fs.append(hdfsPath);
			ftpClient.setRestartOffset(hdfsFileSize);
			ret = ftpClient.retrieveFile(ftpFileName, fsdos);
			fsdos.close();
		}
		logger.info("download finishd:" + ftpFileName);
		return ret;

	}
	public void disconnect() throws IOException {
		try {
			ftpClient.logout();
		} catch (IOException e) {
			throw e;
		} finally {
			if (ftpClient.isConnected()) {
				ftpClient.disconnect();
			}
		}
	}
}	


你可能感兴趣的:(java,ftp,FTPClient,ftp下载)