當前位置: 首頁>>代碼示例>>Java>>正文


Java FTPClient.connect方法代碼示例

本文整理匯總了Java中org.apache.commons.net.ftp.FTPClient.connect方法的典型用法代碼示例。如果您正苦於以下問題:Java FTPClient.connect方法的具體用法?Java FTPClient.connect怎麽用?Java FTPClient.connect使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.apache.commons.net.ftp.FTPClient的用法示例。


在下文中一共展示了FTPClient.connect方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: download

import org.apache.commons.net.ftp.FTPClient; //導入方法依賴的package包/類
public static byte[] download(String url, int port, String username, String password, String remotePath,
		String fileName) throws IOException {
	FTPClient ftp = new FTPClient();
	ftp.setConnectTimeout(5000);
	ftp.setAutodetectUTF8(true);
	ftp.setCharset(CharsetUtil.UTF_8);
	ftp.setControlEncoding(CharsetUtil.UTF_8.name());
	try {
		ftp.connect(url, port);
		ftp.login(username, password);// 登錄
		if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
			ftp.disconnect();
			throw new IOException("login fail!");
		}
		ftp.changeWorkingDirectory(remotePath);
		ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
		FTPFile[] fs = ftp.listFiles();
		for (FTPFile ff : fs) {
			if (ff.getName().equals(fileName)) {
				try (ByteArrayOutputStream is = new ByteArrayOutputStream();) {
					ftp.retrieveFile(ff.getName(), is);
					byte[] result = is.toByteArray();
					return result;
				}
			}
		}

		ftp.logout();
	} finally {
		if (ftp.isConnected()) {
			ftp.disconnect();
		}
	}
	return null;
}
 
開發者ID:HankXV,項目名稱:Limitart,代碼行數:36,代碼來源:FTPUtil.java

示例2: initFtpClient

import org.apache.commons.net.ftp.FTPClient; //導入方法依賴的package包/類
private FTPClient initFtpClient(String remoteDir) throws IOException {
    FTPClient ftp = new FTPClient();
    // 設置連接超時時間
    ftp.setConnectTimeout(CONNECT_TIMEOUT);
    // 設置傳輸文件名編碼方式
    ftp.setControlEncoding(CONTROL_ENCODING);
    ftp.connect(host, ftpPort);
    int reply = ftp.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply)) {
        logger.debug("無法連接FTP");
        return null;
    }
    boolean loginRet = ftp.login(ftpUsername, ftpPassword);
    if (!loginRet) {
        logger.debug("FTP登錄失敗");
        return null;
    }
    // 進入被動模式
    ftp.enterLocalPassiveMode();
    boolean changeDirResult = MKDAndCWD(ftp, remoteDir);
    if (!changeDirResult) {
        logger.debug("創建/切換文件夾失敗");
        return null;
    }
    // 傳輸二進製文件
    ftp.setFileType(FTP.BINARY_FILE_TYPE);
    return ftp;
}
 
開發者ID:wyp0596,項目名稱:elegant-springboot,代碼行數:29,代碼來源:MyFtpClient.java

示例3: connectServer

import org.apache.commons.net.ftp.FTPClient; //導入方法依賴的package包/類
private boolean connectServer(String ip, int port, String user, String pwd) {

        boolean isSuccess = false;
        ftpClient = new FTPClient();
        try {
            ftpClient.connect(ip);
            isSuccess = ftpClient.login(user, pwd);
        } catch (IOException e) {
            logger.error("連接FTP服務器異常", e);
        }
        return isSuccess;
    }
 
開發者ID:jeikerxiao,項目名稱:X-mall,代碼行數:13,代碼來源:FTPUtil.java

示例4: uploadFileToWebApp

import org.apache.commons.net.ftp.FTPClient; //導入方法依賴的package包/類
/**
 * Uploads a file to an Azure web app.
 * @param profile the publishing profile for the web app.
 * @param fileName the name of the file on server
 * @param file the local file
 */
