当前位置:七道奇文章资讯编程技术Java编程
日期:2011-01-26 02:54:00  来源:本站整理

加强的Java FTP工具----扩大免费版的edtftpj[Java编程]

赞助商链接



  本文“加强的Java FTP工具----扩大免费版的edtftpj[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:

    edtftpjs是国外的一个公司所做.有免费版、企业版之分,还有不用语言的版本.商业版的功效强盛,是非常优异的FTP组建.免费的凑合能用,但是功效相对简单,实现粗糙.利用起来问题多多.

    为了让免费版的edtftpj工具也具有商业版的一些强劲功效,本人操纵业余时间做了扩大,经过测试可以正常利用.此中的算法大概不是最好,也欢送高人供应更好的算法.

    扩大的主要环绕最常用的功效来举行:

    1、加强上传下载功效,使其支持文件和文件夹,假如是文件夹,上传下载保持源的目录构造.

    2、加强判断文件、文件夹能否存在的办法,使得ftp的文件操作如本地文件File操作一样简单.

    3、增添判断能否为文件、能否为目录的办法.

    4、增添FTP配置管理的工具实现,这个不是主要的,就不贴了.

    环境:

    Java SE 1.5

    edtftpjs-2.03 free版

    实现思绪:

    担当FTP客户端核心的类com.enterprisedt.net.ftp.FileTransferClient,增添一些更为通用的有效的办法.本身考虑到覆盖,感受不当.就扩大吧!

    实现代码:

import com.enterprisedt.net.ftp.FTPException;
import com.enterprisedt.net.ftp.FileTransferClient;
import com.enterprisedt.net.ftp.WriteMode;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import zzvcom.cms.ccm.commons.StringTookit;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;

/**
* FTP加强工具
*
* @author leizhimin 2008-12-13 16:13:01
*/

public class UltraFTPClient extends FileTransferClient {
        private static Log log = LogFactory.getLog(UltraFTPClient.class);

        public UltraFTPClient() {
        }

        /**
         * 下载文件(夹),在本地保持FTP上的目录构造
         *
         * @param localFolderPath 本地存放文件夹
         * @param remotePath            远程文件(夹)途径
         * @param remoteSubPath     远程文件存放相对根目录
         * @throws FTPException
         * @throws IOException
         */

        public void ftpDownload(final String localFolderPath, final String remotePath, String remoteSubPath) throws FTPException, IOException, ParseException {
                if (isDir(remoteSubPath)) {
                        String localPath = localFolderPath + StringTookit.getRelativeRootPath(remoteSubPath, StringTookit.getParentPath(remotePath));
                        if (!new File(localPath).exists())
                                new File(localPath).mkdirs();
                        String[] x = directoryNameList(remoteSubPath, false);
                        for (String fname : x) {
                                String rmFilePath = StringTookit.formatPath(remoteSubPath + "/" + fname);
                                if (isDir(rmFilePath)) {
                                        ftpDownload(localFolderPath, remotePath, rmFilePath);
                                } else {
                                        String _localPath = localFolderPath + "/" +
                                                        StringTookit.getRelativeRootPath(rmFilePath, StringTookit.getParentPath(remotePath));
                                        downloadFile(_localPath, rmFilePath, WriteMode.OVERWRITE);
                                }
                        }
                } else if (isFile(remotePath)) {
                        String localPath = localFolderPath + StringTookit.getRelativeRootPath(remoteSubPath, remotePath);
                        downloadFile(localPath, remoteSubPath, WriteMode.OVERWRITE);
                } else {
                        log.error("所下载的文件或文件夹不存在,请查抄!");
                }
                log.info("FTP下载从服务器上的" + remoteSubPath + "下载到本地" + localFolderPath + "完毕!");
        }

        /**
         * 上传文件(夹),在FTP上保持本地的目录构造
         *
         * @param localFile         本地文件
         * @param localFilePath 本地文件的途径
         * @param remoteSubPath 远程文件存放相对根目录
         * @throws IOException
         * @throws FTPException
         */

        public void ftpUpload(File localFile, final String localFilePath, final String remoteSubPath) throws IOException, FTPException {
                if (localFile.isDirectory()) {
                        for (File file : localFile.listFiles()) {
                                if (file.isDirectory()) {
                                        String remotePath = StringTookit.formatPath(remoteSubPath) + StringTookit.getRelativeRootPath(file.getPath(), StringTookit.getParentPath(localFilePath));
                                        log.info(remotePath);
                                        if (!isExist(remotePath)) createDirectory(remotePath);
                                        ftpUpload(file, localFilePath, remoteSubPath);
                                } else {
                                        String remotePath = StringTookit.formatPath(remoteSubPath) +
                                                        StringTookit.getRelativeRootPath(file.getPath(), StringTookit.getParentPath(localFilePath));
                                        uploadFile(file.getPath(), remotePath, WriteMode.APPEND);
                                }
                        }
                } else if (localFile.isFile()) {
                        String remotePath = StringTookit.formatPath(remoteSubPath) +
                                        StringTookit.getRelativeRootPath(localFile.getPath(), StringTookit.getParentPath(localFilePath));
                        if (!isExist(StringTookit.getParentPath(remotePath)))
                                createDirectory(StringTookit.getParentPath(remotePath));
                        System.out.println(remotePath);
                        uploadFile(localFile.getPath(), remotePath, WriteMode.APPEND);
                }
                log.info("FTP上传" + localFile.getPath() + "到" + remoteSubPath + "目录下完毕!");
        }

        /**
         * @param remotePath 远程文件(夹)途径
         * @return 远程的文件(夹)资源能否存在
         */

        public boolean isExist(String remotePath) {
                boolean flag = true;
                try {
                        directoryList(remotePath);
                } catch (Exception e) {
                        flag = false;
                }
                return flag;
        }

        /**
         * @param remotePath 远程文件(夹)途径
         * @return 当远程资源存在且为文件时返回ture,不然返回false
         */


        public boolean isFile(String remotePath) {
                try {
                        int size = directoryList(remotePath).length;
                        if (size >= 0) {
                                if (exists(remotePath)) {
                                        return true;
                                }
                        }
                } catch (Exception e) {
                }
                return false;
        }

        /**
         * @param remotePath 远程文件(夹)途径
         * @return 当远程资源存在且为文件夹时返回ture,不然返回false
         */

        public boolean isDir(String remotePath) {
                try {
                        int size = directoryList(remotePath).length;
                        if (size >= 0) {
                                if (exists(remotePath)) {
                                        return false;
                                } else {
                                        return true;
                                }
                        }
                } catch (Exception e) {
                }
                return false;
        }
}


  以上是“加强的Java FTP工具----扩大免费版的edtftpj[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
  • 加强的Java FTP工具----扩大免费版的edtftpj
  • 本文地址: 与您的QQ/BBS好友分享!
    • 好的评价 如果您觉得此文章好,就请您
        0%(0)
    • 差的评价 如果您觉得此文章差,就请您
        0%(0)

    文章评论评论内容只代表网友观点,与本站立场无关!

       评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
    Copyright © 2020-2022 www.xiamiku.com. All Rights Reserved .