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


Java FTP類代碼示例

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


FTP類屬於org.apache.commons.net.ftp包,在下文中一共展示了FTP類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: getFTPClient

import org.apache.commons.net.ftp.FTP; //導入依賴的package包/類
public FTPClient getFTPClient(Uri uri) throws SocketException, IOException, AuthenticationException{
    NetworkCredentialsDatabase database = NetworkCredentialsDatabase.getInstance();
    Credential cred = database.getCredential(uri.toString());
    if(cred==null){
        cred = new Credential("anonymous","", buildKeyFromUri(uri).toString(), true);
    }
    FTPClient ftpclient = ftpClients.get(cred);
    if (ftpclient!=null && ftpclient.isConnected()){
        return ftpclient;
    }
    // Not previous session found, open a new one
    Log.d(TAG, "create new ftp session for "+uri);
    FTPClient ftp = getNewFTPClient(uri,FTP.BINARY_FILE_TYPE);
    if(ftp==null)
        return null;
    Uri key = buildKeyFromUri(uri);
    Log.d(TAG, "new ftp session created with key "+key);
    ftpClients.put(cred, ftp);
    return ftp;
}
 
開發者ID:archos-sa,項目名稱:aos-FileCoreLibrary,代碼行數:21,代碼來源:Session.java

示例2: getFTPSClient

import org.apache.commons.net.ftp.FTP; //導入依賴的package包/類
public FTPClient getFTPSClient(Uri uri) throws SocketException, IOException, AuthenticationException{
    NetworkCredentialsDatabase database = NetworkCredentialsDatabase.getInstance();
    Credential cred = database.getCredential(uri.toString());
    if(cred==null){
        cred = new Credential("anonymous","", buildKeyFromUri(uri).toString(), true);
    }
    FTPClient ftpclient = ftpsClients.get(cred);
    if (ftpclient!=null && ftpclient.isConnected()){
        return ftpclient;
    }
    // Not previous session found, open a new one
    Log.d(TAG, "create new ftp session for "+uri);
    FTPClient ftp = getNewFTPSClient(uri, FTP.BINARY_FILE_TYPE);
    if(ftp==null)
        return null;
    Uri key = buildKeyFromUri(uri);
    Log.d(TAG, "new ftp session created with key "+key);
    ftpsClients.put(cred, ftp);
    return ftp;
}
 
開發者ID:archos-sa,項目名稱:aos-FileCoreLibrary,代碼行數:21,代碼來源:Session.java

示例3: connectClient

import org.apache.commons.net.ftp.FTP; //導入依賴的package包/類
@Override
public boolean connectClient() throws IOException {
    boolean isLoggedIn = true;
    client.setAutodetectUTF8(true);
    client.setControlEncoding("UTF-8");
    client.connect(host, port);
    client.setFileType(FTP.BINARY_FILE_TYPE);
    client.enterLocalPassiveMode();
    client.login(username, password);
    int reply = client.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply)) {
        client.disconnect();
        LogUtils.LOGD(TAG, "Negative reply form FTP server, aborting, id was {}:"+ reply);
        //throw new IOException("failed to connect to FTP server");
        isLoggedIn = false;
    }
    return isLoggedIn;
}
 
開發者ID:kranthi0987,項目名稱:easyfilemanager,代碼行數:19,代碼來源:FTPSNetworkClient.java

示例4: initFtpClient

import org.apache.commons.net.ftp.FTP; //導入依賴的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

示例5: connect

import org.apache.commons.net.ftp.FTP; //導入依賴的package包/類
/**
 * Connect to the FTP server using configuration parameters *
 * 
 * @return An FTPClient instance
 * @throws IOException
 */
private FTPClient connect() throws IOException {
  FTPClient client = null;
  Configuration conf = getConf();
  String host = conf.get(FS_FTP_HOST);
  int port = conf.getInt(FS_FTP_HOST_PORT, FTP.DEFAULT_PORT);
  String user = conf.get(FS_FTP_USER_PREFIX + host);
  String password = conf.get(FS_FTP_PASSWORD_PREFIX + host);
  client = new FTPClient();
  client.connect(host, port);
  int reply = client.getReplyCode();
  if (!FTPReply.isPositiveCompletion(reply)) {
    throw NetUtils.wrapException(host, port,
                 NetUtils.UNKNOWN_HOST, 0,
                 new ConnectException("Server response " + reply));
  } else if (client.login(user, password)) {
    client.setFileTransferMode(FTP.BLOCK_TRANSFER_MODE);
    client.setFileType(FTP.BINARY_FILE_TYPE);
    client.setBufferSize(DEFAULT_BUFFER_SIZE);
  } else {
    throw new IOException("Login failed on server - " + host + ", port - "
        + port + " as user '" + user + "'");
  }

  return client;
}
 
開發者ID:aliyun-beta,項目名稱:aliyun-oss-hadoop-fs,代碼行數:32,代碼來源:FTPFileSystem.java

示例6: uploadAll