public static void uploadFileToWebApp(PublishingProfile profile, String fileName, InputStream file) {
    FTPClient ftpClient = new FTPClient();
    String[] ftpUrlSegments = profile.ftpUrl().split("/", 2);
    String server = ftpUrlSegments[0];
    String path = "./site/wwwroot/webapps";
    if (fileName.contains("/")) {
        int lastslash = fileName.lastIndexOf('/');
        path = path + "/" + fileName.substring(0, lastslash);
        fileName = fileName.substring(lastslash + 1);
    }
    try {
        ftpClient.connect(server);
        ftpClient.login(profile.ftpUsername(), profile.ftpPassword());
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        for (String segment : path.split("/")) {
            if (!ftpClient.changeWorkingDirectory(segment)) {
                ftpClient.makeDirectory(segment);
                ftpClient.changeWorkingDirectory(segment);
            }
        }
        ftpClient.storeFile(fileName, file);
        ftpClient.disconnect();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
開發者ID:Azure-Samples,項目名稱:acr-java-manage-azure-container-registry,代碼行數:33,代碼來源:Utils.java

示例5: uploadFileToFunctionApp

import org.apache.commons.net.ftp.FTPClient; //導入方法依賴的package包/類
/**
 * Uploads a file to an Azure function app.
 * @param profile the publishing profile for the web app.
 * @param fileName the name of the file on server
 * @param file the local file
 */
public static void uploadFileToFunctionApp(PublishingProfile profile, String fileName, InputStream file) {
    FTPClient ftpClient = new FTPClient();
    String[] ftpUrlSegments = profile.ftpUrl().split("/", 2);
    String server = ftpUrlSegments[0];
    String path = "site/wwwroot";
    if (fileName.contains("/")) {
        int lastslash = fileName.lastIndexOf('/');
        path = path + "/" + fileName.substring(0, lastslash);
        fileName = fileName.substring(lastslash + 1);
    }
    try {
        ftpClient.connect(server);
        ftpClient.login(profile.ftpUsername(), profile.ftpPassword());
        ftpClient.setFileType(FTP.ASCII_FILE_TYPE);
        for (String segment : path.split("/")) {
            if (!ftpClient.changeWorkingDirectory(segment)) {
                ftpClient.makeDirectory(segment);
                ftpClient.changeWorkingDirectory(segment);
            }
        }
        ftpClient.storeFile(fileName, file);
        ftpClient.disconnect();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
開發者ID:Azure-Samples,項目名稱:acr-java-manage-azure-container-registry,代碼行數:33,代碼來源:Utils.java

示例6: connectServer

import org.apache.commons.net.ftp.FTPClient; //導入方法依賴的package包/類
/**
 * 連接並登錄FTP服務器
 *
 * @param ip
 * @param port
 * @param user
 * @param password
 * @return
 */
private boolean connectServer(String ip, int port, String user, String password) {
    boolean isSuccess = false;
    ftpClient = new FTPClient();
    try {
        //連接FTP
        ftpClient.connect(ip);
        //登錄FTP
        isSuccess = ftpClient.login(user, password);
    } catch (IOException e) {
        logger.error("ftp連接或登錄失敗", e);
    }
    return isSuccess;
}
 
開發者ID:wangshufu,項目名稱:mmall,代碼行數:23,代碼來源:FTPUtil.java

示例7: getFTPClient

import org.apache.commons.net.ftp.FTPClient; //導入方法依賴的package包/類
protected FTPClient getFTPClient() {
	FTPClient ftpClient = new FTPClient();
	try {
		ftpClient.setConnectTimeout(connectTimeout);
		ftpClient.connect(hostname);
		ftpClient.login(userName, passWord);
		int replyCode = ftpClient.getReplyCode();
		if (!FTPReply.isPositiveCompletion(replyCode)) {
			FtpUtils.disconnect(ftpClient);
			log.warn("FTP登陸失敗,賬號或者密碼有誤");
		} else {
			ftpClient.setSoTimeout(soTimeout);
			/* 設置緩衝區大小 */
			ftpClient.setBufferSize(bufferSize);
			/* 設置服務器編碼 */
			ftpClient.setControlEncoding(encoding);
			/* 設置以二進製方式傳輸 */
			ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
			/* 設置服務器目錄 */
			// ftpClient.changeWorkingDirectory(directory);
			ftpClient.enterLocalPassiveMode();
			log.debug("成功連接並登錄FTP服務器。。。");
		}
	} catch (Exception e) {
		FtpUtils.disconnect(ftpClient);
		log.error("error", e);
	}
	return ftpClient;
}
 
開發者ID:yunjiweidian,項目名稱:TITAN,代碼行數:30,代碼來源:FtpConnManager.java

示例8: connectServer

import org.apache.commons.net.ftp.FTPClient; //導入方法依賴的package包/類
private boolean connectServer(String ip,int port,String user,String pwd){

        boolean isSuccess = false;
        ftpClient = new FTPClient();
        try {
            ftpClient.connect(ip);
            isSuccess = ftpClient.login(user,pwd);
        } catch (IOException e) {
            logger.error("連接FTP服務器異常",e);
        }
        return isSuccess;
    }
 
開發者ID:Liweimin0512,項目名稱:MMall_JAVA,代碼行數:13,代碼來源:FTPUtil.java

示例9: getFTPClient

import org.apache.commons.net.ftp.FTPClient; //導入方法依賴的package包/類
protected FTPClient getFTPClient(final String ftpServer, final String username, final String password)
        throws Exception {
    final FTPClient ftpClient = new FTPClient();
    ftpClient.connect(ftpServer);
    ftpClient.login(username, password);
    ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
    ftpClient.enterLocalPassiveMode();
    return ftpClient;
}
 
開發者ID:Microsoft,項目名稱:azure-maven-plugins,代碼行數:10,代碼來源:FTPUploader.java

示例10: uploadFile

import org.apache.commons.net.ftp.FTPClient; //導入方法依賴的package包/類
/**
 * 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
 */
public static boolean uploadFile(String url,int port,String username, String password, String path, String filename, InputStream input) {
	boolean success = false;
	FTPClient ftp = new FTPClient();
	try {
		int reply;
		ftp.connect(url, port);//連接FTP服務器
		//如果采用默認端口,可以使用ftp.connect(url)的方式直接連接FTP服務器
		ftp.login(username, password);//登錄
		reply = ftp.getReplyCode();
		if (!FTPReply.isPositiveCompletion(reply)) {
			ftp.disconnect();
			return success;
		}
		//設置FTP以2進製傳輸
		ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
		//TODO 讀取文件配置判斷是否使用主動
		ftp.enterLocalPassiveMode();//被動
		//ftp.enterLocalActiveMode();//主動
		//創建目錄
		mkDir(path,ftp);
		//改變目錄
		ftp.changeWorkingDirectory(path);
		ftp.storeFile(filename, input);			
		input.close();
		ftp.logout();
		success = true;
	} catch (IOException e) {
		e.printStackTrace();
	} finally {
		if (ftp.isConnected()) {
			try {
				ftp.disconnect();
			} catch (IOException ioe) {
			}
		}
	}
	return success;
}
 
開發者ID:Xvms,項目名稱:xvms,代碼行數:50,代碼來源:FtpClient.java

示例11: connect

import org.apache.commons.net.ftp.FTPClient; //導入方法依賴的package包/類
@ProtoMethod(description = "Connect to a ftp server", example = "")
@ProtoMethodParam(params = {"host", "port", "username", "password", "function(connected)"})
public void connect(final String host, final int port, final String username, final String password, final FtpConnectedCb callback) {
    mFTPClient = new FTPClient();

    Thread t = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                mFTPClient.connect(host, port);

                MLog.d(TAG, "1");

                if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {
                    boolean logged = mFTPClient.login(username, password);
                    mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
                    mFTPClient.enterLocalPassiveMode();
                    isConnected = logged;

                    callback.event(logged);
                }
                MLog.d(TAG, "" + isConnected);

            } catch (Exception e) {
                MLog.d(TAG, "connection failed error:" + e);
            }
        }
    });
    t.start();
}
 
