为了账号安全,请及时绑定邮箱和手机立即绑定

javaftp下载出现阻塞问题

javaftp下载出现阻塞问题

莫回无 2018-07-26 11:18:05
用ftp下载时,执行到ftpClient.retrieveFile(fileName, os); 这个方法就卡死了,在网上找了很多方法也不行, ftpClient.enterLocalPassiveMode(); 加上主动被动都不行,跪求大神帮忙解决 
查看完整描述

2 回答

?
大话西游666

TA贡献1817条经验 获得超14个赞

以下为下载方法

     /**
     * 下载.
     * @param remotePath FTP目录
     * @param fileName 文件名
     * @param localPath 本地目录
     * @return Result
     * @throws IOException 
     */
    public Result download(String remotePath, String fileName, String localPath) throws IOException {
        boolean flag = true;
        Result result = null;
        // 初始化FTP当前目录
        currentPath = remotePath;
        // 初始化当前流量
        response = 0;
        // 更改FTP目录
        ftpClient.changeWorkingDirectory(remotePath);
        // 得到FTP当前目录下所有文件
        FTPFile[] ftpFiles = ftpClient.listFiles();
        // 循环遍历
        for (FTPFile ftpFile : ftpFiles) {
            // 找到需要下载的文件
            if (ftpFile.getName().equals(fileName)) {
                System.out.println("download...");
                // 创建本地目录
                File file = new File(localPath + "/" + fileName);
                // 下载前时间
                Date startTime = new Date();
                if (ftpFile.isDirectory()) {
                    // 下载多个文件
                    flag = downloadMany(file);
                } else {
                    // 下载当个文件
                    flag = downloadSingle(file, ftpFile);
                }
                // 下载完时间
                Date endTime = new Date();
                // 返回值
                result = new Result(flag, Util.getFormatTime(endTime.getTime() - startTime.getTime()), Util.getFormatSize(response));
            }
        }
        return result;
    }

再建一个Result 类

 /**
 * 执行每一个动作后响应的结果,包括成功的和失败的.
 * 
 * @author hao_yujie, cui_tao
 *
 */
public class Result {

    /**
     * 响应的内容.
     */
    private String response;

    /**
     * 响应的结果.
     */
    private boolean succeed;

    /**
     * 响应的时间.
     */
    private String time;

    /**
     * 无参的构造方法.
     */
    public Result() {
    }

    /**
     * 构造方法.
     * 
     * @param res 响应的内容
     */
    public Result(String res) {
        this.response = res;
    }

    /**
     * 构造方法.
     * 
     * @param suc 响应的结果
     * @param ti 响应的时间
     * @param res 响应的内容
     */
    public Result(boolean suc, String ti, String res) {
        this.succeed = suc;
        this.time = ti;
        this.response = res;
    }

    /**
     * 得到相应内容.
     * 
     * @return 相应内容
     */
    public String getResponse() {
        return response;
    }

    /**
     * 设置相应内容.
     * 
     * @param response 响应内容
     */
    public void setResponse(String response) {
        this.response = response;
    }

    /**
     * 得到相应结果.
     * 
     * @return 相应结果
     */
    public boolean isSucceed() {
        return succeed;
    }

    /**
     * 设置响应结果.
     * 
     * @param succeed 响应结果
     */
    public void setSucceed(boolean succeed) {
        this.succeed = succeed;
    }

    /**
     * 得到响应时间.
     * 
     * @return 响应时间
     */
    public String getTime() {
        return time;
    }

    /**
     * 设置响应时间.
     * 
     * @param time 响应时间
     */
    public void setTime(String time) {
        this.time = time;
    }

}


查看完整回答
反对 回复 2018-08-10
?
潇湘沐

TA贡献1816条经验 获得超6个赞

下载文件的方法是用什么方式去下载都没给
1. File localFile = new File(downFilePath + name);
1. OutputStream is = new FileOutputStream(localFile);
1. //retrieveFile的第一个参数需要是 ISO-8859-1 编码,并且必须是完整路径!
1. String fileName = partsRemoteDir + name;
1. ftpClient.retrieveFile(new String(fileName.getBytes("GBK"),"ISO-8859-1"), is);
1. result="下载成功";
1. is.close();
我是直接连接到ftp,然后用这段代码下载

查看完整回答
反对 回复 2018-08-10
  • 2 回答
  • 0 关注
  • 1770 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信