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;

    }

}

相關文章
相關標籤/搜索