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


Java FTPClient.disconnect方法代碼示例

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


在下文中一共展示了FTPClient.disconnect方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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: testFTPConnect

import org.apache.commons.net.ftp.FTPClient; //導入方法依賴的package包/類
/**
 * Simple test that connects to the inbuilt ftp server and logs on
 * 
 * @throws Exception
 */
public void testFTPConnect() throws Exception
{
    logger.debug("Start testFTPConnect");
    
    FTPClient ftp = connectClient();
    try
    {
        int reply = ftp.getReplyCode();

        if (!FTPReply.isPositiveCompletion(reply))
        {
            fail("FTP server refused connection.");
        }
    
        boolean login = ftp.login(USER_ADMIN, PASSWORD_ADMIN);
        assertTrue("admin login not successful", login);
    } 
    finally
    {
        ftp.disconnect();
    }       
}
 
開發者ID:Alfresco,項目名稱:alfresco-repository,代碼行數:28,代碼來源:FTPServerTest.java

示例3: uploadDirectory

import org.apache.commons.net.ftp.FTPClient; //導入方法依賴的package包/類
/**
 * Upload directory to specified FTP server without retries.
 *
 * @param ftpServer
 * @param username
 * @param password
 * @param sourceDirectoryPath
 * @param targetDirectoryPath
 * @return Boolean to indicate whether uploading is successful.
 */
protected boolean uploadDirectory(final String ftpServer, final String username, final String password,
                                  final String sourceDirectoryPath, final String targetDirectoryPath) {
    log.debug("FTP username: " + username);
    try {
        final FTPClient ftpClient = getFTPClient(ftpServer, username, password);

        log.info(String.format(UPLOAD_DIR_START, sourceDirectoryPath, targetDirectoryPath));
        uploadDirectory(ftpClient, sourceDirectoryPath, targetDirectoryPath, "");
        log.info(String.format(UPLOAD_DIR_FINISH, sourceDirectoryPath, targetDirectoryPath));

        ftpClient.disconnect();
        return true;
    } catch (Exception e) {
        log.debug(e);
        log.error(String.format(UPLOAD_DIR_FAILURE, sourceDirectoryPath, targetDirectoryPath));
    }

    return false;
}
 
開發者ID:Microsoft,項目名稱:azure-maven-plugins,代碼行數:30,代碼來源:FTPUploader.java

示例4: connectFTPServer

import org.apache.commons.net.ftp.FTPClient; //導入方法依賴的package包/類
/**
 * 連接FTP服務器
 * 
 * @author wangwei
 */
public FTPClient connectFTPServer() throws Exception {

	ftpClient = new FTPClient();
	try {
		ftpClient.configure(getFTPClientConfig());
		ftpClient.connect(ftpConstant.getHost(), ftpConstant.getPort());
		ftpClient.login(ftpConstant.getUsername(), ftpConstant.getPassword());

		// 設置以二進製方式傳輸
		ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
		// 設置被動模式
		ftpClient.enterLocalPassiveMode();
		ftpClient.setControlEncoding("GBK");

		// 響應信息
		int replyCode = ftpClient.getReplyCode();
		if ((!FTPReply.isPositiveCompletion(replyCode))) {
			// 關閉Ftp連接
			closeFTPClient();
			// 釋放空間
			ftpClient = null;
			throw new Exception("登錄FTP服務器失敗,請檢查!");
		} else {
			return ftpClient;
		}
	} catch (Exception e) {
		ftpClient.disconnect();
		ftpClient = null;
		throw e;
	}
}
 
開發者ID:smxc,項目名稱:garlicts,代碼行數:37,代碼來源:FTPUpload.java

示例5: closeFTPClient

import org.apache.commons.net.ftp.FTPClient; //導入方法依賴的package包/類
/**
 * 關閉FTP連接
 * 
 * @author wangwei
 */
public void closeFTPClient(FTPClient ftp) throws Exception {

	try {
		if (ftp.isConnected())
			ftp.disconnect();
	} catch (Exception e) {
		throw new Exception("關閉FTP服務出錯!");
	}
}
 
開發者ID:smxc,項目名稱:garlicts,代碼行數:15,代碼來源:FTPUpload.java

示例6: 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

示例7: 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

示例8: downloadFile

import org.apache.commons.net.ftp.FTPClient; //導入方法依賴的package包/類
public static boolean downloadFile(String url, int port, String username, String password,
                                   String remotePath, String fileName, String localPath) {
    boolean success = false;
    FTPClient ftp = new FTPClient();
    try {
        int reply;
        ftp.connect(url, port);
        ftp.login(username, password);
        reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            return success;
        }
        ftp.changeWorkingDirectory(remotePath);
        FTPFile[] fs = ftp.listFiles();
        System.out.println(fs.length);
        for (FTPFile ff : fs) {
            System.out.println(ff.getName());
            if (ff.getName().equals(fileName)) {
                File localFile = new File(localPath + "/" + ff.getName());
                OutputStream is = new FileOutputStream(localFile);
                ftp.retrieveFile(ff.getName(), is);
                is.close();
            }
        }
        ftp.logout();
        success = true;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException ioe) {
            }
        }
    }
    return success;
}
 
