当前位置: 首页>>代码示例>>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;未经允许,请勿转载。