基于java socket编程 的ftp中如何实现整个文件夹的上传和下载? 基于java socket编程 的ftp中如何实现整个文件夹的上传和下载?谢谢了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 整个文件夹上传或下载只是程序对这个文件夹自己做了遍历,逐个处理1.对这个文件夹进行遍历,获得该目录下的文件列表2.逐个下载文件3.对这个文件夹下第一个子文件夹做处理其实,就是树的遍历FTP原语里没有对文件夹上传下载的原语 这是我写的一个例子,希望对你有用.public class FtpClient { FTPClient ftp = null; ReadConfig cfg = ReadConfig.newInstance(); public FtpClient(FTPClient ftp) { UserLog.logDebug("Start..."); this.ftp = ftp; } /** * 是否连接到FTP服务器 */ public boolean isConnection() { boolean isLogin = false; try { ftp = new FTPClient(); ftp.setControlEncoding("GBK"); ftp.connect(cfg.ftpHost, cfg.ftpPort); if (ftp.login(cfg.ftpUserName, cfg.ftpPassword)) { isLogin = true; UserLog.logDebug("Login Success!"); boolean Flag = ftp.changeWorkingDirectory(cfg.remoteDirectory); if (Flag == true) { UserLog.logDebug("set working directory successful "); } } else { isLogin = false; UserLog.logDebug("Login Error!"); } } catch (Exception ex) { UserLog.logDebug("connection", ex); } return isLogin; } /** * 上载文件,以整个文件夹为单位 */ public void upload(String localPath, String remotePath) { FileInputStream fin = null; try { boolean flag = isConnection(); if (flag == true) { File file = new File(localPath); File[] list = file.listFiles(); File temp = null; if (list != null && list.length != 0) { for (int i = 0; i < list.length; i++) { // 如果是文件,则直接执行上传 if (list[i].isFile()) { ftp.changeWorkingDirectory(remotePath); temp = new File(list[i].getAbsolutePath()); fin = new FileInputStream(temp); if (fin != null) { String destName = list[i].getName(); String tempFileName = "temp_" + list[i].getName(); ftp.setFileType(ftp.BINARY_FILE_TYPE); boolean isupload = ftp.storeFile(destName, fin); if (isupload == true) { ftp.rename(tempFileName, destName); UserLog.logDebug("upload success message:" + list[i].getAbsolutePath()); } else { UserLog.logDebug("upload failure message:" + list[i].getAbsolutePath()); } } } else if (list[i].isDirectory()) { ftp.setFileType(ftp.BINARY_FILE_TYPE); ftp.changeWorkingDirectory(remotePath); String newRemote = remotePath + File.separator + list[i].getName(); ftp.makeDirectory(list[i].getName()); upload((localPath + File.separator + list[i] .getName()), newRemote); ftp.changeWorkingDirectory(remotePath); } } }else { UserLog.logDebug("NULL>>>>>>>>>>>>"); return; } } } catch (Exception ex) { UserLog.logDebug("upload", ex); } finally { try { fin.close(); } catch (Exception e) { UserLog.logDebug(e); } } } /** * 从远程FTP中下载文件到本机,以整个文件夹为单位 */ public void download(String localPath, String remotePath) { // boolean flag=this.isConnection(); FileOutputStream outStream = null; FTPFile[] list = null; try { list = ftp.listFiles(remotePath); ftp.setFileType(ftp.BINARY_FILE_TYPE); File temp = null; for (int i = 0; i < list.length; i++) { // 如果是文件,则直接执行下载 if (list[i].isFile()) { ftp.changeWorkingDirectory(remotePath); String fileName = list[i].getName(); temp = new File(localPath + File.separator + fileName); outStream = new FileOutputStream(temp); boolean isDownload = ftp.retrieveFile(fileName, outStream); outStream.close(); if (isDownload == true) { UserLog.logDebug("成功下载文件:" + remotePath + File.separator + fileName); } } else if (list[i].isDirectory())// 是目录 { temp = new File(localPath + File.separator + list[i].getName()); temp.mkdirs(); String newRemote = remotePath + File.separator + list[i].getName(); download(localPath + File.separator + list[i].getName(), newRemote); } } } catch (Exception ex) { UserLog.logDebug("download", ex); } finally { try { outStream.close(); } catch (Exception ex) { UserLog.logDebug(ex); } } } public void closeFtpConnection() { try { if (ftp.isConnected() || ftp != null) { ftp.disconnect(); UserLog.logDebug("Close ftp Connection is finished!"); } ftp = null; } catch (Exception ex) { UserLog.logDebug("closeFtpConnection", ex); } }} 求《core java》电子书 java swing 可以画一个透明的矩形吗?(或多边形) 小菜鸟要送分了 100分求购聊天室的简单程序 请高手帮忙,J2SE里面有没有开源的控件可以用来选DATE或者DATETIME的? 急 小小问题! 写java用什么工具好,最近小弟用jbuilder3很不顺手。 startUML 的Interface Realization 虚线空三角形显示问题 求问?? 求助大神 关于位运算和逻辑运算 原子锁怎样改写 string数组哪里有问题?
1.对这个文件夹进行遍历,获得该目录下的文件列表
2.逐个下载文件
3.对这个文件夹下第一个子文件夹做处理
其实,就是树的遍历FTP原语里没有对文件夹上传下载的原语
public class FtpClient {
FTPClient ftp = null;
ReadConfig cfg = ReadConfig.newInstance(); public FtpClient(FTPClient ftp) {
UserLog.logDebug("Start...");
this.ftp = ftp; } /**
* 是否连接到FTP服务器
*/
public boolean isConnection() {
boolean isLogin = false;
try {
ftp = new FTPClient();
ftp.setControlEncoding("GBK");
ftp.connect(cfg.ftpHost, cfg.ftpPort); if (ftp.login(cfg.ftpUserName, cfg.ftpPassword)) {
isLogin = true;
UserLog.logDebug("Login Success!");
boolean Flag = ftp.changeWorkingDirectory(cfg.remoteDirectory); if (Flag == true) {
UserLog.logDebug("set working directory successful ");
}
} else {
isLogin = false;
UserLog.logDebug("Login Error!");
} } catch (Exception ex) {
UserLog.logDebug("connection", ex);
} return isLogin;
} /**
* 上载文件,以整个文件夹为单位
*/
public void upload(String localPath, String remotePath) { FileInputStream fin = null;
try { boolean flag = isConnection();
if (flag == true) {
File file = new File(localPath); File[] list = file.listFiles(); File temp = null;
if (list != null && list.length != 0) {
for (int i = 0; i < list.length; i++) {
// 如果是文件,则直接执行上传
if (list[i].isFile()) {
ftp.changeWorkingDirectory(remotePath);
temp = new File(list[i].getAbsolutePath());
fin = new FileInputStream(temp);
if (fin != null) {
String destName = list[i].getName();
String tempFileName = "temp_"
+ list[i].getName(); ftp.setFileType(ftp.BINARY_FILE_TYPE); boolean isupload = ftp.storeFile(destName, fin); if (isupload == true) {
ftp.rename(tempFileName, destName);
UserLog.logDebug("upload success message:"
+ list[i].getAbsolutePath());
} else {
UserLog.logDebug("upload failure message:"
+ list[i].getAbsolutePath());
}
}
} else if (list[i].isDirectory()) {
ftp.setFileType(ftp.BINARY_FILE_TYPE); ftp.changeWorkingDirectory(remotePath);
String newRemote = remotePath + File.separator
+ list[i].getName();
ftp.makeDirectory(list[i].getName());
upload((localPath + File.separator + list[i]
.getName()), newRemote); ftp.changeWorkingDirectory(remotePath);
}
}
}else
{
UserLog.logDebug("NULL>>>>>>>>>>>>");
return;
} }
} catch (Exception ex) {
UserLog.logDebug("upload", ex);
} finally {
try {
fin.close(); } catch (Exception e) {
UserLog.logDebug(e);
}
}
} /**
* 从远程FTP中下载文件到本机,以整个文件夹为单位
*/
public void download(String localPath, String remotePath) { // boolean flag=this.isConnection();
FileOutputStream outStream = null;
FTPFile[] list = null; try {
list = ftp.listFiles(remotePath);
ftp.setFileType(ftp.BINARY_FILE_TYPE);
File temp = null;
for (int i = 0; i < list.length; i++) {
// 如果是文件,则直接执行下载
if (list[i].isFile()) {
ftp.changeWorkingDirectory(remotePath);
String fileName = list[i].getName();
temp = new File(localPath + File.separator + fileName); outStream = new FileOutputStream(temp); boolean isDownload = ftp.retrieveFile(fileName, outStream); outStream.close(); if (isDownload == true) {
UserLog.logDebug("成功下载文件:" + remotePath
+ File.separator + fileName); }
} else if (list[i].isDirectory())// 是目录
{
temp = new File(localPath + File.separator
+ list[i].getName());
temp.mkdirs(); String newRemote = remotePath + File.separator
+ list[i].getName();
download(localPath + File.separator + list[i].getName(),
newRemote);
}
}
} catch (Exception ex) {
UserLog.logDebug("download", ex);
} finally {
try {
outStream.close();
} catch (Exception ex) {
UserLog.logDebug(ex);
}
}
} public void closeFtpConnection() {
try {
if (ftp.isConnected() || ftp != null) {
ftp.disconnect();
UserLog.logDebug("Close ftp Connection is finished!");
}
ftp = null;
} catch (Exception ex) {
UserLog.logDebug("closeFtpConnection", ex);
} }
}