以下关于 com.enterprisedt.net.ftp.FTPClient 的使用方法。
1、引入包
import com.enterprisedt.net.ftp.FTPClient;
import com.enterprisedt.net.ftp.FTPFile;
import com.enterprisedt.net.ftp.FTPMessageCollector;
import com.enterprisedt.net.ftp.FTPTransferType;
import com.enterprisedt.net.ftp.FTPConnectMode;
import com.enterprisedt.util.debug.Level; (来源:www.iocblog.net )
import com.enterprisedt.util.debug.Logger;
import java.util.List;
2、我们建一个叫做FtpEptUtil的class
/** 判断文件夹是否存在,这种方式不是很准 */
public static boolean isDirExist(String dirname,String[] files)
{
for (int i=0;i<files.length;i++)
{
if (files[i].indexOf("<DIR>")>-1&&files[i].indexOf(dirname)>-1)
{
return true;
}
}
return false;
}
String host = "10.163.7.15";
String user = "cxl";
String password = "1";
/** 定义FTPClient便利 */
FTPClient ftp = null;
try
{
/** 创建FTPClient */
ftp = new FTPClient();
/** 连接服务器 */
ftp.setRemoteHost(host);
ftp.connect();
/** 登陆 */
ftp.login(user, password);
/** 以波动模式连接 */
ftp.setConnectMode(FTPConnectMode.PASV);
/** ASCII方式:只能传输一些如txt文本文件,
* zip、jpg等文件需要使用BINARY方式
* */
//ftp.setType(FTPTransferType.ASCII);
ftp.setType(FTPTransferType.BINARY);
/** 切换到主目录,并枚举主目录的所有文件及文件夹
* 包括日期、文件大小等详细信息
* files = ftp.dir("."),则只有文件名
*/
String[] files = ftp.dir(".", true);
for (int i = 0; i < files.length; i++)
System.out.println(files[i]);
/** 下载info文件夹下的所有文件到 d:\temp 目录下 */
String outdir = "d:\\temp\\";
File attachments = new File(outdir);
/** 如果文件夹不存在,则创建 */
if (!attachments.exists())
{
attachments.mkdir();
}
/** 获取文件夹下的所有文件 */
files = ftp.dir("info");
ftp.chdir("info");
for (int i=0;i<files.length;i++)
{
/** 下载文件 */
ftp.get(outdir+files[i],files[i]);
/** 下载后删除文件 */
ftp.delete(files[i]);
}
/** 上传文件到服务器当前目录 */
ftp.put("20061108.xml", "20061108.xml");
/** 删除文件 */
ftp.delete("20061108.xml");
/** 不存在则创建文件夹 */
if (!isDirExist("20061108",files))
{
ftp.mkdir("20061108");
}
/** 断开连接 */
ftp.quit();
} catch (Exception e)
{
logger.error("Demo failed", e);
}
分享到:
相关推荐
import com.enterprisedt.net.ftp.FTPFile; import com.enterprisedt.net.ftp.FileTransferClient; import com.enterprisedt.net.ftp.WriteMode; import org.apache.commons.logging.Log; import org.apache.commons...
edtFTPnet开源代码,花了2天时间,已重新实现edtFTPnet需要支撑的nunit.framework.dll,完全...生产edtFTPnet动态库文件后,添加EnterpriseDT.Net.Ftp命名空间后,直接使用FTPClient类,可实现FTP文件上传,下载等功能.
1. **创建FTPClient对象**:通过`com.enterprisedt.net.ftp.FTPClient`类实例化一个FTP客户端对象。 2. **设置连接参数**:包括服务器地址、用户名、密码等,可以使用`setHost()`、`setUser()`和`setPassword()`方法...
在Java编程中,可以利用第三方库如EnterpriseDT的`com.enterprisedt.net.ftp.FTPClient`类来实现对FTP服务器的操作。以下是从给定的部分代码中提取的关键知识点: #### 连接FTP服务器 首先,实例化`FTPClient`对象...
- **使用第三方库**:例如`com.enterprisedt.net.ftp.FTPClient`库,虽然它的名称包含FTP,但实际上也支持通过HTTP协议下载文件。 ##### 2. FTP 方式 - **Servlet + Apache Commons Net 的 FTPClient**:这种方式...
import com.enterprisedt.net.ftp.*; public class FtPsExample { public static void main(String[] args) { FTPClient client = new FTPClient(); // 设置SSL相关参数 client.setSecurity(FTPClient....
- **安装与导入**:将`edtftpj-2.0.1`库添加到项目的类路径中,然后在代码中导入所需的类,如`com.enterprisedt.ftp.FTPClient`。 - **连接**:使用`FTPClient`的`connect()`方法连接到FTP服务器,传入服务器地址...