import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
public class FtpUtil {
private FTPClient ftpClient;
public static final int BINARY_FILE_TYPE = FTP.BINARY_FILE_TYPE;
public static final int ASCII_FILE_TYPE = FTP.ASCII_FILE_TYPE;
// path should not the path from root index
// or some FTP server would go to root as '/'.
public void connectServer(FtpConfig ftpConfig) throws SocketException,
IOException {
String server = ftpConfig.getServer();
int port = ftpConfig.getPort();
String user = ftpConfig.getUsername();
String password = ftpConfig.getPassword();
String location = ftpConfig.getLocation();
connectServer(server, port, user, password, location);
}
public void connectServer(String server, int port, String user,
String password, String path) throws SocketException, IOException {
ftpClient = new FTPClient();
ftpClient.connect(server, port);
System.out.println("Connected to " + server + ".");
System.out.println(ftpClient.getReplyCode());
ftpClient.login(user, password);
// Path is the sub-path of the FTP path
if (path.length() != 0) {
ftpClient.changeWorkingDirectory(path);
}
}
//FTP.BINARY_FILE_TYPE | FTP.ASCII_FILE_TYPE
// Set transform type
public void setFileType(int fileType) throws IOException {
ftpClient.setFileType(fileType);
}
public void closeServer() throws IOException {
if (ftpClient.isConnected()) {
ftpClient.disconnect();
}
}
//=======================================================================
//== About directory =====
// The following method using relative path better.
//=======================================================================
public boolean changeDirectory(String path) throws IOException {
return ftpClient.changeWorkingDirectory(path);
}
public boolean createDirectory(String pathName) throws IOException {
return ftpClient.makeDirectory(pathName);
}
public boolean removeDirectory(String path) throws IOException {
return ftpClient.removeDirectory(path);
}
// delete all subDirectory and files.
public boolean removeDirectory(String path, boolean isAll)
throws IOException {
if (!isAll) {
return removeDirectory(path);
}
FTPFile[] ftpFileArr = ftpClient.listFiles(path);
if (ftpFileArr == null || ftpFileArr.length == 0) {
return removeDirectory(path);
}
//
for (FTPFile ftpFile : ftpFileArr) {
String name = ftpFile.getName();
if (ftpFile.isDirectory()) {
System.out.println("* [sD]Delete subPath ["+path + "/" + name+"]");
removeDirectory(path + "/" + name, true);
} else if (ftpFile.isFile()) {
System.out.println("* [sF]Delete file ["+path + "/" + name+"]");
deleteFile(path + "/" + name);
} else if (ftpFile.isSymbolicLink()) {
} else if (ftpFile.isUnknown()) {
}
}
return ftpClient.removeDirectory(path);
}
// Check the path is exist; exist return true, else false.
public boolean existDirectory(String path) throws IOException {
boolean flag = false;
FTPFile[] ftpFileArr = ftpClient.listFiles(path);
for (FTPFile ftpFile : ftpFileArr) {
if (ftpFile.isDirectory()
&& ftpFile.getName().equalsIgnoreCase(path)) {
flag = true;
break;
}
}
return flag;
}
//=======================================================================
//== About file =====
// Download and Upload file using
// ftpUtil.setFileType(FtpUtil.BINARY_FILE_TYPE) better!
//=======================================================================
// #1. list & delete operation
// Not contains directory
public List<String> getFileList(String path) throws IOException {
// listFiles return contains directory and file, it's FTPFile instance
// listNames() contains directory, so using following to filer directory.
//String[] fileNameArr = ftpClient.listNames(path);
FTPFile[] ftpFiles= ftpClient.listFiles(path);
List<String> retList = new ArrayList<String>();
if (ftpFiles == null || ftpFiles.length == 0) {
return retList;
}
for (FTPFile ftpFile : ftpFiles) {
if (ftpFile.isFile()) {
retList.add(ftpFile.getName());
}
}
return retList;
}
public boolean deleteFile(String pathName) throws IOException {
return ftpClient.deleteFile(pathName);
}
// #2. upload to ftp server
// InputStream <------> byte[] simple and See API
public boolean uploadFile(String fileName, String newName)
throws IOException {
boolean flag = false;
InputStream iStream = null;
try {
iStream = new FileInputStream(fileName);
flag = ftpClient.storeFile(newName, iStream);
} catch (IOException e) {
flag = false;
return flag;
} finally {
if (iStream != null) {
iStream.close();
}
}
return flag;
}
public boolean uploadFile(String fileName) throws IOException {
return uploadFile(fileName, fileName);
}
public boolean uploadFile(InputStream iStream, String newName)
throws IOException {
boolean flag = false;
try {
// can execute [OutputStream storeFileStream(String remote)]
// Above method return's value is the local file stream.
flag = ftpClient.storeFile(newName, iStream);
} catch (IOException e) {
flag = false;
return flag;
} finally {
if (iStream != null) {
iStream.close();
}
}
return flag;
}
// #3. Down load
public boolean download(String remoteFileName, String localFileName)
throws IOException {
boolean flag = false;
File outfile = new File(localFileName);
OutputStream oStream = null;
try {
oStream = new FileOutputStream(outfile);
flag = ftpClient.retrieveFile(remoteFileName, oStream);
} catch (IOException e) {
flag = false;
return flag;
} finally {
oStream.close();
}
return flag;
}
public InputStream downFile(String sourceFileName) throws IOException {
return ftpClient.retrieveFileStream(sourceFileName);
}
发表评论
-
sun.net.ftp.FtpClient类
2008-12-29 22:52 1867sun.net.ftp.FtpClient.,该类库主要提供了 ... -
Map类的使用
2008-10-08 09:57 2317Map以按键/数值对的形式 ... -
Excel 读写操作
2008-09-18 15:42 973package trans; import java.io.B ... -
Oracle数据导入导出imp/exp命令
2008-09-09 13:05 873Oracle数据导入导出imp/exp就相当于oracle数据 ... -
Java实用经验总结--日期、数字篇
2008-08-25 16:48 1188Java实用经验总结--日期、数字篇1. 日期部分对于像日 ... -
ORACLE SQL and SQL*PLUS
2008-08-15 10:16 1331四、从多个表里选取数据记录 1. 数据表间的连接 简单的连接语 ... -
常用的SQL语法和数据对象
2008-08-14 12:16 833控制语句 (DML) 部分 1.INSERT (往数据表里 ... -
Oracle 排序中常用的NULL值处理方法
2008-08-14 12:10 11671、缺省处理Oracle在Order by 时缺省认为null ... -
sun.net.ftp.FtpClient使用
2008-08-06 21:10 7838FTP使用sun.net.ftp包下的api,package ... -
加密
2008-04-03 09:28 1439import javax.crypto.Cipher;impo ... -
获得星期信息
2008-04-03 09:20 997String str = "2008-03-03&q ...
相关推荐
在本文中,我们将深入探讨如何使用Apache Commons Net库中的FTP客户端类来实现FTP文件传输操作。首先,我们需要理解FTP的基本概念。 FTP是一种应用层协议,用于在互联网上进行文件传输。它允许用户从远程服务器上传...
FTPClient 是一个基于 Java 的 FTP 客户端库,提供了丰富的 FTP 操作 API,例如上传、下载、删除文件等。然而,在高并发场景下,频繁创建和销毁 FTPClient 对象可能会对服务器造成很大的压力。 为了解决这个问题,...
net 包是一个用于操作Internet基础协议(Finger,Whois,TFTP,Telnet,POP3,FTP,NNTP,以及SMTP)的底层API。Net包不仅支持对各种低层次协议的访问,而且还提供了一个高层的抽象。它使得开发者不再需要直接面对...
org.apache.commons.net.ftp的官方完整jar包 放心使用
其中,FTPClient组件提供了全面的FTP协议支持,包括上传、下载文件,创建和删除目录,设置权限等操作。该库还支持FTP over SSL/TLS(FTPS)和FTPES(Explicit FTP over SSL/TLS),确保数据传输的安全性。 2. **...
ftpClient.changeWorkingDirectory(path); ftpClient.enterLocalPassiveMode(); //由于apache不支持中文语言环境,通过定制类解析中文日期类型 ... FTPFile[] files = ftpClient.listFiles();
`FTPDemo.java`很可能是包含实际操作的示例程序,它可能展示了如何使用`FTPClientTemplate`进行文件上传、下载、删除等操作。`FtpRuntimeException.java`可能是自定义的异常类,用于捕获FTP操作过程中可能出现的问题...
7. **线程安全**:在多线程环境中,由于FTPClient对象不是线程安全的,所以每个线程应拥有独立的`FTPClient`实例,或者使用同步机制(如synchronized关键字)来保证对FTPClient的操作是互斥的。 8. **资源释放**:...
使用JDK1.8、SpringBoot2.2.10.RELEASE、lombok1.18.8、guava23.0、hutool5.3.10、commons-pool2 2.7.0、tika1.22等实现多Ftp连接池实现,通过守护线程实现连接池内连接可用性校验,配置最大、最小连接个数防止Ftp...
总结一下,"FTP Jar包 Commons等 绝对能用"指的是使用Apache Commons Net库进行FTP操作,这个库提供了全面的FTP功能,包括上传和下载,且经过验证,可以稳定地工作在Java环境中。通过理解并熟练使用这个库,开发者...
你可以通过创建`FTPClient`实例并调用其方法来执行FTP操作,如登录服务器、改变工作目录、上传或下载文件等。 2. **连接设置**:在使用`FTPClient`之前,你需要设置服务器的主机名、端口号、用户名和密码。这些可以...
Apache Commons IO库(common-io)是一个专注于I/O操作的实用工具集,提供了大量的静态方法来处理文件、流、过滤器、读写操作等。其中包含的功能有: 1. 文件操作:如创建、复制、移动、删除文件,以及检查文件属性...
创建和销毁FTPClient对象是非常耗费资源的操作,因此,我们可以使用连接池来复用这些对象,减少创建和销毁的次数,从而提高性能。 那么,什么是对象池呢?对象池是一种机制,它可以管理对象池中对象的生命周期,...
总结来说,通过Apache Commons Net库,我们可以轻松地在Android应用中实现FTP功能,进行文件的上传、下载、目录操作等,从而实现与远程服务器的数据交换。不过,注意在实际开发中,应根据具体需求选择合适的FTP操作...
在描述中提到的博客链接可能包含了一个完整的示例代码,演示了如何使用Apache Commons Net库进行FTP操作。通常,这些示例会涵盖文件的打开、关闭、错误处理以及FTP连接的管理和断开等关键步骤。 标签中提到的“源码...
1. `FTPClient` - Apache Commons Net库中的类,负责建立FTP连接和执行FTP命令。 2. `ProtocolCommandListener` - 监听FTP命令的接口,可以用来跟踪FTP会话中的事件。 3. `FTPFile` - 表示FTP服务器上的文件或目录的...
【common包】通常指的是Apache Commons,这是一个包含各种实用工具类的Java库,提供了丰富的功能,如文件处理、IO操作、日期时间处理等。在FTP下载的上下文中,可能使用了Apache Commons Net库,它提供了一系列与FTP...
Apache Commons Net是一个强大的Java库,专注于网络协议和操作,尤其在FTP(文件传输协议)方面表现出色。本文将深入解析`commons-net-3.6.jar`包,了解其核心功能、使用方法以及在实际开发中的应用。 Apache ...
1. FTP/FTPS模块:Apache Commons Net提供了强大的FTP客户端支持,包括文件上传、下载、列表、删除、重命名等操作,同时也支持FTP over SSL/TLS(FTPS)以确保数据传输的安全性。 2. Telnet模块:这个模块提供了对...