Java—FTP文件服务器工具类FtpUtil

Ftp文件服务器上传下载文件操作工具类

package ftp;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * @author zqq
 * @date 2021/4/7 16:54
 */
public class FtpUtil {
     

    private static final String FTP_USER = "ftpuser";
    private static final String FTP_PASSWORD = "ftp123456";
    private static final String FTP_IP = "192.168.65.135";
    private static final Integer FTP_PORT = 21;
    private static final String CHARSET_ISO = "ISO-8859-1";

    public static void main(String[] args) throws Exception{
     
        //upload("C:\\Users\\user\\Desktop\\up2.txt","/upload");
        //downLoad("/upload","上传.txt","C:\\Users\\user\\Desktop\\ftp");
        downLoadFolder("/upload","C:\\Users\\user\\Desktop\\ftp");
    }

    /**
     * 下载指定Ftp文件夹的全部文件
     * @param folderPath ftp文件夹路径
     * @param localPath 下载本地磁盘路径
     * @return
     */
    public static Boolean downLoadFolder(String folderPath,String localPath){
     
        FTPClient ftp = getFTPClient();
        try {
     
            if(ftp == null){
     
                return false;
            }
            //切换到文件目录
            ftp.changeWorkingDirectory(folderPath);
            //获取文件集合
            FTPFile[] files = ftp.listFiles();
            for(FTPFile file : files){
     
                if(file.isFile()){
     
                    try (OutputStream out = new FileOutputStream(localPath +  "\\" + file.getName())){
     
                        ftp.retrieveFile(new String(file.getName().getBytes(),CHARSET_ISO),out);
                        out.flush();
                    }
                }
            }
        }catch (Exception e){
     
            e.printStackTrace();
            return false;
        }finally {
     
            closeFtp(ftp);
        }
        return true;
    }

    /**
     * 文件下载
     * @param parentPath 文件在ftp上级路径
     * @param ftpFileName 文件在ftp名称
     * @param localPath 下载本地磁盘路径
     * @return
     */
    public static Boolean downLoad(String parentPath,String ftpFileName,String localPath){
     
        FTPClient ftp = getFTPClient();
        try {
     
            if(ftp == null){
     
                return false;
            }
            parentPath = parentPath + "/" +ftpFileName;
            //根据文件名称获取输入流
            try (InputStream in = ftp.retrieveFileStream(new String(parentPath.getBytes(),CHARSET_ISO))){
     
                //写入本地文件目录
                Files.copy(in, Paths.get(localPath + "\\" + ftpFileName));
                return true;
            }
        }catch (Exception e){
     
            e.printStackTrace();
        }finally {
     
            closeFtp(ftp);
        }
        return false;
    }

    /**
     * 文件上传
     * @param filePath 文件路径
     * @param ftpPath ftp路径
     * @return
     */
    public static Boolean upload(String filePath,String ftpPath){
     
        return upload(new File(filePath),ftpPath);
    }

    /**
     * 文件上传
     * @param file 文件对象
     * @param ftpPath ftp路径
     * @return
     */
    public static Boolean upload(File file,String ftpPath){
     
        FTPClient ftp = getFTPClient();
        try {
     
            if(ftp == null){
     
                return false;
            }
            //设置被动模式
            ftp.enterLocalActiveMode();
            //二进制传输
            ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
            //如果ftp目录不存在则创建目录
            if(!ftp.changeWorkingDirectory(ftpPath)){
     
                ftp.makeDirectory(ftpPath);
                //改变工作目录
                ftp.changeWorkingDirectory(ftpPath);
            }
            //文件名
            String fileName = file.getName();
            //上传文件
            if(ftp.storeFile(new String(fileName.getBytes(),CHARSET_ISO),new FileInputStream(file))){
     
                return true;
            }
        }catch (Exception e){
     
            e.printStackTrace();
        }finally {
     
            closeFtp(ftp);
        }
        return false;
    }

    /**
     * 获取Ftp客户端对象
     * @return
     */
    private static FTPClient getFTPClient(){
     
        try {
     
            FTPClient ftpClient = new FTPClient();
            ftpClient.connect(FTP_IP,FTP_PORT);
            //连接超时时间
            ftpClient.setConnectTimeout(2 * 60 * 1000);
            //传输超时时间
            ftpClient.setDataTimeout(2 * 60 * 1000);
            ftpClient.setControlEncoding("utf-8");
            //用户名密码登录
            ftpClient.login(FTP_USER,FTP_PASSWORD);
            //校验响应码
            if(!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())){
     
                ftpClient.disconnect();
                return null;
            }
            return ftpClient;
        }catch (Exception e){
     
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 关闭Ftp客户端
     * @param ftp
     */
    private static void closeFtp(FTPClient ftp){
     
        try {
     
            if(ftp != null){
     
                ftp.logout();
            }
        }catch (Exception e){
     
            e.printStackTrace();
        }finally {
     
            try {
     
                if(ftp != null){
     
                    ftp.disconnect();
                }
            } catch (IOException e) {
     
                e.printStackTrace();
            }
        }
    }
}

你可能感兴趣的:(Java,ftp)