FTP处理类

本文档详细介绍了如何使用FTP进行文件下载和上传的步骤,涵盖了FTP在服务器管理中的基本应用,尤其在Linux和Apache环境下的实践操作。
摘要由CSDN通过智能技术生成
package **.tool;

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

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

public class FTPHelper {
   

    private FTPClient ftp = null;

    private String server;

    private String uname;

    private String password;

    private int port = 21;

    public FTPHelper(String server, int port, String uname, String password) {
   
        this.server = server;
        if (this.port > 0) {
   
            this.port = port;
        }
        this.uname = uname;
        this.password = password;

        this.ftp = new FTPClient();
    }

    public FTPClient connectFTPServer() throws Exception {
   
        try {
   
            this.ftp.setControlEncoding("GBK");
            this.ftp.setAutodetectUTF8(true);
            this.ftp.connect(this.server, this.port);
            if (!this.ftp.login(this.uname, this.password)) {
   
                this.ftp.logout();
                this.ftp = null;
                return this.ftp;
            }
            this.ftp.setFileType(2);
            this.ftp.enterLocalPassiveMode();
            this.ftp.setConnectTimeout(10000);
            this.ftp.setBufferSize(1024);
            int replyCode = this.ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(replyCode)) {
   
                closeFTPClient();
                this.ftp = null;
                throw new Exception( this.server + " - "+ this.uname + " - " + this.password);
            }
            return this.ftp;
        }catch (Exception e) {
   
            this.ftp.disconnect();
            this.ftp = null;
            throw e;
        }
    }

    public FTPClientConfig getFTPClientConfig() throws Exception {
   
        String systemKey = "UNIX";
        String serverLanguageCode = "zh";
        FTPClientConfig conf = new FTPClientConfig(systemKey);
        conf.setServerLanguageCode(serverLanguageCode);
        conf.setDefaultDateFormatStr("yyyy-MM-dd");
        return conf;
    }

    public Boolean uploadFile(String localFile, String newName) throws Exception {
   
        InputStream input = null;
        boolean success = false;
        try {
   
            File file = null;
            if (checkFileExist(localFile)) {
   
                file = new File(localFile);
            }
            input = new FileInputStream(file);
            success = this.ftp.storeFile(newName, input);
            if (!success) {
   
                throw new Exception("");
            }
        } catch (Exception e) {
   
            throw e;
        } finally {
   
            if (input != null) {
   
                input.close();
            }
        }
        return Boolean.valueOf(success);
    }

    public Boolean uploadFile(InputStream input, String newName) throws Exception {
   
        boolean success = false;
        try {
   
            success = this.ftp.storeFile(newName, input);
            if (!success) {
   
                throw new Exception("");
            }
        } catch (Exception e) {
   
            e.printStackTrace();
            throw e;
        } finally {
   
            if (input != null) {
   
                input.close();
            }
        }
        return Boolean.valueOf(success);
    }

    public Boolean uploadFile(String localFile, String newName, String remoteFoldPath) throws Exception {
   
        InputStream input = null;
        boolean success = false;
        try {
   
            File file = null;
            if (checkFileExist(localFile)) {
   
                file = new File(localFile);
            }
            input = new FileInputStream(file);
            if (!changeDirectory(remoteFoldPath).booleanValue()) {
   
                System.out.println("");
                return Boolean.valueOf(false);
            }
            success = this.ftp.storeFile(newName, input);
            if (!success) {
   
                throw new Exception("");
            }
        } catch (Exception e) {
   
            throw e;
        } finally {
   
            if (input != null) {
   
                input.close();
            }
        }
        return Boolean.valueOf(success);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值