zoukankan      html  css  js  c++  java
  • 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);
    爱生活,更爱给我带来生活的人
  • 相关阅读:
    25个优秀的 ASP.NET MVC教程及文章
    获取SQLSERVER数据库insert into操作的主键返回值,SCOPE_IDENTITY
    Silverlight 3.0 不再包含 asp:silverlight 控件
    MVP模式最佳实践(1)—MVP模式简介
    小谈.NET CLR
    XmlDocument,XmlNode,XmlElement创建复杂XML文档
    NHibernate的关联映射(onetoone,onetomany,manytomany)以及cascade分析
    关于CSS优先级的探讨
    使用ASP.NET实现Model View Presenter(MVP)
    用IIS来启用SSL
  • 原文地址:https://www.cnblogs.com/chenyq/p/5383440.html
Copyright © 2011-2022 走看看