開發者ID:thomas-young-2013,項目名稱:wherehowsX,代碼行數:40,代碼來源:FtpUtils.java

示例9: disconnect

import org.apache.commons.net.ftp.FTPClient; //導入方法依賴的package包/類
/**
 * Logout and disconnect the given FTPClient. *
 * 
 * @param client
 * @throws IOException
 */
private void disconnect(FTPClient client) throws IOException {
  if (client != null) {
    if (!client.isConnected()) {
      throw new FTPException("Client not connected");
    }
    boolean logoutSuccess = client.logout();
    client.disconnect();
    if (!logoutSuccess) {
      LOG.warn("Logout failed while disconnecting, error code - "
          + client.getReplyCode());
    }
  }
}
 
開發者ID:nucypher,項目名稱:hadoop-oss,代碼行數:20,代碼來源:FTPFileSystem.java

示例10: disconnect

import org.apache.commons.net.ftp.FTPClient; //導入方法依賴的package包/類
/**
 * 斷開FTP連接
 * 
 * @author gaoxianglong
 */
protected static void disconnect(FTPClient ftpClient) {
	if (null != ftpClient) {
		try {
			ftpClient.disconnect();
		} catch (IOException e1) {
			log.error("斷開FTP會話連接異常", e1);
		}
	}
}
 
開發者ID:yunjiweidian,項目名稱:TITAN,代碼行數:15,代碼來源:FtpUtils.java

示例11: 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

示例12: disconnect

import org.apache.commons.net.ftp.FTPClient; //導入方法依賴的package包/類
/**
 * 斷開與遠程服務器的連接
 *
 * @throws IOException
 */
public void disconnect(FTPClient ftpClient) throws IOException {
    if (ftpClient.isConnected()) {
        ftpClient.disconnect();
    }
}
 
開發者ID:numsg,項目名稱:spring-boot-seed,代碼行數:11,代碼來源:FtpHelper.java

示例13: testPathNames

import org.apache.commons.net.ftp.FTPClient; //導入方法依賴的package包/類
/**
 * Test of obscure path names in the FTP server
 * 
 * RFC959 states that paths are constructed thus...
 * <string> ::= <char> | <char><string>
 * <pathname> ::= <string>
 * <char> ::= any of the 128 ASCII characters except <CR> and <LF>
 *       
 *  So we need to check how high characters and problematic are encoded     
 */
public void testPathNames() throws Exception
{
    
    logger.debug("Start testPathNames");
    
    FTPClient ftp = connectClient();

    String PATH1="testPathNames";
    
    try
    {
        int reply = ftp.getReplyCode();

        if (!FTPReply.isPositiveCompletion(reply))
        {
            fail("FTP server refused connection.");
        }
    
        boolean login = ftp.login(USER_ADMIN, PASSWORD_ADMIN);
        assertTrue("admin login successful", login);
                      
        reply = ftp.cwd("/Alfresco/User*Homes");
        assertTrue(FTPReply.isPositiveCompletion(reply));
                    
        // Delete the root directory in case it was left over from a previous test run
        try
        {
            ftp.removeDirectory(PATH1);
        }
        catch (IOException e)
        {
            // ignore this error
        }
        
        // make root directory for this test
        boolean success = ftp.makeDirectory(PATH1);
        assertTrue("unable to make directory:" + PATH1, success);
        
        success = ftp.changeWorkingDirectory(PATH1);
        assertTrue("unable to change to working directory:" + PATH1, success);
        
        assertTrue("with a space", ftp.makeDirectory("test space"));
        assertTrue("with exclamation", ftp.makeDirectory("space!"));
        assertTrue("with dollar", ftp.makeDirectory("space$"));
        assertTrue("with brackets", ftp.makeDirectory("space()"));
        assertTrue("with hash curley  brackets", ftp.makeDirectory("space{}"));


        //Pound sign U+00A3
        //Yen Sign U+00A5
        //Capital Omega U+03A9

        assertTrue("with pound sign", ftp.makeDirectory("pound \u00A3.world"));
        assertTrue("with yen sign", ftp.makeDirectory("yen \u00A5.world"));
        
        // Test steps that do not work
        // assertTrue("with omega", ftp.makeDirectory("omega \u03A9.world"));
        // assertTrue("with obscure ASCII chars", ftp.makeDirectory("?/.,<>"));    
    } 
    finally
    {
        // clean up tree if left over from previous run

        ftp.disconnect();
    }    


}
 