開發者ID:victordiaz,項目名稱:phonk,代碼行數:31,代碼來源:PFtpClient.java

示例12: getNewFTPClient

import org.apache.commons.net.ftp.FTPClient; //導入方法依賴的package包/類
public FTPClient getNewFTPClient(Uri path, int mode) throws SocketException, IOException, AuthenticationException{

        // Use default port if not set
        int port = path.getPort();
        if (port<0) {
            port = 21; // default port
        }

        String username="anonymous"; // default user
        String password = ""; // default password

        NetworkCredentialsDatabase database = NetworkCredentialsDatabase.getInstance();
        Credential cred = database.getCredential(path.toString());
        if(cred!=null){
            password= cred.getPassword();
            username = cred.getUsername();
        }
        FTPClient ftp= new FTPClient();

        //try to connect
        ftp.connect(path.getHost(), port);
        //login to 	server
        if(!ftp.login(username, password))
        {
            ftp.logout();
            throw new AuthenticationException();
        }
        if(mode>=0){
            ftp.setFileType(mode);

        }
        int reply = ftp.getReplyCode();
        //FTPReply stores a set of constants for FTP reply codes. 
        if (!FTPReply.isPositiveCompletion(reply))
        {
            try {
                ftp.disconnect();
            } catch (IOException e) {
                throw e;
            }
            return null;
        }
        //enter passive mode
        ftp.enterLocalPassiveMode();

        return ftp;
    }
 
