基于java socket编程 的ftp中如何实现整个文件夹的上传和下载?
谢谢了   

解决方案 »

  1.   

    整个文件夹上传或下载只是程序对这个文件夹自己做了遍历,逐个处理
    1.对这个文件夹进行遍历,获得该目录下的文件列表
    2.逐个下载文件
    3.对这个文件夹下第一个子文件夹做处理
    其实,就是树的遍历FTP原语里没有对文件夹上传下载的原语
      

  2.   

    这是我写的一个例子,希望对你有用.
    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);
    } }
    }