• springMVC下载FTP上的文件


    springMVC下载FTP上的文件

    今天没时间写。先上传 一个工具类

    工具类

    package com.utils;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    import org.apache.commons.lang3.StringUtils;
    import org.apache.commons.net.ftp.FTPClient;
    import org.apache.commons.net.ftp.FTPClientConfig;
    import org.apache.commons.net.ftp.FTPFile;
    import org.apache.commons.net.ftp.FTPReply;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    /**
     * ftp 工具类
     * 
     * @date 2016年1月15日
     */
    public class FTPUtils {
    
        private static final Logger logger = LoggerFactory.getLogger(FTPUtils.class);
    
        private static final FTPClientConfig config = new FTPClientConfig(FTPClientConfig.SYST_NT);
    public static final String DATE_TIME_FORMAT_TWO = "yyyy-MM-dd HH:mm";
        private static final int BUFFER_SIZE = 1024;
    
        static {
            config.setDefaultDateFormatStr(DATE_TIME_FORMAT_TWO);
            config.setServerTimeZoneId("GMT+8");
            config.setServerLanguageCode("zh");
        }
    
        /**
         * 获取ftp 客户端
         *
         * @param host
         * @param port
         * @param username
         * @param password
         * 
         * @return FTPClient
         */
        public static FTPClient client(String host, int port, String username, String password) {
            FTPClient client = new FTPClient();
            client.configure(config);
    
            try {
                client.connect(host, port);
                client.enterLocalPassiveMode();
    
                int replyCode = client.getReplyCode();
    
                if (FTPReply.isPositiveCompletion(replyCode)) {
                    client.login(username, password);
                    replyCode = client.getReplyCode();
    
                    if (FTPReply.isPositiveCompletion(replyCode)) {
                        return client;
                    } else {
                        logger.info(String.format("ftp client open faild. replyCode: %d, %s ", replyCode, client.getReplyString()));
                        client.disconnect();
                        return null;
                    }
                } else {
                    logger.info(String.format("ftp client open faild. replyCode: %d, %s ", replyCode, client.getReplyString()));
                    client.disconnect();
                    return null;
                }
            } catch (IOException e) {
                logger.error("ftp client open faild", e);
                return null;
            }
        }
    
        /**
         * ftp 下载远程文件
         * 
         * @param client ftp客户端对象
         * @param remoteDirPath 远程目录
         * @param remoteFile 远程文件
         * @param localDir 本地存储目录(空 使用用户当前目录)
         * @param localFile 本地存储名称(空 使用文件原始名称)
         * 
         * @return boolean
         */
        public static boolean download(FTPClient client, String remoteDirPath, String remoteFile, String localDir, String localFile) {
            try {
                boolean dir = client.changeWorkingDirectory(remoteDirPath);
    
                boolean result = false;
    
                if (dir) {
    
                    FTPFile[] files = client.listFiles(remoteFile);
    
                    if (files.length == 1) {
                        FTPFile file = files[0];
    
                        if (file.isFile()) {
                            if (StringUtils.isEmpty(localFile))
                                localFile = file.getName();
                            if (StringUtils.isEmpty(localDir))
                                localDir = System.getProperty("user.home");
    
                            File localDirFile = new File(localDir);
    
                            if (!localDirFile.exists())
                                localDirFile.mkdirs();
    
                            StringBuilder sb = new StringBuilder(localDir);
                            sb.append(File.separator).append(localFile);
    
                            try (FileOutputStream out = new FileOutputStream(sb.toString())) {
    
                                client.setBufferSize(BUFFER_SIZE);
                                client.setFileType(FTPClient.BINARY_FILE_TYPE);
    
                                result = client.retrieveFile(file.getName(), out);
                            } catch (IOException e) {
                                e.printStackTrace();
                                logger.error("ftp download faild", e);
                            }
                        }
                    }
                }
    
                return result;
            } catch (IOException e) {
                e.printStackTrace();
                logger.error("ftp download faild", e);
                return false;
            }
        }
    
        /**
         * ftp 连接断开
         *
         * @param client ftp 客户端
         */
        public static void close(FTPClient client) {
            try {
                if (!client.isConnected())
                    client.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    }
    FTPClient client = FTPUtils.client(ftpHost, ftpPort, ftpUsername, ftpPassword);
    FTPUtils.download(client, "/", file + zip, localPath, null);
    FTPUtils.close(client);
    爱生活,更爱给我带来生活的人
  • 相关阅读:
    angularjs1-8,cacheFactory,sce
    angularjs1-7,http,location
    angularjs1-7,供应商
    angularjs1-6,自定义服务
    UI设计师不可不知的安卓屏幕知识-安卓100分享
    k8s ingres 的安装与使用
    get、put、post、delete含义与区别
    [影像技术与PACS] 从技术角度看国内部份PACS厂商
    UML类图符号 各种关系说明以及举例
    理解 Delphi 的类(八)
  • 原文地址:https://www.cnblogs.com/chenyq/p/5383440.html
Copyright © 2020-2023  润新知