開發者ID:archos-sa,項目名稱:aos-FileCoreLibrary,代碼行數:48,代碼來源:Session.java

示例13: execute

import org.apache.commons.net.ftp.FTPClient; //導入方法依賴的package包/類
/**
 * 執行FTP回調操作的方法
 *
 * @param <T>      the type parameter
 * @param callback 回調的函數
 * @return the t
 * @throws IOException the io exception
 */
public <T> T execute(FTPClientCallback<T> callback) throws IOException {
    FTPClient ftp = new FTPClient();
    try {
        //登錄FTP服務器
        try {
            //設置超時時間
            ftp.setDataTimeout(7200);
            //設置默認編碼
            ftp.setControlEncoding(DEAFULT_REMOTE_CHARSET);
            //設置默認端口
            ftp.setDefaultPort(DEAFULT_REMOTE_PORT);
            //設置是否顯示隱藏文件
            ftp.setListHiddenFiles(false);
            //連接ftp服務器
            if (StringUtils.isNotEmpty(port) && NumberUtils.isDigits(port)) {
                ftp.connect(host, Integer.valueOf(port));
            } else {
                ftp.connect(host);
            }
        } catch (ConnectException e) {
            logger.error("連接FTP服務器失敗:" + ftp.getReplyString() + ftp.getReplyCode());
            throw new IOException("Problem connecting the FTP-server fail", e);
        }
        //得到連接的返回編碼
        int reply = ftp.getReplyCode();

        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
        }
        //登錄失敗權限驗證失敗
        if (!ftp.login(getUsername(), getPassword())) {
            ftp.quit();
            ftp.disconnect();
            logger.error("連接FTP服務器用戶或者密碼失敗::" + ftp.getReplyString());
            throw new IOException("Cant Authentificate to FTP-Server");
        }
        if (logger.isDebugEnabled()) {
            logger.info("成功登錄FTP服務器:" + host + " 端口:" + port);
        }
        ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
        //回調FTP的操作
        return callback.doTransfer(ftp);
    } finally {
        //FTP退出
        ftp.logout();
        //斷開FTP連接
        if (ftp.isConnected()) {
            ftp.disconnect();
        }
    }
}
 
開發者ID:numsg,項目名稱:spring-boot-seed,代碼行數:60,代碼來源:FTPClientImpl.java

示例14: connect

import org.apache.commons.net.ftp.FTPClient; //導入方法依賴的package包/類
/**
 * 連接到FTP服務器
 *
 * @param hostname 主機名
 * @param port     端口
 * @param username 用戶名
 * @param password 密碼
 * @return 是否連接成功
 * @throws IOException
 */
public boolean connect(FTPClient ftpClient, String hostname, int port, String username, String password) throws IOException {
    ftpClient.connect(hostname, port);
    ftpClient.setControlEncoding(DEAFULT_REMOTE_CHARSET);
    if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode()) && ftpClient.login(username, password)) {
        return true;
    }
    disconnect(ftpClient);
    return false;
}
 
開發者ID:numsg,項目名稱:spring-boot-seed,代碼行數:20,代碼來源:FtpHelper.java


注:本文中的org.apache.commons.net.ftp.FTPClient.connect方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。