import org.apache.commons.net.ftp.FTP; //導入依賴的package包/類
private boolean uploadAll() {
    FTPClient ftpClient = new FTPClient();
    try {
        ftpClient.connect(FTP_HOST, FTP_PORT);
        if (!ftpClient.login(FTP_USER, FTP_PASSWORD)) {
            return false;
        }
        String dirName = getDirName();
        ftpClient.makeDirectory(dirName);
        ftpClient.enterLocalPassiveMode();
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE, FTP.BINARY_FILE_TYPE);
        ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
        boolean result = true;
        for (File file: files) {
            result &= upload(ftpClient, file, dirName + "/" + file.getName());
        }
        return result;
    } catch (IOException e) {
        Log.e(BugReportSenderFtp.class.getSimpleName(), "FTP network error: " + e.getMessage());
    } finally {
        closeSilently(ftpClient);
    }
    return false;
}
 
開發者ID:yeriomin,項目名稱:YalpStore,代碼行數:25,代碼來源:BugReportSenderFtp.java

示例7: connect

import org.apache.commons.net.ftp.FTP; //導入依賴的package包/類
/**
 * 連接FTP服務器
 * @param host FTP服務器地址
 * @param port FTP服務器端口號
 * @param username 用戶名
 * @param password 密碼
 * @return
 */
public  static boolean connect(String host, int port, String username, String password) {
    try{
    	 ftpClient  = new FTPClient();
        ftpClient.connect(host, port);
        // 登錄
        ftpClient.login(username, password);
        ftpClient.setBufferSize(FTP_SERVER_BUFFER_SIZE_VALUE);  
		ftpClient.setControlEncoding(FTP_CLIENT_ENCODING);
		ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
	   	ftpClient.setDataTimeout(FTP_SERVER_DATA_TIME_OUT);
    	ftpClient.setSoTimeout(FTP_SERVER_SO_TIME_OUT);
    	// 檢驗是否連接成功
        int reply = ftpClient.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
        	LOGGER.error("連接FTP服務器失敗,響應碼:"+reply);
        	disConnectionServer();
            return false;
        }
    }catch(IOException e){
    	LOGGER.error("連接FTP服務器失敗",e);
        return false;
    }
    return true;
}
 
開發者ID:codeWatching,項目名稱:codePay,代碼行數:33,代碼來源:FtpClientUtils.java

示例8: connect

import org.apache.commons.net.ftp.FTP; //導入依賴的package包/類
/**
 * Opens the Server connection using the specified IP address and the default port.
 * @throws Exception Thrown if an exception is encountered during the connect or login operations.
 */
public void connect() throws Exception {
    logger.info("Connecting to '" + address + "'...");
    server.connect(address, port);

    logger.info("Logging in with credentials '" + username + "', '<hidden>'");
    server.login(username, password);

    logger.info("Configuring connection...");
    server.setFileType(FTP.BINARY_FILE_TYPE, FTP.BINARY_FILE_TYPE);
    server.setBufferSize(1024 * 1024);
    server.setAutodetectUTF8(true);
    server.setControlKeepAliveTimeout(300);

    logger.info("Connection established.");
}
 
開發者ID:jpdillingham,項目名稱:SeedboxSync,代碼行數:20,代碼來源:Server.java

示例9: connectToFtpServer

import org.apache.commons.net.ftp.FTP; //導入依賴的package包/類
/**
 * Connects the client to the FTP Server if is not already connected.
 */
public void connectToFtpServer() {
    try {
        ftpClient.connect(ftpServerName, port);
        ftpClient.login(username, password);

        ftpClient.enterLocalPassiveMode();
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        ftpClient.changeWorkingDirectory(ftpHomeDirectory);

        if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
            throw new IOException("FTP server refused connection.");
        }

        LOGGER.info(String.format("Connected to an FTP Server with IP (%s:%s).", ftpServerName, port));
    } catch (IOException e) {
        LOGGER.error(String.format("Failed to connect to an FTP Server with IP (%s:%s).", ftpServerName, port), e);
    }
}
 
開發者ID:MusalaSoft,項目名稱:atmosphere-agent,代碼行數:22,代碼來源:FtpConnectionManager.java

示例10: getFTPInputStream

import org.apache.commons.net.ftp.FTP; //導入依賴的package包/類
private static InputStream getFTPInputStream(String ftpServerName, String checklistRemoteFilePath) throws IOException {
    FTPClient ftp = new FTPClient();
    ftp.connect(ftpServerName);
    int reply = ftp.getReplyCode();

    if (!FTPReply.isPositiveCompletion(reply)) {
        ftp.disconnect();
        throw new IOException("FTP server " + ftpServerName + " refused connection");
    }

    if (!ftp.login(USERNAME, PASSWORD)) {
        ftp.logout();
        throw new IOException("FTP server failed to login using username anonymous");
    }
    ftp.setFileType(FTP.ASCII_FILE_TYPE);
    InputStream inputStream = ftp.retrieveFileStream(checklistRemoteFilePath);
    return inputStream;
}
 
開發者ID:EMBL-EBI-SUBS-OLD,項目名稱:subs,代碼行數:19,代碼來源:FTPChecklistValidatorFactoryImpl.java

示例11: getVersionAndTimestamp

