FTP組件edtFTPj

來源:互聯網
上載者:User

  edtFTPj是一個非常強大的FTP組件,有Java版本、.NET版本、JavaScript版本。

  Java版本的有收費的edtFTPj/PRO,還有免費開源的edtFTPj/Free。

  這裡使用edtFTPj/Free。

  edtFTPj/Free提供了一套穩定、功能強大又便於使用的類庫,這讓使用FTP協議傳輸檔案變得非常簡單。

  edtFTPj/Free有以下功能:

  1)通過FTP協議快速穩定的檔案傳輸

  2)具有主動(Active)模式和被動(Passive)模式,支援二進位(Binary)傳輸和ASCII傳輸

  3)支援中斷後恢複傳輸

  4)支援以FTP流的方式靈活地從FTP伺服器讀取和寫入

  edtFTPj/Free的 http://www.enterprisedt.com/products/edtftpj/download.html

  下載解壓後把edtftpj.jar添加到classpath路徑下。

  1、寫一個FTP工具類,擷取FTP用戶端。

package cn.luxh.utils;import java.io.IOException;import com.enterprisedt.net.ftp.FTPConnectMode;import com.enterprisedt.net.ftp.FTPException;import com.enterprisedt.net.ftp.FTPTransferType;import com.enterprisedt.net.ftp.FileTransferClient;/** * FTP Util * @author Luxh */public class FtpUtil {        /**     *      * @param host            FTP伺服器位址     * @param userName     FTP帳號     * @param password    FTP密碼     * @return     * @throws FTPException     * @throws IOException     */    public static FileTransferClient getFileTransferClient(String host,String userName,String password) throws FTPException, IOException {                //建立一個FTP client        //官方已不推薦使用FTPClient        FileTransferClient ftp = new FileTransferClient();                //設定FTP伺服器        ftp.setRemoteHost(host);        //設定FTP使用者名稱        ftp.setUserName(userName);        //設定FTP密碼        ftp.setPassword(password);                //解決中文檔案名稱亂碼        ftp.getAdvancedSettings().setControlEncoding("GBK");                //被動模式,資料連線由用戶端發起        ftp.getAdvancedFTPSettings().setConnectMode(FTPConnectMode.PASV);                //用HTML 和文本編寫的檔案必須用ASCII模式上傳,用BINARY模式上傳會破壞檔案,導致檔案執行出錯.        //ftp.setContentType(FTPTransferType.ASCII);                //BINARY模式用來傳送可執行檔,壓縮檔,和圖片檔案.        ftp.setContentType(FTPTransferType.BINARY);                //串連到FTP伺服器        ftp.connect();                return ftp;    }        /**     * 從FTP Server中斷連線     * @param ftp     * @throws FTPException     * @throws IOException     */    public static void disconnectFromFTPServer(FileTransferClient ftp) throws FTPException, IOException {        if(ftp != null && ftp.isConnected()) {            ftp.disconnect();        }    }}

 

  2、簡單測試一下上傳檔案、下載檔案、刪除檔案。

package cn.luxh.test;import java.io.IOException;import org.junit.Test;import com.enterprisedt.net.ftp.FTPException;import com.enterprisedt.net.ftp.FileTransferClient;import cn.luxh.utils.FtpUtil;public class FtpUtilTest {        /**FTP伺服器*/    private static final String FTP_HOST = "127.0.0.1";    /**FTP帳號*/    private static final String FTP_USERNAME = "root";    /**FTP密碼*/    private static final String FTP_PASSWORD = "root";        /**     * 上傳檔案     * @throws IOException      * @throws FTPException      */    @Test    public void testUploadFile() throws FTPException, IOException {        FileTransferClient ftp = FtpUtil.getFileTransferClient(FTP_HOST,FTP_USERNAME,FTP_PASSWORD);        String localFileName = "d:/test/中文.txt";        String remoteFileName = "/test/中文.txt";        ftp.uploadFile(localFileName, remoteFileName);        FtpUtil.disconnectFromFTPServer(ftp);    }        /**     * 下載檔案     * @throws FTPException     * @throws IOException     */    @Test    public void testDownloadFile() throws FTPException, IOException {        FileTransferClient ftp = FtpUtil.getFileTransferClient(FTP_HOST,FTP_USERNAME,FTP_PASSWORD);        ftp.downloadFile("D:/test/中文.txt", "/test/中文.txt");        FtpUtil.disconnectFromFTPServer(ftp);    }        /**     * 刪除檔案     * @throws FTPException     * @throws IOException     */    @Test    public void testDeleteFile() throws FTPException, IOException {        FileTransferClient ftp = FtpUtil.getFileTransferClient(FTP_HOST,FTP_USERNAME,FTP_PASSWORD);        ftp.deleteFile("/test/中文.txt");        FtpUtil.disconnectFromFTPServer(ftp);    }}

 

 

 

 

 

  

  

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.