Java使用Apache Commons Net实现FTP功能

maven依赖:

<!-- https://mvnrepository.com/artifact/commons-net/commons-net -->
<dependency>
    <groupId>commons-net</groupId>
    <artifactId>commons-net</artifactId>
    <version>3.6</version>
</dependency>

示例代码:

package com.zifeiy.test.normal;

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

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

public class NormalTest20181224FTPClient {
    
    public static String ftpUrl = "xxx.xxx.xxx.xxx";
    public static String ftpUsername = "ftp_username";
    public static String ftpPassword = "ftp_password";
    
    public static void main(String[] args) {
        FTPClient ftp = new FTPClient();
        FTPClientConfig config = new FTPClientConfig();
        ftp.configure(config);
        try {
            int reply;
            ftp.connect(ftpUrl);
            System.out.println("Connected to " + ftpUrl + ".");
            System.out.print(ftp.getReplyString());
            reply = ftp.getReplyCode();

            if(!FTPReply.isPositiveCompletion(reply)) {
              ftp.disconnect();
              System.err.println("FTP server refused connection.");
              System.exit(1);
            }
            
            ftp.logout();
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        
    }
    
}

参考链接:http://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/ftp/FTPClient.html

转载于:https://www.cnblogs.com/zifeiy/p/10169110.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值