使用Java方式ftp下载

本文介绍了如何使用Java的commons-net库进行FTP文件下载。首先,通过Maven引入该库的最新版本3.6。接着,展示了具体的Java源码实现。最后,提供了调用方法的说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


1、引入maven配置(commons-net,当前版本最新3.6);

        <dependency>
            <groupId>commons-net</groupId>
            <artifactId>commons-net</artifactId>
            <version>3.6</version>
        </dependency>

2、下面是源码:


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

/**
 * Ftp访问 <br/>
 * Created by claireliu on 2017/5/23.
 */
public class FtpFunction {

	private static Logger log = Logger.getLogger(FtpFunction.class);

	static FTPClient ftp;

	public static boolean getConnect() {
		boolean result = false;
		try {
			int port = 21;
			String address = "127.0.0.1";
			String userName = "bbb";
			String userPassword = "ccc";
			ftp = new FTPClient();
			ftp.connect(address, port);
			ftp.login(userName, userPassword);
			ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
			ftp.setControlEncoding("GBK");
			// 调用FTPClient.enterLocalPassiveMode();这个方法的意思就是每次数据连接之前,ftp
			// client告诉ftp server开通一个端口来传输数据。为什么要这样做呢,因为ftp
			// server可能每次开启不同的端口来传输数据,但是在linux上,由于安全限制,可能某些端口没有开启,所以就出现阻塞(会出现下载不了的情况或者listfiles为空。)。
			ftp.enterLocalPassiveMode();
			int reply = ftp.getReplyCode();
			if (!FTPReply.isPositiveCompletion(reply)) {
				ftp.disconnect();
				return result;
			}
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
			log.error("op<getConnect> getConnect fail", e);
		}
		return result;
	}

	 
	/**
	 * 下载
	 * @param remotePath ftp服务器上的路径
	 * @param fileName 文件名
	 * @param localPath 本地的存储路径。
	 * @return 成功与否
	 */
	public static boolean downFileSimpleFile(String remotePath, String fileName, String localPath) {
		boolean success = false;
		OutputStream is = null;
		try {
			getConnect();
			String remoteFileName =  remotePath + File.separator + fileName;
			File localFile = new File(localPath + File.separator + fileName);
			is = new FileOutputStream(localFile);

			ftp.setBufferSize(1024);
			ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
			ftp.enterLocalPassiveMode();
			ftp.retrieveFile(remoteFileName, is);

			success = true;
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			close(is);
		}
		return success;
	}

	private static void close(OutputStream is) {
		try {
            ftp.logout();
        } catch (IOException e) {
            e.printStackTrace();
        }

		if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

		if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException ioe) {
            }
        }
	}



}


3、调用:


FtpFunction.downFileSimpleFile("logFile", "file_20170515112505.txt", "/opt/");


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值