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);

 

转载于:https://www.cnblogs.com/chenyq/p/5383440.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值