package com.ftp.example;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import com.enterprisedt.net.ftp.FTPException;
import com.enterprisedt.net.ftp.FTPFile;
import com.enterprisedt.net.ftp.FTPTransferType;
import com.enterprisedt.net.ftp.FileTransferClient;
public class FtpClientExample {
/**
*
* @param ftpip ftpIP地址
* @param user 用户
* @param pswd 密码
* @return
*/
public FileTransferClient getConnect(String ftpip, String user, String pswd) {
FileTransferClient client = new FileTransferClient();
try {
client.setRemoteHost(ftpip);
client.setUserName(user);
client.setPassword(pswd);
client.setRemotePort(Integer.parseInt("21"));
client.connect();
if (client.isConnected()) {
client.setContentType(FTPTransferType.BINARY);
System.out.println("------ftp连接成功!!");
client.getAdvancedFTPSettings().setControlEncoding("GB2312");
return client;
} else {
System.out.println("------不能登录FTP服务器,请和管理员联系!!");
return null;
}
} catch (FTPException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 上传到ftp
* @param localhostPath 本地的文件路径
* @param ftpPath 上传存放文件的ftp路径
* @param FileName 存放到ftp文件名
* @return
* @throws FTPException
* @throws IOException
* @throws ParseException
*/
public boolean uploadFileToFtp(String localhostPath, String ftpPath,
String FileName) throws FTPException, IOException, ParseException {
String pathFile = localhostPath;
String reFile = ftpPath + FileName;
FileTransferClient client = this.getConnect("127.0.0.1", "pas", "pas");
int length = client.directoryList(ftpPath).length;
if (length <= 0) {
System.out.println("FTP文件路径错误");
return false;
}
client.uploadFile(pathFile, reFile);
client.disconnect();
return true;
}
/**
* 从ftp下载到本地
* @param ftpPath
* @param fileNames
* @param localhostPath
* @return
* @throws FTPException
* @throws IOException
* @throws ParseException
*/
public boolean downloadFileFromFtp(String ftpPath, List fileNames,
String localhostPath) throws FTPException, IOException,
ParseException {
String pathFile = localhostPath;
String reFile = ftpPath;
FileTransferClient client = this.getConnect("127.0.0.1", "pas", "pas");
int length = client.directoryList(ftpPath).length;
if (length <= 0) {
System.out.println("ftp路径" + ftpPath + "不是正确:");
return false;
}
System.out.println("可下载的文件个数" + fileNames.size());
for (int i = 0; i < fileNames.size(); i++) {
String fileName = (String) fileNames.get(i);
pathFile = pathFile + "\\" + fileName;
reFile = reFile + fileName;
System.out.println(reFile + "是否存在:" + client.exists(reFile));
if (client.exists(reFile)) {
System.out.println("pathFile--" + pathFile);
System.out.println("reFile--" + reFile);
client.downloadFile(pathFile, reFile);
client.deleteFile(reFile);
pathFile = localhostPath;
reFile = ftpPath;
continue;
}
}
client.disconnect();
return true;
}
/**
* 获取文件夹下面所有的子目录文件
*
* @param filePath
* @return
* @throws ParseException
* @throws IOException
* @throws FTPException
*/
public List getPathFile(String filePath) throws FTPException, IOException,
ParseException {
List list = new ArrayList();
FileTransferClient client = this.getConnect("127.0.0.1", "pas", "pas");
FTPFile diretoryNames[];
diretoryNames = client.directoryList(filePath);
if (diretoryNames.length > 0) // 目录下面有文件
{
for (int j = 0; j < diretoryNames.length; j++) {
if (diretoryNames[j].getName().equals(".")
|| diretoryNames[j].getName().equals(".."))
continue;
list.add(diretoryNames[j].getName());
}
}
client.disconnect();
return list;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
FtpClientExample example = new FtpClientExample();
try {
List list = example.getPathFile("/bank/010/sqzf/");
System.out.println(list.size());
} catch (FTPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
分享到:
相关推荐
FTP上传下载文件
windows c++简单的 ftp上传下载
一个支持FTP上传下载的类。 支持窗口显示和线程控制
关于VC++ FTP上传下载的操作 包含有XML文件操作类
ftp上传下载工具上传下载工具ftp上传下载工具ftp上传下载工具ftp上传下载工具ftp上传下载工具ftp上传下载工具ftp上传下载工具ftp上传下载工具ftp
ftp上传下载器
VisualC 实效编程 99 FTP上传下载VisualC 实效编程 99 FTP上传下载VisualC 实效编程 99 FTP上传下载VisualC 实效编程 99 FTP上传下载VisualC 实效编程 99 FTP上传下载VisualC 实效编程 99 FTP上传下载VisualC 实效...
ftp上传下载 linux环境下程序 网络高级程序设计课程实验
qt ftp上传下载。博客地址 https://blog.csdn.net/sinat_33859977/article/details/99564157
利用C# 实现ftp上传下载 文件 文件夹等操作
用纯VB API实现的FTP上传下载的类,可以指下载,上传有进度控制,在公司里以应用相当长时间,比较稳定。代码结构清晰,附带一调用示例。
ftp上传下载java代码,代码完整 都是自己编写的
FTP上传下载 C#源代码,建议大家下载看一下再评论FTP上传下载 C#源代码,建议大家下载看一下再评论
FTP上传下载的实现,网络编程 FTP SOCKET MFC 网络
FlashXP+FTP上传下载工具破解版
ftp上传下载的好工具 已经是破解的了 不需要注册 你只需要连接自己的ftp就可以了 剩下的就不用说了吧
12.FTP上传下载文件演示(Visual C++编程 源代码)12.FTP上传下载文件演示(Visual C++编程 源代码)12.FTP上传下载文件演示(Visual C++编程 源代码)12.FTP上传下载文件演示(Visual C++编程 源代码)12.FTP上传...
FTP上传下载,上传文件的类型及文件大小都放到调用此类的方法中去检测,比如放到前台JAVASCRIPT中去检测等
本程序在VS2010环境下进行开发,程序用C# WinForm实现了FTP上传下载(可实现上传所选目录下的所有文件)并带上传进度条,程序会在运行根目录下自动生成日志文件以方便查看信息...本程序主要由socket进行通信...程序由...
curl实现ftp上传下载,vs版本实现,可以使用