apache的ftpclient的使用

网上搜了一些关于apache的ftpclient的使用范式
import java.io.File;  
import java.io.FileInputStream;  
import java.io.FileOutputStream;  
import java.io.IOException;  
import java.io.InputStream;  
import java.io.OutputStream;  
import java.net.SocketException;  
import java.util.ArrayList;  
import java.util.Date;  
import java.util.List;  
import org.apache.commons.net.ftp.FTP;  
import org.apache.commons.net.ftp.FTPClient;  
import org.apache.commons.net.ftp.FTPFile;  
  
public class FtpUtil {  
    private FTPClient ftpClient;  
    public static final int BINARY_FILE_TYPE = FTP.BINARY_FILE_TYPE;  
    public static final int ASCII_FILE_TYPE = FTP.ASCII_FILE_TYPE;  
      
    // path should not the path from root index  
    // or some FTP server would go to root as '/'.  
    public void connectServer(FtpConfig ftpConfig) throws SocketException,  
            IOException {  
        String server = ftpConfig.getServer();  
        int port = ftpConfig.getPort();  
        String user = ftpConfig.getUsername();  
        String password = ftpConfig.getPassword();  
        String location = ftpConfig.getLocation();  
        connectServer(server, port, user, password, location);  
    }  
      
      
    public void connectServer(String server, int port, String user,  
            String password, String path) throws SocketException, IOException {  
        ftpClient = new FTPClient();  
        ftpClient.connect(server, port);  
        System.out.println("Connected to " + server + ".");  
        System.out.println(ftpClient.getReplyCode());  
        ftpClient.login(user, password);  
        // Path is the sub-path of the FTP path  
        if (path.length() != 0) {  
            ftpClient.changeWorkingDirectory(path);  
        }  
    }  
    //FTP.BINARY_FILE_TYPE | FTP.ASCII_FILE_TYPE  
    // Set transform type  
    public void setFileType(int fileType) throws IOException {  
        ftpClient.setFileType(fileType);  
    }  
  
    public void closeServer() throws IOException {  
        if (ftpClient.isConnected()) {  
            ftpClient.disconnect();  
        }  
    }  
    //=======================================================================  
    //==         About directory       =====  
    // The following method using relative path better.  
    //=======================================================================  
      
    public boolean changeDirectory(String path) throws IOException {  
        return ftpClient.changeWorkingDirectory(path);  
    }  
    public boolean createDirectory(String pathName) throws IOException {  
        return ftpClient.makeDirectory(pathName);  
    }  
    public boolean removeDirectory(String path) throws IOException {  
        return ftpClient.removeDirectory(path);  
    }  
      
    // delete all subDirectory and files.  
    public boolean removeDirectory(String path, boolean isAll)  
            throws IOException {  
          
        if (!isAll) {  
            return removeDirectory(path);  
        }  
  
        FTPFile[] ftpFileArr = ftpClient.listFiles(path);  
        if (ftpFileArr == null || ftpFileArr.length == 0) {  
            return removeDirectory(path);  
        }  
        //   
        for (FTPFile ftpFile : ftpFileArr) {  
            String name = ftpFile.getName();  
            if (ftpFile.isDirectory()) {  
System.out.println("* [sD]Delete subPath ["+path + "/" + name+"]");               
                removeDirectory(path + "/" + name, true);  
            } else if (ftpFile.isFile()) {  
System.out.println("* [sF]Delete file ["+path + "/" + name+"]");                          
                deleteFile(path + "/" + name);  
            } else if (ftpFile.isSymbolicLink()) {  
  
            } else if (ftpFile.isUnknown()) {  
  
            }  
        }  
        return ftpClient.removeDirectory(path);  
    }  
      
    // Check the path is exist; exist return true, else false.  
    public boolean existDirectory(String path) throws IOException {  
        boolean flag = false;  
        FTPFile[] ftpFileArr = ftpClient.listFiles(path);  
        for (FTPFile ftpFile : ftpFileArr) {  
            if (ftpFile.isDirectory()  
                    && ftpFile.getName().equalsIgnoreCase(path)) {  
                flag = true;  
                break;  
            }  
        }  
        return flag;  
    }  
  
    //=======================================================================  
    //==         About file        =====  
    // Download and Upload file using  
    // ftpUtil.setFileType(FtpUtil.BINARY_FILE_TYPE) better!  
    //=======================================================================  
  
