Linux服务器FTP工具类



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

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class FtpUtils {
    private String userName;
    private String password;
    private String ip;
    private int port;

    private FTPClient ftpClient = null;
    private FTPSClient ftps = null;

    //构造方法初始化类
    public FtpUtils(String userName, String password, String ip, int port) {
        this.userName = userName;
        this.password = password;
        this.ip = ip;
        this.port = port;
    }

    //连接ftp
    public boolean connectServer() throws Exception {
        boolean flag = true;
        if (ftpClient == null) {
            ftpClient = new FTPClient();
            ftpClient.connect(ip, port);

            int reply = ftpClient.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftpClient.disconnect();
                return false;
            }

            boolean bok = ftpClient.login(userName, password);
            if (!bok) {
                try {
                    ftpClient.disconnect();
                    ftpClient = null;
                } catch (Exception e) {
                }
                throw new Exception("can not login ftp server");
            }

            ftpClient.setBufferSize(1024);
            ftpClient.setControlEncoding("GBK");
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            ftpClient.setDataTimeout(120000);
            ftpClient.enterLocalPassiveMode();
            ftpClient.setUseEPSVwithIPv4(false);
        }
        return flag;
    }
      /*  //列出所有文件内容
        public List<String> listRemoteAllFiles(String path) throws Exception {
            ftpClient.enterLocalPassiveMode();
            FTPFile[] files = ftpClient.listFiles(path, new FTPFileFilter() {
                @Override
                public boolean accept(FTPFile file) {
                   if (file.isFile()) return true ;
                    //if (file.isDirectory()) return true ;
                    return false ;
                }}) ;

            List<String> list = new ArrayList() ;
            for (FTPFile file : files) {
                list.add(file.getName()) ;
            }
            return list ;
        }*/
    //列出所有文件路径
  /*    public List<String> listRemoteAllFiles(String path) throws Exception {
          ftpClient.enterLocalPassiveMode();
          FTPFile[] files = ftpClient.listFiles(path);

          List<String> list = new ArrayList() ;
          for (FTPFile file : files) {
              if(file.isDirectory()){

                  System.out.println();

              }else {
                  list.add(file.getName());
              }

          }
          return list ;
      }*/

    //***********************

    /**
     * 查询给定文件夹路径的所有文件,包括此路径中的字目录
     *
     * @param pathName
     * @param fileDirs
     * @throws IOException
     */
    public void listRemoteAllFiles(String pathName, final List<String> fileDirs) throws IOException {
        if (pathName.startsWith("/") && pathName.endsWith("/")) {
            //更换目录到当前目录
            ftpClient.changeWorkingDirectory(pathName);
            FTPFile[] files = ftpClient.listFiles();
            for (FTPFile file : files) {
                if (file.isFile()) {
                    fileDirs.add(pathName + file.getName());
                } else if (file.isDirectory()) {

                    /*需要加此判断。否则,ftp默认将‘项目文件所在目录之下的目录(./)’与‘项目文件所在目录向上一级目录下的目录(../)’都纳入递归,这样下去就陷入一个死循环了。需将其过滤掉。*/
                    if (!".".equals(file.getName()) && !"..".equals(file.getName())) {
                        listRemoteAllFiles(pathName + file.getName() + "/", fileDirs);
                    }
                }
            }
        }
    }


    //***********************

    public void closeConnect() {
        try {
            if (ftpClient != null) {
                ftpClient.logout();
                ftpClient.disconnect();
            }
        } catch (Exception e) {
        }

    }


    /**
     * 通过ftp下载远程ftp服务器文件
     *
     * @param remotePath 远程文件所在的文件夹
     * @param fileName   远程文件文件名,不能为中文
     * @param localPath  本地保存文件路径
     * @return 下载成功返回true
     * @throws Exception
     */
    public boolean downloadFile(String remotePath, String fileName, String localPath) throws Exception {

        FileOutputStream fos = null;
        try {
            File localFile = new File(localPath, fileName);
            fos = new FileOutputStream(localFile);

            ftpClient.enterLocalPassiveMode();
            ftpClient.changeWorkingDirectory(remotePath);
            ftpClient.setBufferSize(1024);
            //设置文件类型(二进制)
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            boolean bok = ftpClient.retrieveFile(fileName, fos);

            fos.close();
            fos = null;

            return bok;
        } catch (Exception e) {
            throw e;
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                    fos = null;
                } catch (Exception e2) {
                }
            }
        }

    }


    //上传文件
    public boolean uploadFile(String remotePath, String filename, String localFilePath) throws Exception {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(new File(localFilePath));

            ftpClient.enterLocalPassiveMode();
            ftpClient.changeWorkingDirectory(remotePath);
            boolean bok = ftpClient.storeFile(filename, fis);

            fis.close();
            fis = null;

            return bok;
        } catch (Exception e) {
            throw e;
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                    fis = null;
                } catch (Exception e2) {
                }
            }
        }

    }

    //删除文件
    public boolean removeFile(String remotePath, String filename) throws Exception {
        ftpClient.changeWorkingDirectory(remotePath);
        boolean bok = ftpClient.deleteFile(filename);
        return bok;
    }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值