`

it.sauronsoftware.ftp4j.FTPClient

阅读更多
package classTest;

import it.sauronsoftware.ftp4j.FTPClient;

import java.io.File;

public class FtpHandle {
	private FTPClient client = new FTPClient();
	
	/**
	 * 登录FTP,并返回登录是否成功的Boolean值
	 * @param host
	 * @param port
	 * @param user
	 * @param password
	 * @return
	 */
	public boolean login(String host, int port, String user, String password) {
		boolean flag = true;
		try {
			client.connect(host, port);
			client.login(user, password);
			//数据传输方式
			client.setType(FTPClient.TYPE_BINARY);
			client.setCharset("GBK");
		} catch (Exception e) {
			e.printStackTrace();
			flag = false;
		}
		return flag;
	}
	/**
	 * 关闭FTP连接
	 */
	public void close() {
		if(client.isConnected()) {
			try {
				client.disconnect(false);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 在父级节点创建子节点目录,如果父级节点为根节点parent可以为空或“/”
	 * @param parent
	 * @param path
	 * @return
	 */
	public boolean makeDir(String parent, String path) {
		boolean flag = true;
		flag = cdAssignPath(parent);
		if(flag) {
			try {
				//创建已经存在的目录会报错
				client.createDirectory(path);
			}catch (Exception e) {
				e.printStackTrace();
				flag = false;
			}
		}
		return flag;
	}
	
	/**
	 * 上传文件
	 * @param path保存FTP位置
	 * @param file要上传的文件
	 */
	public void upload(String path, File file) {
		if(cdAssignPath(path)) {
			try {
				client.upload(file);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 下载文件
	 * @param remotePath
	 * @param remoteName
	 * @param localPath
	 * @param localName
	 */
	public void download(String remotePath, String remoteName, String localPath, String localName) {
		if(cdAssignPath(remotePath)) {
			try {
				File file = new File(localPath);
				if(!file.exists()) {
					file.mkdirs();
				}
				client.download(remoteName, new File(localPath + "/" + localName));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 获取指定路径下的文件列表
	 * @param path
	 * @return
	 */
	public String[] ls(String path) {
		try {
			if(cdAssignPath(path)) {
				return client.listNames();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 切换当前目录当根目录
	 */
	private void cdRoot() {
		try {
			while(!"/".equals(client.currentDirectory())) {
				client.changeDirectoryUp();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 切换当前目录到指定路径,该路径必须从根路径开始
	 * @param path
	 * @return
	 */
	public boolean cdAssignPath(String path) {
		boolean flag = true;
		cdRoot();
		try {
			client.changeDirectory(path);
		} catch (Exception e) {
			e.printStackTrace();
			flag = false;
		}
		return flag;
	}
}

 

分享到:
评论

相关推荐

    ftp4j-1.3.1下载

    The main class of the library is FTPClient (it.sauronsoftware.ftp4j.FTPClient). 1. 创建FTPClient实例 FTPClient client = new FTPClient(); 连接到指定的FTP服务器(域名或IP) 不指定端口,则使用默认端口21 ...

    sun.FtpClient,ftp4j.FTPClient,apache.FTPClient不同的方式操作FTP

    sun.net.ftp.FtpClient,it.sauronsoftware.ftp4j.FTPClient,org.apache.commons.net.ftp.FTPClient三种不同的方式操作FTP

    ftp4j-一个开源的支持代理的FTP组件

    import it.sauronsoftware.ftp4j.FTPClient; import it.sauronsoftware.ftp4j.FTPFile; import it.sauronsoftware.ftp4j.connectors.SOCKS4Connector; ...... //ftp4j使用socks4代理连接FTP示例,by ...

    ftp4j-1.7.2.jar

    FTP4J库的核心类是`ch.ethz.ganymed.ftp4j.FTPClient`。通过实例化这个类,你可以开始与FTP服务器交互。以下是一些基本操作的示例代码: ```java import ch.ethz.ganymed.ftp4j.FTPClient; public class ...

    android 使用ftp上传下载(使用ftp4j)弹出框(进度条)

    本教程将详细讲解如何使用开源库ftp4j来实现这一功能,同时结合ProgressDialog来展示上传和下载的进度。 首先,我们需要了解ftp4j库。ftp4j是一个由Marco Sulla开发的Java库,它提供了完整的FTP客户端功能,包括...

    ftp4j 上传文件和文件夹

    import it.sauronsoftware.ftp4j.FtpClient; import it.sauronsoftware.ftp4j.FtpException; public class FtpUploader { public void upload(String host, int port, String user, String password, File local...

    Java常用FTP文件操作说明 Apache.FTPClient,ftp4j,jftp

    import it.sauronsoftware.ftp4j.*; FTPClient client = new FTPClient(); client.connect("ftp.server.com"); client.login("username", "password"); FTPFileUpload upload = new FTPFileUpload(client); upload...

    commons-net-3.0.1.jar ftp4j

    import it.sauronsoftware.ftp4j.FTPClient; public class Ftp4jUploader { public static void main(String[] args) { FTPClient client = new FTPClient(); try { client.connect("ftp.server.com"); client...

    java源码:FTP客户端Java类库 ftp4j.zip

    import it.sauronsoftware.ftp4j.FTPClient; import it.sauronsoftware.ftp4j.FTPDataTransferMode; public class Ftp4jExample { public static void main(String[] args) { FTPClient client = new FTPClient()...

    基于java的FTP客户端Java类库 ftp4j.zip

    import it.sauronsoftware.ftp4j.FTPClient; FTPClient client = new FTPClient(); client.connect("ftp.server.com", 21); client.login("username", "password"); ``` 然后,你可以执行各种FTP操作,例如上传文件...

    FTP客户端Java类库 ftp4j

    import it.sauronsoftware.ftp4j.FTPClient; public class FtpClientExample { public static void main(String[] args) { FTPClient client = new FTPClient(); try { // 连接FTP服务器 client.connect("ftp....

    FTP下载上传的Demo

    import it.sauronsoftware.ftp4j.FTPClient; import java.io.FileOutputStream; import java.io.InputStream; public class FtpTest { public static void main(String[] args) { FTPClient client = new ...

    java定时从ftp服务器更新相关文件

    import it.sauronsoftware.ftp4j.FTPClient; public class FtpTest { public static void main(String[] args) { FTPClient client = new FTPClient(); try { client.connect("ftp.example.com"); client....

    FTP客户端Java类库 ftp4j源码示例

    import it.sauronsoftware.ftp4j.FTPClient; import it.sauronsoftware.ftp4j.FTPReply; public class Ftp4jExample { public static void main(String[] args) { FTPClient client = new FTPClient(); try { /...

    android ftp4j上传下载带进度条

    implementation 'it.sauronsoftware.ftp4j:ftp4j:1.7.5' } ``` 接下来,我们讨论如何进行文件上传。FTP4J提供`FTPClient`类来连接FTP服务器。首先创建`FTPClient`实例,然后设置连接参数,如主机名、端口号、...

    Java实现文件格式转换代码实例

    import it.sauronsoftware.jave.*; AudioAttributes audio = new AudioAttributes(); audio.setCodec("libmp3lame"); audio.setBitRate(new Integer(128000)); audio.setChannels(new Integer(2)); audio....

    Ftps,SSL的基本用法

    在本文中,我们将详细探讨FTPS和SSL的基本用法,以及如何使用Java的ftp4j库进行FTP操作。 FTPS分为两种模式: Explicit(明示式)和Implicit(隐式)模式。在 Explicit 模式下,客户端先建立一个未加密的FTP连接,...

Global site tag (gtag.js) - Google Analytics