Commons Net 2.0 API 之ftpClient类

ftpClient类囊括了Java对于ftp服务器上的一系列操作。

ftpClient类继承自ftp类,增加了上传文件流、下载文件流、获得目录名称和文件名称等一系列操作。

创建ftpClient对象

ftpClient = new FTPClient();

连接ftpClient对象,为该对象添加一个connect方法

public boolean connect() {
        boolean isConnectionSuccess = false;
        if (ftpClient == null) {
            ftpClient = new FTPClient();
        }
        try {
            ftpClient.connect(host, port);
            int reply = ftpClient.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                return false;
            }
            isConnectionSuccess = ftpClient.login(userName, password);
            if (!isConnectionSuccess) {
                return false;
            }
            if (!ftpClient.setFileType(FTP.BINARY_FILE_TYPE)) {
                return false;
            }
            if (workingDirectory != null) {
                ftpClient.makeDirectory(workingDirectory);
                if (!ftpClient.changeWorkingDirectory(workingDirectory)) {
                    return false;
                }
            }
            ftpClient.enterLocalPassiveMode();
        } catch (IOException e) {
            if (ftpClient != null && ftpClient.isConnected())
                try {
                    ftpClient.disconnect();
                } catch (IOException e1) {
                    return false;
                }
        }
        return isConnectionSuccess;
    }


关闭连接

public void disConnect() {
        if (ftpClient != null && ftpClient.isConnected())
            try {
                ftpClient.disconnect();
            } catch (IOException e1) {
                logger
                        .error("[error]--disConnect exception:"
                                + e1.getMessage());
            }
    }

上传至ftp方法 ftpclient.appendFile(string filename,fileinputstream fileinputstream)

public boolean uploadFile(File file) {
        boolean isUpload = false;
        InputStream in = null;
        if (file != null) {
            try {
                in = new FileInputStream(file);
                isUpload = ftpClient.appendFile(file.getName(), in);
            } catch (IOException e) {
                return false;
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        return false;
                    }
                }
            }
        } else {
            return false;
        }
        return isUpload;
    }

从ftp下载文件

public boolean downloadFile(String remoteFile,String localFile) throws IOException
    {
        boolean flag=false;
        File outfile=new File(localFile);
        FileOutputStream out=null;
        if(!outfile.exists())
        {
            String[] names = ftpClient.listNames(remoteFile);
            if(names.length!=0)
            {
                try
                {            
                    out=new FileOutputStream(outfile);             
                    flag=ftpClient.retrieveFile(remoteFile, out);
                }
                catch (IOException e) {
                    flag = false;
                    return flag;
                    } finally {
                        try {
                            if (out != null)
                            {
                                out.close();
                                flag=true;
                                logger.info("Download successfully...");
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        finally
                        {
                            return flag;
                        }
                    }
            }
            else
            {
                logger.error("File not found from the FTP...");
                flag=false;
                return flag;
            }
        }
        else
        {
            logger.error("The localFile exists,please renamed the localFile!");
            flag=false;
            return flag;
        }
    }


请注意ftpclient 的listFiles()和listNames()的区别:

1.listFiles()可以列出目录下所有的文件 返回对象为ftpFile类的数组ftpFile[]。

2.重载:同时在listFiles(string filename)增加参数可以用于查询 当前目录下所有名称包含"filename"的文件,返回同样是ftpFile[]。

3.技巧:可以根据ftpFile[]数组的length来确认文件是否存在。

4.区别:listFiles()和listNames()的区别在于listFiles()返回文件数组ftpFile[],而listNames()返回文件名称数组string[]。

你可能感兴趣的:(.net)