diff --git a/Aria/src/main/java/com/arialyy/aria/core/FtpUrlEntity.java b/Aria/src/main/java/com/arialyy/aria/core/FtpUrlEntity.java index 15e05cd1..a684e810 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/FtpUrlEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/core/FtpUrlEntity.java @@ -23,7 +23,7 @@ import java.net.InetAddress; * Created by Aria.Lao on 2017/10/24. * ftp url 信息链接实体 */ -public class FtpUrlEntity { +public class FtpUrlEntity implements Cloneable { public String remotePath; @@ -67,4 +67,14 @@ public class FtpUrlEntity { * 有效的ip地址 */ public InetAddress validAddr; + + @Override public FtpUrlEntity clone() { + FtpUrlEntity entity = null; + try { + entity = (FtpUrlEntity) super.clone(); + } catch (CloneNotSupportedException e) { + e.printStackTrace(); + } + return entity; + } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/TaskManager.java b/Aria/src/main/java/com/arialyy/aria/core/TaskManager.java index 2d8820e2..72c5a3d3 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/TaskManager.java +++ b/Aria/src/main/java/com/arialyy/aria/core/TaskManager.java @@ -51,7 +51,7 @@ public class TaskManager { * @param task 任务 * @return {@code true}添加成功 */ - public boolean addTask(String key, AbsTask task) { + public boolean addTask(String key, Class clazz, T task) { String hash = CommonUtil.keyToHashKey(key); if (map.keySet().contains(hash)) { ALog.e(TAG, "任务【" + key + "】已存在"); diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/AbsFtpInfoThread.java b/Aria/src/main/java/com/arialyy/aria/core/common/AbsFtpInfoThread.java index 992b695c..a4a2a704 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/AbsFtpInfoThread.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/AbsFtpInfoThread.java @@ -70,17 +70,30 @@ public abstract class AbsFtpInfoThread 0) { + ALog.i(TAG, "路径【" + setRemotePath() + "】该下文件列表 ==================================="); + for (FTPFile file : files1) { + ALog.d(TAG, file.toString()); + } + ALog.i(TAG, "================================= --end-- ==================================="); + } + client.disconnect(); return; } - mSize = getFileSize(files, client, remotePath); + //为了防止编码错乱,需要使用原始字符串 + mSize = getFileSize(files, client, setRemotePath()); int reply = client.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { if (isUpload) { @@ -159,10 +172,15 @@ public abstract class AbsFtpInfoThread> extends AbsThreadTask { - protected String charSet, serverIp, port; + private final String TAG = "AbsFtpThreadTask"; + protected String charSet, port; /** * FTP 服务器编码 */ @@ -46,36 +49,91 @@ public abstract class AbsFtpThreadTask= ips.length) { + ALog.w(TAG, "遇到[ECONNREFUSED-连接被服务器拒绝]错误,已没有其他地址,链接失败"); + return null; + } + try { + Thread.sleep(1000); + } catch (InterruptedException e1) { + e1.printStackTrace(); + } + ALog.w(TAG, "遇到[ECONNREFUSED-连接被服务器拒绝]错误,正在尝试下一个地址"); + return connect(new FTPClient(), ips, index + 1, port); + } + } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/FtpDirDownloadTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/FtpDirDownloadTarget.java index bb8a1f13..206887b9 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/FtpDirDownloadTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/FtpDirDownloadTarget.java @@ -16,7 +16,6 @@ package com.arialyy.aria.core.download; import android.text.TextUtils; -import android.util.Log; import com.arialyy.aria.core.inf.AbsTaskEntity; import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.util.ALog; @@ -35,7 +34,6 @@ public class FtpDirDownloadTarget mTargetName = targetName; mTaskEntity.urlEntity = CommonUtil.getFtpUrlInfo(url); mTaskEntity.requestType = AbsTaskEntity.FTP_DIR; - Log.d(TAG, "FTP_TARGET_MD5:" + mTaskEntity.hashCode()); } private void init(String key) { diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/FtpDownloadTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/FtpDownloadTarget.java index e28769df..fa7d8aa2 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/FtpDownloadTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/FtpDownloadTarget.java @@ -17,7 +17,6 @@ package com.arialyy.aria.core.download; import android.support.annotation.NonNull; import android.text.TextUtils; -import android.util.Log; import com.arialyy.aria.core.inf.AbsTaskEntity; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java index cc0ec1b6..97bacaf5 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java @@ -43,6 +43,15 @@ import java.util.concurrent.Executors; */ public abstract class AbsGroupUtil implements IUtil { private final String TAG = "AbsGroupUtil"; + /** + * FTP文件夹 + */ + protected int FTP_DIR = 0xa1; + /** + * HTTP 任务组 + */ + protected int HTTP_GROUP = 0xa2; + /** * 任务组所有任务总长度 */ @@ -50,7 +59,7 @@ public abstract class AbsGroupUtil implements IUtil { long mCurrentLocation = 0; private ExecutorService mExePool; protected IDownloadGroupListener mListener; - protected DownloadGroupTaskEntity mTaskEntity; + protected DownloadGroupTaskEntity mGTEntity; private boolean isRunning = false; private Timer mTimer; /** @@ -82,34 +91,32 @@ public abstract class AbsGroupUtil implements IUtil { private int mFailNum = 0; //停止的任务数 private int mStopNum = 0; - //实际的下载任务数 int mActualTaskNum = 0; //初始化完成的任务数 int mInitNum = 0; // 初始化失败的任务数 int mInitFailNum = 0; - //任务组大小 int mGroupSize = 0; - AbsGroupUtil(IDownloadGroupListener listener, DownloadGroupTaskEntity taskEntity) { + AbsGroupUtil(IDownloadGroupListener listener, DownloadGroupTaskEntity groupEntity) { mListener = listener; - mTaskEntity = taskEntity; + mGTEntity = groupEntity; mExePool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); List tasks = - DbEntity.findDatas(DownloadTaskEntity.class, "groupName=?", mTaskEntity.key); + DbEntity.findDatas(DownloadTaskEntity.class, "groupName=?", mGTEntity.key); if (tasks != null && !tasks.isEmpty()) { for (DownloadTaskEntity te : tasks) { - te.removeFile = mTaskEntity.removeFile; + te.removeFile = mGTEntity.removeFile; if (te.getEntity() == null) continue; mTasksMap.put(te.getEntity().getUrl(), te); } } - mGroupSize = mTaskEntity.entity.getSubTask().size(); - mTotalLen = taskEntity.getEntity().getFileSize(); + mGroupSize = mGTEntity.entity.getSubTask().size(); + mTotalLen = groupEntity.getEntity().getFileSize(); isNeedLoadFileSize = mTotalLen <= 1; - for (DownloadEntity entity : mTaskEntity.entity.getSubTask()) { + for (DownloadEntity entity : mGTEntity.entity.getSubTask()) { File file = new File(entity.getDownloadPath()); if (entity.getState() == IEntity.STATE_COMPLETE && file.exists()) { mCompleteNum++; @@ -126,10 +133,20 @@ public abstract class AbsGroupUtil implements IUtil { updateFileSize(); } + /** + * 获取任务类型 + * + * @return {@link #FTP_DIR}、{@link #HTTP_GROUP} + */ + abstract int getTaskType(); + + /** + * 更新任务组文件大小 + */ void updateFileSize() { if (isNeedLoadFileSize) { - mTaskEntity.getEntity().setFileSize(mTotalLen); - mTaskEntity.getEntity().update(); + mGTEntity.getEntity().setFileSize(mTotalLen); + mGTEntity.getEntity().update(); } } @@ -170,7 +187,7 @@ public abstract class AbsGroupUtil implements IUtil { * @param url 子任务下载地址 */ public void cancelSubTask(String url) { - List urls = mTaskEntity.entity.getUrls(); + List urls = mGTEntity.entity.getUrls(); if (urls != null && !urls.isEmpty() && urls.contains(url)) { urls.remove(url); DownloadTaskEntity det = @@ -183,7 +200,7 @@ public abstract class AbsGroupUtil implements IUtil { mListener.onCancel(); } } - mTaskEntity.update(); + mGTEntity.update(); } Downloader d = getDownloader(url, false); if (d != null) { @@ -254,7 +271,7 @@ public abstract class AbsGroupUtil implements IUtil { } } delDownloadInfo(); - mTaskEntity.deleteData(); + mGTEntity.deleteData(); } public void onCancel() { @@ -266,20 +283,20 @@ public abstract class AbsGroupUtil implements IUtil { */ private void delDownloadInfo() { List tasks = - DbEntity.findDatas(DownloadTaskEntity.class, "groupName=?", mTaskEntity.key); + DbEntity.findDatas(DownloadTaskEntity.class, "groupName=?", mGTEntity.key); if (tasks != null && !tasks.isEmpty()) { for (DownloadTaskEntity taskEntity : tasks) { - CommonUtil.delDownloadTaskConfig(mTaskEntity.removeFile, taskEntity); + CommonUtil.delDownloadTaskConfig(mGTEntity.removeFile, taskEntity); } } - File dir = new File(mTaskEntity.getEntity().getDirPath()); - if (mTaskEntity.removeFile) { + File dir = new File(mGTEntity.getEntity().getDirPath()); + if (mGTEntity.removeFile) { if (dir.exists()) { dir.delete(); } } else { - if (!mTaskEntity.getEntity().isComplete()) { + if (!mGTEntity.getEntity().isComplete()) { dir.delete(); } } @@ -387,30 +404,34 @@ public abstract class AbsGroupUtil implements IUtil { DownloadTaskEntity taskEntity = mTasksMap.get(entity.getUrl()); if (taskEntity != null) { taskEntity.entity = entity; - //ftp登录的 - taskEntity.urlEntity = createFtpUrlEntity(entity); + if (getTaskType() == FTP_DIR) { + taskEntity.urlEntity = createFtpUrlEntity(entity); + } mTasksMap.put(entity.getUrl(), taskEntity); return taskEntity; } taskEntity = new DownloadTaskEntity(); taskEntity.entity = entity; - taskEntity.headers = mTaskEntity.headers; - taskEntity.requestEnum = mTaskEntity.requestEnum; - taskEntity.redirectUrlKey = mTaskEntity.redirectUrlKey; - taskEntity.removeFile = mTaskEntity.removeFile; - taskEntity.groupName = mTaskEntity.key; + taskEntity.headers = mGTEntity.headers; + taskEntity.requestEnum = mGTEntity.requestEnum; + taskEntity.redirectUrlKey = mGTEntity.redirectUrlKey; + taskEntity.removeFile = mGTEntity.removeFile; + taskEntity.groupName = mGTEntity.key; taskEntity.isGroupTask = true; - taskEntity.requestType = mTaskEntity.requestType; - //ftp登录的 - taskEntity.urlEntity = createFtpUrlEntity(entity); + taskEntity.requestType = mGTEntity.requestType; + taskEntity.key = entity.getDownloadPath(); + if (getTaskType() == FTP_DIR) { + taskEntity.urlEntity = createFtpUrlEntity(entity); + } taskEntity.save(); mTasksMap.put(entity.getUrl(), taskEntity); return taskEntity; } private FtpUrlEntity createFtpUrlEntity(DownloadEntity entity) { - FtpUrlEntity urlEntity = CommonUtil.getFtpUrlInfo(entity.getUrl()); - urlEntity.validAddr = mTaskEntity.urlEntity.validAddr; + FtpUrlEntity urlEntity = mGTEntity.urlEntity.clone(); + urlEntity.url = entity.getUrl(); + urlEntity.remotePath = CommonUtil.getRemotePath(entity.getUrl()); return urlEntity; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java index ca9f891a..82513e9a 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java @@ -43,6 +43,10 @@ public class DownloadGroupUtil extends AbsGroupUtil implements IUtil { mInfoPool = Executors.newCachedThreadPool(); } + @Override int getTaskType() { + return HTTP_GROUP; + } + @Override public void onCancel() { super.onCancel(); if (!mInfoPool.isShutdown()) { @@ -98,7 +102,7 @@ public class DownloadGroupUtil extends AbsGroupUtil implements IUtil { createChildDownload(te); } mInitNum++; - if (mInitNum + mInitFailNum >= mTaskEntity.getEntity().getSubTask().size() + if (mInitNum + mInitFailNum >= mGTEntity.getEntity().getSubTask().size() || !isNeedLoadFileSize) { startRunningFlow(); updateFileSize(); @@ -121,7 +125,7 @@ public class DownloadGroupUtil extends AbsGroupUtil implements IUtil { if (mActualTaskNum < 0) mActualTaskNum = 0; } failNum++; - if (mInitNum + mInitFailNum >= mTaskEntity.getEntity().getSubTask().size() + if (mInitNum + mInitFailNum >= mGTEntity.getEntity().getSubTask().size() || !isNeedLoadFileSize) { startRunningFlow(); updateFileSize(); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirDownloadUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirDownloadUtil.java index ebd6dc6c..c939d53e 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirDownloadUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirDownloadUtil.java @@ -31,27 +31,31 @@ public class FtpDirDownloadUtil extends AbsGroupUtil { super(listener, taskEntity); } + @Override int getTaskType() { + return FTP_DIR; + } + @Override protected void onStart() { super.onStart(); - if (mTaskEntity.getEntity().getFileSize() > 1) { + if (mGTEntity.getEntity().getFileSize() > 1) { startDownload(); } else { - new FtpDirInfoThread(mTaskEntity, new OnFileInfoCallback() { + new FtpDirInfoThread(mGTEntity, new OnFileInfoCallback() { @Override public void onComplete(String url, int code) { if (code >= 200 && code < 300) { - for (DownloadEntity entity : mTaskEntity.entity.getSubTask()) { + for (DownloadEntity entity : mGTEntity.entity.getSubTask()) { mExeMap.put(entity.getUrl(), createChildDownloadTask(entity)); } - mActualTaskNum = mTaskEntity.entity.getSubTask().size(); + mActualTaskNum = mGTEntity.entity.getSubTask().size(); mGroupSize = mActualTaskNum; - mTotalLen = mTaskEntity.entity.getFileSize(); + mTotalLen = mGTEntity.entity.getFileSize(); startDownload(); } } @Override public void onFail(String url, String errorMsg, boolean needRetry) { mListener.onFail(needRetry); - ErrorHelp.saveError("FTP_DIR", mTaskEntity.getEntity(), "", errorMsg); + ErrorHelp.saveError("FTP_DIR", mGTEntity.getEntity(), "", errorMsg); } }).start(); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirInfoThread.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirInfoThread.java index a7ba36d8..caec83c6 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirInfoThread.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirInfoThread.java @@ -37,7 +37,6 @@ class FtpDirInfoThread extends AbsFtpInfoThread 0) { + return TextUtils.isEmpty(m.group(4)) ? "" : "/" + m.group(4); + } + ALog.w(TAG, "链接【" + url + "】没有找到remotePath"); + return ""; + } + /** * 转换Url * diff --git a/app/src/main/java/com/arialyy/simple/download/FtpDownloadActivity.java b/app/src/main/java/com/arialyy/simple/download/FtpDownloadActivity.java index 65f0e100..1b12ae96 100644 --- a/app/src/main/java/com/arialyy/simple/download/FtpDownloadActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/FtpDownloadActivity.java @@ -37,8 +37,8 @@ import java.io.File; public class FtpDownloadActivity extends BaseActivity { //private final String URL = "ftp://192.168.1.9:21/下载/AriaPrj.zip"; //private final String URL = "ftp://192.168.1.9:21/下载/[电影天堂www.dy2018.com]赛车总动员3BD中英双字.mp4"; - //private final String URL = "ftp://d:d@dygodj8.com:12311/[电影天堂www.dy2018.com]光辉岁月BD韩语中字.rmvb"; - private final String URL = "ftp://172.18.104.64:21/upload/成都.mp3"; + private final String URL = "ftp://j:j@g.dygod18.com:7978/江湖告急BD国粤双语中字[电影天堂www.dy2018.com].mkv"; + //private final String URL = "ftp://172.18.104.64:21/upload/测试/成都.mp3"; @Override protected void init(Bundle savedInstanceState) { super.init(savedInstanceState); @@ -60,8 +60,8 @@ public class FtpDownloadActivity extends BaseActivity { - private static final String dir = "ftp://172.18.104.64:21/haha/"; + private static final String dir = "ftp://172.18.104.64:21/upload/测试"; @Bind(R.id.child_list) SubStateLinearLayout mChildList;