import org.apache.commons.net.ftp.FTP; //導入依賴的package包/類
private Pair<String, Date> getVersionAndTimestamp(String path) throws SocketException, IOException, InterruptedException, ParseException {
	try (MyFTPClient ftp = new MyFTPClient(ftpServer, ftpPort, ftpUser, ftpPw)) {		
		for (FTPFile f : ftp.listFiles(path)) {
			if (f.getName().equals(ProgramPackageFile.BUILD_FN)) {
				File tsF = new File("./updateTimestamp.txt");
				ftp.downloadFile(path, f, tsF, FTP.ASCII_FILE_TYPE);
				Properties p = new Properties();
				FileInputStream fis = new FileInputStream(tsF);
				p.load(fis);
				fis.close();
				tsF.delete();

				logger.debug("remote timestamp string: "+p.getProperty("Date"));
				return Pair.of(p.getProperty("Version"), CoreUtils.DATE_FORMAT.parse(p.getProperty("Date")));
			}
		}
	}
	
	return null;
}
 
開發者ID:Transkribus,項目名稱:TranskribusSwtGui,代碼行數:21,代碼來源:FTPProgramUpdater.java

示例12: downloadUpdate

import org.apache.commons.net.ftp.FTP; //導入依賴的package包/類
@Override
public void downloadUpdate(final File downloadFile, FTPProgramPackageFile f, final IProgressMonitor monitor, boolean downloadAll) throws Exception {
	monitor.beginTask("Downloading "+f.f.getName(), 100);
	// TODO: use FTPUtils.downloadFile method...
	FTPTransferListener transferL = new FTPTransferListener(f.f) {
		@Override public void downloaded(int percent) {
			monitor.worked(percent);
			monitor.subTask(percent+"%");											
			if (monitor.isCanceled()) {
				boolean success = abort();
			}
		}
	};
	
	try {
		FTPUtils.downloadFile(ftpServer, ftpPort, ftpUser, ftpPw, 
				f.path, FTP.BINARY_FILE_TYPE, f.f, downloadFile, transferL);
	} catch (IOException e) {
		throw new InvocationTargetException(e, e.getMessage());
	}
	
	monitor.done();
}
 
開發者ID:Transkribus,項目名稱:TranskribusSwtGui,代碼行數:24,代碼來源:FTPProgramUpdater.java

示例13: connect

import org.apache.commons.net.ftp.FTP; //導入依賴的package包/類
public boolean connect(String hostname,int port,String username,String password) throws IOException{   
    ftpClient.setConnectTimeout(30*1000);
	ftpClient.connect(hostname, port); 
    //ftpClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out))); 
    if(FTPReply.isPositiveCompletion(ftpClient.getReplyCode())){   
        if(ftpClient.login(username, password)){  
        	logger.info("login to [{}] with username[{}] password=[{}] success",hostname,username,password);
            ftpClient.enterLocalPassiveMode();   
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE); 
        	return true;   
        } 
        else{  
        	logger.error("unable to login to ftp server[{}] with username[{}] password=[{}], abort!",hostname,username,password );
	        disconnect();   
        }
    }
    return false;   
}
 
開發者ID:ExLibrisChina,項目名稱:CatalogAutoExportKits_Huiwen,代碼行數:19,代碼來源:FtpHelper.java

示例14: login

import org.apache.commons.net.ftp.FTP; //導入依賴的package包/類
private void login() throws IOException {
	if(!isConnected()){
		connect();
	}
	
	ftpClient.login(user, password);
	
	if(passiveMode){
		ftpClient.enterLocalPassiveMode();
	}
	
	//PDF transmitted with ASCII file type break the file 
	ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
	ftpClient.setBufferSize(0);
	ftpClient.setControlKeepAliveTimeout(timeOutInSeconds);
}
 
開發者ID:MatheusArleson,項目名稱:PdfUtil,代碼行數:17,代碼來源:FTPUtil.java

示例15: connect

import org.apache.commons.net.ftp.FTP; //導入依賴的package包/類
/**
 * Connect to the FTP server using configuration parameters *
 * 
 * @return An FTPClient instance
 * @throws IOException
 */
private FTPClient connect() throws IOException {
  FTPClient client = null;
  Configuration conf = getConf();
  String host = conf.get("fs.ftp.host");
  int port = conf.getInt("fs.ftp.host.port", FTP.DEFAULT_PORT);
  String user = conf.get("fs.ftp.user." + host);
  String password = conf.get("fs.ftp.password." + host);
  client = new FTPClient();
  client.connect(host, port);
  int reply = client.getReplyCode();
  if (!FTPReply.isPositiveCompletion(reply)) {
    throw new IOException("Server - " + host
        + " refused connection on port - " + port);
  } else if (client.login(user, password)) {
    client.setFileTransferMode(FTP.BLOCK_TRANSFER_MODE);
    client.setFileType(FTP.BINARY_FILE_TYPE);
    client.setBufferSize(DEFAULT_BUFFER_SIZE);
  } else {
    throw new IOException("Login failed on server - " + host + ", port - "
        + port);
  }

  return client;
}
 
開發者ID:rhli,項目名稱:hadoop-EAR,代碼行數:31,代碼來源:FTPFileSystem.java


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