開發者ID:Alfresco,項目名稱:alfresco-repository,代碼行數:79,代碼來源:FTPServerTest.java

示例14: testRenameCase

import org.apache.commons.net.ftp.FTPClient; //導入方法依賴的package包/類
/**
 * Test of rename case ALF-20584
 * 
  
 */
public void testRenameCase() throws Exception
{
    
    logger.debug("Start testRenameCase");
    
    FTPClient ftp = connectClient();

    String PATH1="testRenameCase";
    
    try
    {
        int reply = ftp.getReplyCode();

        if (!FTPReply.isPositiveCompletion(reply))
        {
            fail("FTP server refused connection.");
        }
    
        boolean login = ftp.login(USER_ADMIN, PASSWORD_ADMIN);
        assertTrue("admin login successful", login);
                      
        reply = ftp.cwd("/Alfresco/User*Homes");
        assertTrue(FTPReply.isPositiveCompletion(reply));
                    
        // Delete the root directory in case it was left over from a previous test run
        try
        {
            ftp.removeDirectory(PATH1);
        }
        catch (IOException e)
        {
            // ignore this error
        }
        
        // make root directory for this test
        boolean success = ftp.makeDirectory(PATH1);
        assertTrue("unable to make directory:" + PATH1, success);
        
        ftp.cwd(PATH1);
        
        String FILE1_CONTENT_2="That's how it is says Pooh!";
        ftp.storeFile("FileA.txt" , new ByteArrayInputStream(FILE1_CONTENT_2.getBytes("UTF-8")));
        
        assertTrue("unable to rename", ftp.rename("FileA.txt", "FILEA.TXT"));
    
    } 
    finally
    {
        // clean up tree if left over from previous run

        ftp.disconnect();
    }    


}
 
開發者ID:Alfresco,項目名稱:alfresco-repository,代碼行數:61,代碼來源:FTPServerTest.java

示例15: testFtpQuotaAndFtp

import org.apache.commons.net.ftp.FTPClient; //導入方法依賴的package包/類
/**
 * Test a quota failue exception over FTP.
 * A file should not exist after a create and quota exception. 
 */
public void testFtpQuotaAndFtp() throws Exception
{
    // Enable usages
    ContentUsageImpl contentUsage = (ContentUsageImpl)applicationContext.getBean("contentUsageImpl");
    contentUsage.setEnabled(true);
    contentUsage.init();
    UserUsageTrackingComponent userUsageTrackingComponent = (UserUsageTrackingComponent)applicationContext.getBean("userUsageTrackingComponent");
    userUsageTrackingComponent.setEnabled(true);
    userUsageTrackingComponent.bootstrapInternal();
    
    final String TEST_DIR="/Alfresco/User Homes/" + USER_THREE;
 
    FTPClient ftpOne = connectClient();
    try
    {
        int reply = ftpOne.getReplyCode();

        if (!FTPReply.isPositiveCompletion(reply))
        {
            fail("FTP server refused connection.");
        }
            
        boolean login = ftpOne.login(USER_THREE, PASSWORD_THREE);
        assertTrue("user three login not successful", login);
                    
        boolean success = ftpOne.changeWorkingDirectory("Alfresco");
        assertTrue("user three unable to cd to Alfreco", success);
        success = ftpOne.changeWorkingDirectory("User*Homes");
        assertTrue("user one unable to cd to User*Homes", success);
        success = ftpOne.changeWorkingDirectory(USER_THREE);
        assertTrue("user one unable to cd to " + USER_THREE, success);
        
        /**
         * Create a file as user three which is bigger than the quota
         */
        String FILE3_CONTENT_3="test file 3 content that needs to be greater than 100 bytes to result in a quota exception being thrown";
        String FILE1_NAME = "test.docx";

        // Should not be success
        success = ftpOne.appendFile(FILE1_NAME , new ByteArrayInputStream(FILE3_CONTENT_3.getBytes("UTF-8")));
        assertFalse("user one can ignore quota", success);
            
        boolean deleted = ftpOne.deleteFile(FILE1_NAME);
        assertFalse("quota exception expected", deleted);
        
        logger.debug("test done");
                 
    } 
    finally
    {
        // Disable usages
        contentUsage.setEnabled(false);
        contentUsage.init();
        userUsageTrackingComponent.setEnabled(false);
        userUsageTrackingComponent.bootstrapInternal();
        
        
        ftpOne.dele(TEST_DIR);
        if(ftpOne != null)
        {
            ftpOne.disconnect();
        }
    }       

}
 
開發者ID:Alfresco,項目名稱:alfresco-repository,代碼行數:70,代碼來源:FTPServerTest.java


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