    // #1. list & delete operation  
    // Not contains directory  
    public List<String> getFileList(String path) throws IOException {  
        // listFiles return contains directory and file, it's FTPFile instance  
        // listNames() contains directory, so using following to filer directory.  
        //String[] fileNameArr = ftpClient.listNames(path);  
        FTPFile[] ftpFiles= ftpClient.listFiles(path);  
          
        List<String> retList = new ArrayList<String>();  
        if (ftpFiles == null || ftpFiles.length == 0) {  
            return retList;  
        }  
        for (FTPFile ftpFile : ftpFiles) {  
            if (ftpFile.isFile()) {  
                retList.add(ftpFile.getName());  
            }  
        }  
        return retList;  
    }  
  
    public boolean deleteFile(String pathName) throws IOException {  
        return ftpClient.deleteFile(pathName);  
    }  
  
    // #2. upload to ftp server  
    // InputStream <------> byte[]  simple and See API  
  
    public boolean uploadFile(String fileName, String newName)  
            throws IOException {  
        boolean flag = false;  
        InputStream iStream = null;  
        try {  
            iStream = new FileInputStream(fileName);  
            flag = ftpClient.storeFile(newName, iStream);  
        } catch (IOException e) {  
            flag = false;  
            return flag;  
        } finally {  
            if (iStream != null) {  
                iStream.close();  
            }  
        }  
        return flag;  
    }  
  
    public boolean uploadFile(String fileName) throws IOException {  
        return uploadFile(fileName, fileName);  
    }  
  
    public boolean uploadFile(InputStream iStream, String newName)  
            throws IOException {  
        boolean flag = false;  
        try {  
            // can execute [OutputStream storeFileStream(String remote)]  
            // Above method return's value is the local file stream.  
            flag = ftpClient.storeFile(newName, iStream);  
        } catch (IOException e) {  
            flag = false;  
            return flag;  
        } finally {  
            if (iStream != null) {  
                iStream.close();  
            }  
        }  
        return flag;  
    }  
  
    // #3. Down load   
  
    public boolean download(String remoteFileName, String localFileName)  
            throws IOException {  
        boolean flag = false;  
        File outfile = new File(localFileName);  
        OutputStream oStream = null;  
        try {  
            oStream = new FileOutputStream(outfile);  
            flag = ftpClient.retrieveFile(remoteFileName, oStream);  
        } catch (IOException e) {  
            flag = false;  
            return flag;  
        } finally {  
            oStream.close();  
        }  
        return flag;  
    }  
      
    public InputStream downFile(String sourceFileName) throws IOException {  
        return ftpClient.retrieveFileStream(sourceFileName);  
    }  


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Apache FTPClient是一个开源的Java FTP客户端库,它可以用来连接FTP服务器并执行文件传输操作。Apache FTPClient连接FTP服务器的原理如下: 1. 创建FTPClient对象:首先,需要创建一个FTPClient对象来连接FTP服务器。FTPClientApache FTPClient库提供的一个类,它提供了各种方法来连接FTP服务器、传输文件等。创建FTPClient对象的代码如下: ``` FTPClient ftpClient = new FTPClient(); ``` 2. 连接FTP服务器:FTPClient对象创建后,需要使用connect方法连接FTP服务器。connect方法需要传入FTP服务器的地址和端口号。连接FTP服务器的代码如下: ``` ftpClient.connect(serverAddress, port); ``` 3. 登录FTP服务器:连接FTP服务器后,需要使用login方法登录FTP服务器。login方法需要传入FTP服务器的用户名和密码。登录FTP服务器的代码如下: ``` boolean success = ftpClient.login(username, password); ``` 4. 执行FTP操作:登录FTP服务器成功后,可以使用FTPClient对象提供的各种方法来执行FTP操作,比如上传文件、下载文件、删除文件等。FTP操作的代码如下: ``` ftpClient.changeWorkingDirectory(remoteDirectory); InputStream inputStream = new FileInputStream(localFile); ftpClient.storeFile(remoteFile, inputStream); inputStream.close(); ``` 5. 断开FTP连接:FTP操作完成后,需要使用disconnect方法断开FTP连接。断开FTP连接的代码如下: ``` ftpClient.disconnect(); ``` 以上就是Apache FTPClient连接FTP服务器的原理。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值