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; } }
相关推荐
import it.sauronsoftware.ftp4j.FTPClient; import it.sauronsoftware.ftp4j.FTPFile; import it.sauronsoftware.ftp4j.connectors.SOCKS4Connector; ...... //ftp4j使用socks4代理连接FTP示例,by ...
The main class of the library is FTPClient (it.sauronsoftware.ftp4j.FTPClient). 1. 创建FTPClient实例 FTPClient client = new FTPClient(); 连接到指定的FTP服务器(域名或IP) 不指定端口,则使用默认端口21 ...
sun.net.ftp.FtpClient,it.sauronsoftware.ftp4j.FTPClient,org.apache.commons.net.ftp.FTPClient三种不同的方式操作FTP
FTP4J库的核心类是`ch.ethz.ganymed.ftp4j.FTPClient`。通过实例化这个类,你可以开始与FTP服务器交互。以下是一些基本操作的示例代码: ```java import ch.ethz.ganymed.ftp4j.FTPClient; public class ...
import it.sauronsoftware.ftp4j.*; FTPClient client = new FTPClient(); client.connect("ftp.server.com"); client.login("username", "password"); FTPFileUpload upload = new FTPFileUpload(client); upload...
本教程将详细讲解如何使用开源库ftp4j来实现这一功能,同时结合ProgressDialog来展示上传和下载的进度。 首先,我们需要了解ftp4j库。ftp4j是一个由Marco Sulla开发的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....
import it.sauronsoftware.ftp4j.FTPClient; public class FtpClientExample { public static void main(String[] args) { FTPClient client = new FTPClient(); try { // 连接FTP服务器 client.connect("ftp....
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...
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...
import it.sauronsoftware.ftp4j.FTPClient; import it.sauronsoftware.ftp4j.FTPDataTransferMode; public class Ftp4jExample { public static void main(String[] args) { FTPClient client = new FTPClient()...
import it.sauronsoftware.ftp4j.FTPClient; FTPClient client = new FTPClient(); client.connect("ftp.server.com", 21); client.login("username", "password"); ``` 然后,你可以执行各种FTP操作,例如上传文件...
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 ...
import it.sauronsoftware.ftp4j.FTPClient; import it.sauronsoftware.ftp4j.FTPReply; public class Ftp4jExample { public static void main(String[] args) { FTPClient client = new FTPClient(); try { /...
implementation 'it.sauronsoftware.ftp4j:ftp4j:1.7.5' } ``` 接下来,我们讨论如何进行文件上传。FTP4J提供`FTPClient`类来连接FTP服务器。首先创建`FTPClient`实例,然后设置连接参数,如主机名、端口号、...
在本文中,我们将详细探讨FTPS和SSL的基本用法,以及如何使用Java的ftp4j库进行FTP操作。 FTPS分为两种模式: Explicit(明示式)和Implicit(隐式)模式。在 Explicit 模式下,客户端先建立一个未加密的FTP连接,...
import it.sauronsoftware.jave.*; AudioAttributes audio = new AudioAttributes(); audio.setCodec("libmp3lame"); audio.setBitRate(new Integer(128000)); audio.setChannels(new Integer(2)); audio....
4. 下载文件:使用 FTPClient 的 retrieveFile 方法将文件从 FTP 服务器下载到本地。 FtpUseBean 实例代码解析 FtpUseBean 是一个 JavaBean,用于描述 FTP 上传或下载的信息。它包含以下属性: * host:FTP ...