用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; } }

瀟湘沐
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,然后用這段代碼下載
添加回答
舉報
0/150
提交
取消