FTP上传下载

package cn.tisson.icsp.utils;java


import java.io.File;apache

import java.io.FileOutputStream;服务器

import java.io.IOException;测试

import java.io.InputStream;url

import java.io.OutputStream;.net


import org.apache.commons.net.ftp.FTP;server

import org.apache.commons.net.ftp.FTPClient;ip

import org.apache.commons.net.ftp.FTPFile;rem

import org.apache.commons.net.ftp.FTPReply;get


public class FtpApche {

    private static FTPClient ftpClient = new FTPClient();

    private static String encoding = System.getProperty("file.encoding");


    /**

     * Description: 向FTP服务器上传文件

     *

     * @param url      FTP服务器hostname

     * @param port     FTP服务器端口

     * @param username FTP登陆帐号

     * @param password FTP登陆密码

     * @param path     FTP服务器保存目录,若是是根目录则为“/”

     * @param filename 上传到FTP服务器上的文件名

     * @param input    本地文件输入流

     * @return 成功返回true,不然返回false

     * @Version1.0

     */

    public static boolean uploadFile(String url, int port, String username,

                                     String password, String path, String filename, InputStream input) {

        boolean result = false;


        try {

            int reply;

            // 若是采用默认端口,能够使用ftp.connect(url)的方式直接链接FTP服务器

            ftpClient.connect(url);

            // ftp.connect(url, port);// 链接FTP服务器

            // 登陆

            ftpClient.login(username, password);

            ftpClient.setControlEncoding(encoding);

            // 检验是否链接成功

            reply = ftpClient.getReplyCode();

            if (!FTPReply.isPositiveCompletion(reply)) {

                System.out.println("链接失败");

                ftpClient.disconnect();

                return result;

            }


            // 转移工做目录至指定目录下

            boolean change = ftpClient.changeWorkingDirectory(path);

            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

            if (change) {

                result = ftpClient.storeFile(new String(filename.getBytes(encoding), "iso-8859-1"), input);

                if (result) {

                    System.out.println("上传成功!");

                }

            }

            input.close();

            ftpClient.logout();

        } catch (IOException e) {

            e.printStackTrace();

        } finally {

            if (ftpClient.isConnected()) {

                try {

                    ftpClient.disconnect();

                } catch (IOException ioe) {

                }

            }

        }

        return result;

    }


    /**

     * Description: 从FTP服务器下载文件

     *

     * @param url        FTP服务器hostname

     * @param port       FTP服务器端口

     * @param username   FTP登陆帐号

     * @param password   FTP登陆密码

     * @param remotePath FTP服务器上的相对路径

     * @param fileName   要下载的文件名

     * @param localPath  下载后保存到本地的路径

     * @return

     * @Version1.0

     */

    public static boolean downFile(String url, int port, String username,

                                   String password, String remotePath, String fileName,

                                   String localPath) {

        boolean result = false;

        try {

            int reply;

            ftpClient.setControlEncoding(encoding);


            /*

             *  为了上传和下载中文文件,有些地方建议使用如下两句代替

             *  new String(remotePath.getBytes(encoding),"iso-8859-1")转码。

             *  通过测试,通不过。

             */

//            FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);

//            conf.setServerLanguageCode("zh");


            ftpClient.connect(url, port);

            // 若是采用默认端口,能够使用ftp.connect(url)的方式直接链接FTP服务器

            ftpClient.login(username, password);// 登陆

            // 设置文件传输类型为二进制

            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);

            // 获取ftp登陆应答代码

            reply = ftpClient.getReplyCode();

            // 验证是否登录成功

            if (!FTPReply.isPositiveCompletion(reply)) {

                ftpClient.disconnect();

                System.err.println("FTP server refused connection.");

                return result;

            }

            // 转移到FTP服务器目录至指定的目录下

            ftpClient.changeWorkingDirectory(new String(remotePath.getBytes(encoding), "iso-8859-1"));

            // 获取文件列表

            FTPFile[] fs = ftpClient.listFiles();

            for (FTPFile ff : fs) {

                if (ff.getName().equals(fileName)) {

                    File localFile = new File(localPath + "/" + ff.getName());

                    OutputStream is = new FileOutputStream(localFile);

                    ftpClient.retrieveFile(ff.getName(), is);

                    is.close();

                }

            }


            ftpClient.logout();

            result = true;

        } catch (IOException e) {

            e.printStackTrace();

        } finally {

            if (ftpClient.isConnected()) {

                try {

                    ftpClient.disconnect();

                } catch (IOException ioe) {

                }

            }

        }

        return result;

    }

}

相关文章
相关标签/搜索