用ftp下載時,執(zhí)行到ftpClient.retrieveFile(fileName, os); 這個方法就卡死了,在網(wǎng)上找了很多方法也不行, ftpClient.enterLocalPassiveMode(); 加上主動被動都不行,跪求大神幫忙解決
2 回答

大話西游666
TA貢獻(xiàn)1817條經(jīng)驗 獲得超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當(dāng)前目錄 currentPath = remotePath; // 初始化當(dāng)前流量 response = 0; // 更改FTP目錄 ftpClient.changeWorkingDirectory(remotePath); // 得到FTP當(dāng)前目錄下所有文件 FTPFile[] ftpFiles = ftpClient.listFiles(); // 循環(huán)遍歷 for (FTPFile ftpFile : ftpFiles) { // 找到需要下載的文件 if (ftpFile.getName().equals(fileName)) { System.out.println("download..."); // 創(chuàng)建本地目錄 File file = new File(localPath + "/" + fileName); // 下載前時間 Date startTime = new Date(); if (ftpFile.isDirectory()) { // 下載多個文件 flag = downloadMany(file); } else { // 下載當(dāng)個文件 flag = downloadSingle(file, ftpFile); } // 下載完時間 Date endTime = new Date(); // 返回值 result = new Result(flag, Util.getFormatTime(endTime.getTime() - startTime.getTime()), Util.getFormatSize(response)); } } return result; }
再建一個Result 類
/** * 執(zhí)行每一個動作后響應(yīng)的結(jié)果,包括成功的和失敗的. * * @author hao_yujie, cui_tao * */ public class Result { /** * 響應(yīng)的內(nèi)容. */ private String response; /** * 響應(yīng)的結(jié)果. */ private boolean succeed; /** * 響應(yīng)的時間. */ private String time; /** * 無參的構(gòu)造方法. */ public Result() { } /** * 構(gòu)造方法. * * @param res 響應(yīng)的內(nèi)容 */ public Result(String res) { this.response = res; } /** * 構(gòu)造方法. * * @param suc 響應(yīng)的結(jié)果 * @param ti 響應(yīng)的時間 * @param res 響應(yīng)的內(nèi)容 */ public Result(boolean suc, String ti, String res) { this.succeed = suc; this.time = ti; this.response = res; } /** * 得到相應(yīng)內(nèi)容. * * @return 相應(yīng)內(nèi)容 */ public String getResponse() { return response; } /** * 設(shè)置相應(yīng)內(nèi)容. * * @param response 響應(yīng)內(nèi)容 */ public void setResponse(String response) { this.response = response; } /** * 得到相應(yīng)結(jié)果. * * @return 相應(yīng)結(jié)果 */ public boolean isSucceed() { return succeed; } /** * 設(shè)置響應(yīng)結(jié)果. * * @param succeed 響應(yīng)結(jié)果 */ public void setSucceed(boolean succeed) { this.succeed = succeed; } /** * 得到響應(yīng)時間. * * @return 響應(yīng)時間 */ public String getTime() { return time; } /** * 設(shè)置響應(yīng)時間. * * @param time 響應(yīng)時間 */ public void setTime(String time) { this.time = time; } }

瀟湘沐
TA貢獻(xiàn)1816條經(jīng)驗 獲得超6個贊
下載文件的方法是用什么方式去下載都沒給
1. File localFile = new File(downFilePath + name);
1. OutputStream is = new FileOutputStream(localFile);
1. //retrieveFile的第一個參數(shù)需要是 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,然后用這段代碼下載
添加回答
舉報
0/150
提交
取消