手写一个好用的Java FTP *** 作工具类

手写一个好用的Java FTP *** 作工具类,第1张

前言

   网上百度了很多FTPjava 工具类,发现文章代码都比较久远,且代码臃肿,即使搜到了代码写的还可以的,封装的常用 *** 作方法不全面,于是自己花了半天实现一个好用的工具类。最初想用java自带的FTPClient 的jar 去封装,后来和apache的jar工具包对比后,发现易用性远不如apache,于是决定采用apache的ftp的jar 封装ftp *** 作类。

 之前写过一篇  windows 服务器搭建FTP服务的教程

点击查看

工具类方法
  • 账户密码登录方法
  • 无账号密码登录方法
  • 字符转码方法
  • 判断文件目录是否存在方法
  • 获取文件列表方法
  • 上传文件方法
  • 下载文件方法
  • 上传文件夹方法
  • 下载文件夹方法
  • 删除文件方法
  • 删除文件夹方法
  • 创建文件夹方法
  • 文件重命名方法

 

 

 代码展示

pom文件引入依赖关系 commons-net jar

        
        
            commons-net
            commons-net
            3.6
        

工具类完整代码


import org.apache.commons.net.ftp.*;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Java FTP工具类
 */
public class FTPUtil {

    private static FTPClient ftp;

    /**
     * 方法描述:  转码
     */
    private static String transcode(String text){
        try {
            return new String(text.getBytes("GBK"),FTP.DEFAULT_CONTROL_ENCODING);
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }

    /**
     * 方法描述: 连接 ftp服务器 匿名登录无密码
     */
    public static void connectServer(String ip, int port) throws IOException {
        connectServer(ip,port,"anonymous",null);
    }

    /**
     * 方法描述: 连接 ftp服务器
     */
    public static void connectServer(String ip, int port, String user, String password) throws IOException {
        // 连接ftp服务器
        ftp = new FTPClient();
        ftp.connect(ip, port);
        // 登录ftp服务器
        ftp.login(user, password);
        //设置编码
        ftp.setControlEncoding("GBK");
        //设置文件类型
        ftp.setFileType(FTP.BINARY_FILE_TYPE);
    }

    /**
     * 关闭连接
     */
    public static void closeServer() throws IOException {
        if (ftp.isConnected()) {
            ftp.logout();
            ftp.disconnect();
        }
    }
    

    /**
     * 判断目录是否存在
     */
    public static boolean existDirectory(String pathname) throws IOException {
        boolean flag = false;
        FTPFile[] ftpFileArr = ftp.listFiles(pathname);
        for (FTPFile ftpFile : ftpFileArr) {
            if (ftpFile.isDirectory() && ftpFile.getName().equalsIgnoreCase(pathname)) {
                flag = true;
                break;
            }
        }
        return flag;
    }

    /*
     * 获取文件列表
     */
    public static List listFiles(String path) throws IOException {
        FTPFile[] ftpFiles = ftp.listFiles(path);
        List retList = new ArrayList();
        for (FTPFile ftpFile : ftpFiles) {
            retList.add(ftpFile.getName());
        }
        return retList;
    }


    /**
     * 上传文件
     */
    public static boolean uploadFile(String remote,String local) throws IOException {
        InputStream is=new FileInputStream(local);
        return ftp.storeFile(transcode(remote),is);
    }

    /**
     * 下载文件
     */
    public static boolean downloadFile(String remote,String local) throws IOException {
        OutputStream out=new FileOutputStream(local);
        return ftp.retrieveFile(transcode(remote),out);
    }

    /**
     * 删除文件
     */
    public static boolean deleteFile(String remote) throws IOException {
        return ftp.deleteFile(transcode(remote));
    }

    /**
     * 删除文件夹
     */
    public static void deleteFolder(String remote) throws IOException {
        FTPFile[] ftpFiles=ftp.listFiles(transcode(remote));
        for (FTPFile ftpFile : ftpFiles) {
            if(ftpFile.isDirectory()){
                deleteFolder(remote+"/"+ftpFile.getName());
                ftp.removeDirectory(transcode(remote+"/"+ftpFile.getName()));
            }else{
                deleteFile(ftpFile.getName());
            }
        }
        ftp.removeDirectory(transcode(remote));
    }

    /**
     * 上传文件夹到ftp服务器
     */
    public static void uploadFolder(String remote,String local) throws IOException {
        File localFile=new File(local);
        if(localFile.isDirectory()){
            String remoteDir=remote+"/"+localFile.getName();
            makeDirectory(remoteDir);
            File[] partFiles=localFile.listFiles();
            for (File file : partFiles) {
                if(file.isDirectory()){
                    uploadFolder(remoteDir+"/"+file.getName(),local+"/"+file.getName());
                }else {
                    uploadFile(remoteDir+"/"+file.getName(),local+"/"+file.getName());
                }
            }
        }
    }
    /**
     * 下载文件夹到本地
     */
    public static void downloadFolder(String remote,String local) throws IOException {
        File localFile=new File(local);
        if(!localFile.exists()){
            localFile.mkdirs();
        }
        FTPFile[] ftpFiles=ftp.listFiles(transcode(remote));
        for (FTPFile ftpFile : ftpFiles) {
            if(ftpFile.isDirectory()){
                downloadFolder(remote+"/"+ftpFile.getName(),local+"/"+ftpFile.getName());
            }else {
                downloadFile(remote+"/"+ftpFile.getName(),local+"/"+ftpFile.getName());
            }
        }
    }

    /**
     * 创建文件夹
     */
    public static void makeDirectory(String remote) throws IOException {
        if(remote.startsWith("/")){
            remote=remote.substring(1);
        }
        String[] dirNames = remote.split("/");
        String tempPath="";
        for (String dirName : dirNames) {
            tempPath=tempPath+"/"+dirName;
            ftp.makeDirectory(transcode(tempPath));
        }
    }


    /**
     * 重命名
     */
    public static boolean rename(String from, String to) throws IOException {
        return  ftp.rename(transcode(from),transcode(to));
    }



}
使用示例
    public static void main(String[] args) throws IOException {
        //匿名免密码登录
         FTPUtil.connectServer("172.16.10.201",19001,"anonymous",null);
         //下载ftp根目录所有文件到本地文件夹
         FTPUtil.downloadFolder("/","D://ftp");
         //删除文件夹以及文件
         FTPUtil.deleteFolder("tarzan");
        //创建文件夹
         FTPUtil.makeDirectory("tarzan/cms");
        //文件夹或文件重命名
         FTPUtil.rename("泰山","泰山123");
        //上传文件夹
        FTPUtil.uploadFolder("software","D:\Git");

    }

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/730525.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-27
下一篇 2022-04-27

发表评论

登录后才能评论

评论列表(0条)

保存