From e362621c3899fe60df42df4433560ff4649f9a1e Mon Sep 17 00:00:00 2001 From: laoyuyu <511455842@qq.com> Date: Thu, 7 Jun 2018 21:59:14 +0800 Subject: [PATCH] =?UTF-8?q?ftp=E4=B8=8B=E8=BD=BD=E8=8E=B7=E5=8F=96dns?= =?UTF-8?q?=E7=9A=84ip=E5=88=97=E8=A1=A8=E6=97=B6=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E8=B6=85=E6=97=B6=E9=99=90=E5=88=B6=EF=BC=8C=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E7=AB=AF=E5=8F=A3=E4=B8=8D=E5=AD=98=E5=9C=A8=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E7=9A=84=E5=B4=A9=E6=BA=83=E9=97=AE=E9=A2=98=EF=BC=8Chttps://g?= =?UTF-8?q?ithub.com/AriaLyy/Aria/issues/251=20=C3=A4=E4=BF=AE=E5=A4=8Dhtt?= =?UTF-8?q?ps://github.com/AriaLyy/Aria/issues/233?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Aria/build.gradle | 4 +- .../core/command/group/GroupCancelCmd.java | 2 +- .../aria/core/command/normal/StartCmd.java | 5 +- .../arialyy/aria/core/common/AbsFileer.java | 44 ++++++--- .../aria/core/common/AbsFtpInfoThread.java | 56 +++++++++-- .../aria/core/common/AbsFtpThreadTask.java | 2 +- .../aria/core/common/AbsThreadTask.java | 81 +++++++++------- .../aria/core/download/BaseNormalTarget.java | 6 +- .../core/download/DownloadGroupListener.java | 1 + .../core/download/DownloadGroupTarget.java | 8 +- .../download/downloader/AbsGroupUtil.java | 53 +++++++---- .../downloader/DownloadGroupUtil.java | 17 +++- .../download/downloader/FtpThreadTask.java | 37 +++----- .../download/downloader/HttpThreadTask.java | 27 ++---- .../com/arialyy/aria/core/inf/AbsTarget.java | 2 +- .../arialyy/aria/core/queue/AbsTaskQueue.java | 16 ++-- .../aria/core/scheduler/AbsSchedulers.java | 3 +- .../core/upload/uploader/FtpThreadTask.java | 28 ++---- .../core/upload/uploader/HttpThreadTask.java | 6 +- .../main/java/com/arialyy/aria/util/ALog.java | 3 + .../com/arialyy/aria/util/CommonUtil.java | 95 ++++++++----------- .../java/com/arialyy/aria/util/Regular.java | 9 -- README.md | 3 +- app/src/main/AndroidManifest.xml | 5 +- app/src/main/assets/aria_config.xml | 2 +- .../download/group/DownloadGroupActivity.java | 5 +- .../arialyy/simple/test/AnyRunActivity.java | 8 +- .../com/arialyy/simple/test/TestActivity.java | 8 +- aria/src/main/AndroidManifest.xml | 1 + 29 files changed, 302 insertions(+), 235 deletions(-) diff --git a/Aria/build.gradle b/Aria/build.gradle index 18b2d152..572c2daa 100644 --- a/Aria/build.gradle +++ b/Aria/build.gradle @@ -26,8 +26,8 @@ dependencies { testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.1.1' compile project(':AriaAnnotations') - compile 'com.arialyy.aria:aria-ftp-plug:1.0.3' +// compile 'com.arialyy.aria:aria-ftp-plug:1.0.3' - // compile project(':AriaFtpPlug') + compile project(':AriaFtpPlug') } apply from: 'bintray-release.gradle' diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/group/GroupCancelCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/group/GroupCancelCmd.java index 8c0c7c75..9205443d 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/group/GroupCancelCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/group/GroupCancelCmd.java @@ -19,7 +19,7 @@ import com.arialyy.aria.core.inf.AbsGroupTaskEntity; /** * Created by AriaL on 2017/6/29. - * 删除任务组 + * 删除任务组子任务 */ class GroupCancelCmd extends AbsGroupCmd { /** diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/normal/StartCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/normal/StartCmd.java index b3da691f..51bad424 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/normal/StartCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/normal/StartCmd.java @@ -86,8 +86,11 @@ class StartCmd extends AbsNormalCmd { } } } else { - if (!task.isRunning()) { + //任务没执行并且执行队列中没有该任务,才认为任务没有运行中 + if (!task.isRunning() && !mQueue.taskIsRunning(task.getKey())) { resumeTask(); + }else { + ALog.w(TAG, String.format("任务【%s】已经在运行", task.getTaskName())); } } if (mQueue.getCurrentCachePoolNum() == 0) { diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java b/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java index a8af4f7d..40ef2e0a 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java @@ -16,7 +16,6 @@ package com.arialyy.aria.core.common; import android.content.Context; -import android.util.SparseArray; import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.inf.AbsNormalEntity; @@ -29,8 +28,10 @@ import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.DbHelper; import java.io.File; import java.util.ArrayList; +import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Properties; import java.util.Set; import java.util.Timer; @@ -65,8 +66,7 @@ public abstract class AbsFileer mTask = new SparseArray<>(); - + private Map mTask = new HashMap<>(); private Timer mTimer; @Deprecated @@ -188,7 +188,7 @@ public abstract class AbsFileer 0 ? temp : 0; } else { tRecord.startLocation = 0; @@ -373,7 +373,7 @@ public abstract class AbsFileer= 0) { Long r = tr.startLocation; //记录的位置需要在线程区间中 @@ -447,7 +447,7 @@ public abstract class AbsFileer 0) { @@ -488,6 +488,26 @@ public abstract class AbsFileer keys = mTask.keySet(); + for (Integer key : keys) { + AbsThreadTask task = mTask.get(key); + if (task != null && !task.isThreadComplete()) { + task.getConfig().START_LOCATION = task.getCurrentLocation(); + ALog.d(TAG, String.format("任务【%s】开始重试,线程__%s__【开始位置:%s,结束位置:%s】", mEntity.getFileName(), + key, task.getConfig().START_LOCATION, task.getConfig().END_LOCATION)); + mFixedThreadPool.execute(task); + } + } + } + /** * 处理新任务 * @@ -496,7 +516,7 @@ public abstract class AbsFileer config = new SubThreadConfig<>(); 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 a8fabcd8..c2b649ec 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 @@ -26,6 +26,7 @@ import com.arialyy.aria.util.Regular; import java.io.File; import java.io.IOException; import java.net.InetAddress; +import java.net.UnknownHostException; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.commons.net.ftp.FTP; @@ -72,7 +73,7 @@ public abstract class AbsFtpInfoThread 0) { - ALog.i(TAG, "路径【" + setRemotePath() + "】下的文件列表 ==================================="); + ALog.i(TAG, + String.format("路径【%s】下的文件列表 ===================================", setRemotePath())); for (FTPFile file : files1) { ALog.d(TAG, file.toString()); } @@ -114,7 +116,8 @@ public abstract class AbsFtpInfoThread 0) { client = new FTPClient(); InetAddress ip = InetAddress.getByName(urlEntity.hostName); - client.setConnectTimeout(10000); // 连接10s超时 + client.setConnectTimeout(mConnectTimeOut); // 连接10s超时 client.connect(ip, Integer.parseInt(urlEntity.port)); mTaskEntity.getUrlEntity().validAddr = ip; } else { - InetAddress[] ips = InetAddress.getAllByName(urlEntity.hostName); + DNSQueryThread dnsThread = new DNSQueryThread(urlEntity.hostName); + dnsThread.start(); + dnsThread.join(mConnectTimeOut); + InetAddress[] ips = dnsThread.getIps(); client = connect(new FTPClient(), ips, 0, Integer.parseInt(urlEntity.port)); } @@ -206,7 +212,8 @@ public abstract class AbsFtpInfoThread mConfig; protected ENTITY mEntity; protected TASK_ENTITY mTaskEntity; - private int mFailNum = 0; - private String mTaskType; + private int mFailTimes = 0; private Timer mFailTimer; private long mLastSaveTime; private ExecutorService mConfigThreadPool; protected int mConnectTimeOut; //连接超时时间 protected int mReadTimeOut; //流读取的超时时间 protected boolean isNotNetRetry = false; //断网情况是否重试 - protected boolean taskBreak = false; //任务中断 + private boolean taskBreak = false; //任务中断 private Thread mConfigThread = new Thread(new Runnable() { @Override public void run() { @@ -76,13 +75,10 @@ public abstract class AbsThreadTask= Build.VERSION_CODES.LOLLIPOP; } @@ -124,21 +142,16 @@ public abstract class AbsThreadTask } else if (getTargetType() == FTP) { filePath += mEntity.getFileName(); } + } else { + // http文件名设置 + if (TextUtils.isEmpty(mEntity.getFileName())) { + mEntity.setFileName(file.getName()); + } } - mEntity.setFileName(file.getName()); //设置文件保存路径,如果新文件路径和就文件路径不同,则修改路径 if (!filePath.equals(mEntity.getDownloadPath())) { diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupListener.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupListener.java index 806d341f..48cb6167 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupListener.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupListener.java @@ -89,6 +89,7 @@ class DownloadGroupListener for (DownloadEntity e : mEntity.getSubEntities()) { location += e.getCurrentProgress(); } + mEntity.setCurrentProgress(location); mEntity.update(); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java index bb872563..dd148419 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java @@ -143,9 +143,13 @@ public class DownloadGroupTarget extends BaseGroupTarget { reChangeDirPath(mDirPathTemp); } - if (!mSubNameTemp.isEmpty()) { - updateSingleSubFileName(); + if (mSubNameTemp.isEmpty()) { + for (String url : mUrls) { + int lastIndex = url.lastIndexOf(File.separator); + mSubNameTemp.add(url.substring(lastIndex + 1, url.length())); + } } + updateSingleSubFileName(); return true; } return false; 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 8bc5bd04..f2ac361e 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 @@ -39,6 +39,7 @@ import java.util.concurrent.Executors; * 任务组核心逻辑 */ public abstract class AbsGroupUtil implements IUtil { + private static final Object LOCK = new Object(); private final String TAG = "AbsGroupUtil"; /** * FTP文件夹 @@ -158,6 +159,9 @@ public abstract class AbsGroupUtil implements IUtil { DownloadTaskEntity det = mTasksMap.get(url); if (det != null) { mTotalLen -= det.getEntity().getFileSize(); + mCurrentLocation -= det.getEntity().getCurrentProgress(); + mExeMap.remove(det.getKey()); + mFailMap.remove(det.getKey()); mGroupSize--; if (mGroupSize == 0) { closeTimer(false); @@ -305,12 +309,12 @@ public abstract class AbsGroupUtil implements IUtil { } - private void clearState(){ + private void clearState() { mDownloaderMap.clear(); mFailMap.clear(); } - private void closeTimer(boolean isRunning) { + void closeTimer(boolean isRunning) { this.isRunning = isRunning; if (mTimer != null) { mTimer.purge(); @@ -374,6 +378,8 @@ public abstract class AbsGroupUtil implements IUtil { private int RUN_SAVE_INTERVAL = 5 * 1000; //5s保存一次下载中的进度 private long lastSaveTime; private long lastLen = 0; + private Timer timer; + private boolean isNotNetRetry = false; ChildDownloadListener(DownloadTaskEntity entity) { subTaskEntity = entity; @@ -381,6 +387,7 @@ public abstract class AbsGroupUtil implements IUtil { subEntity.setFailNum(0); lastLen = subEntity.getCurrentProgress(); lastSaveTime = System.currentTimeMillis(); + isNotNetRetry = AriaManager.getInstance(AriaManager.APP).getDownloadConfig().isNotNetRetry(); } @Override public void onPre() { @@ -423,7 +430,7 @@ public abstract class AbsGroupUtil implements IUtil { saveData(IEntity.STATE_STOP, stopLocation); handleSpeed(0); mListener.onSubStop(subEntity); - synchronized (AbsGroupUtil.class) { + synchronized (AbsGroupUtil.LOCK) { mStopNum++; if (mStopNum + mCompleteNum + mFailMap.size() == mGroupSize) { closeTimer(false); @@ -439,16 +446,17 @@ public abstract class AbsGroupUtil implements IUtil { } @Override public void onComplete() { + subEntity.setComplete(true); saveData(IEntity.STATE_COMPLETE, subEntity.getFileSize()); handleSpeed(0); mListener.onSubComplete(subEntity); - synchronized (ChildDownloadListener.class) { + synchronized (AbsGroupUtil.LOCK) { mCompleteNum++; //如果子任务完成的数量和总任务数一致,表示任务组任务已经完成 - if (mCompleteNum >= mGroupSize) { + if (mCompleteNum == mGroupSize) { closeTimer(false); mListener.onComplete(); - } else if (mFailMap.size() > 0 && mStopNum + mCompleteNum + mFailMap.size() >= mGroupSize) { + } else if (mFailMap.size() > 0 && mStopNum + mCompleteNum + mFailMap.size() == mGroupSize) { //如果子任务完成数量加上失败的数量和总任务数一致,则任务组停止下载 closeTimer(false); mListener.onStop(mCurrentLocation); @@ -464,41 +472,48 @@ public abstract class AbsGroupUtil implements IUtil { } /** - * 重试下载 + * 重试下载,只有全部都下载失败才会执行任务组的整体重试,否则只会执行单个子任务的重试 */ private void reTry(boolean needRetry) { - synchronized (ChildDownloadListener.class) { - if (subEntity.getFailNum() < 5 && needRetry && NetUtils.isConnected(AriaManager.APP)) { + synchronized (AbsGroupUtil.LOCK) { + if (subEntity.getFailNum() < 5 + && needRetry && (NetUtils.isConnected(AriaManager.APP) || isNotNetRetry)) { reStartTask(); } else { mFailMap.put(subTaskEntity.getUrl(), subTaskEntity); mListener.onSubFail(subEntity); - //如果失败的任务数大于实际的下载任务数,任务组停止下载 - if (mFailMap.size() >= mExeMap.size()) { + if (mFailMap.size() == mExeMap.size() || mFailMap.size() + mCompleteNum == mGroupSize) { closeTimer(false); - if (mFailMap.size() == mGroupSize) { //所有任务都失败了,则认为该任务组已经失败 - mListener.onFail(true); - } else { - mListener.onStop(mCurrentLocation); - } + } + if (mFailMap.size() == mGroupSize) { + mListener.onFail(true); + } else if (mFailMap.size() + mCompleteNum >= mExeMap.size()) { + mListener.onStop(mCurrentLocation); } } } } private void reStartTask() { - Timer timer = new Timer(); + if (timer != null) { + timer.purge(); + timer.cancel(); + } + timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { Downloader dt = mDownloaderMap.get(subEntity.getUrl()); - dt.start(); + if (dt != null) { + dt.retryThradTask(); + } } }, 3000); } private void handleSpeed(long speed) { subEntity.setSpeed(speed); - subEntity.setConvertSpeed(speed <= 0 ? "" : CommonUtil.formatFileSize(speed) + "/s"); + subEntity.setConvertSpeed( + speed <= 0 ? "" : String.format("%s/s", CommonUtil.formatFileSize(speed))); subEntity.setPercent((int) (subEntity.getFileSize() <= 0 ? 0 : subEntity.getCurrentProgress() * 100 / subEntity.getFileSize())); } 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 ac9b1cea..ed7f8361 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 @@ -34,7 +34,14 @@ import java.util.concurrent.Executors; public class DownloadGroupUtil extends AbsGroupUtil implements IUtil { private final String TAG = "DownloadGroupUtil"; private ExecutorService mInfoPool; - private int mInitCompleteNum, mInitFailNum; + /** + * 初始化完成的任务数 + */ + private int mInitCompleteNum; + /** + * 初始化失败的任务数 + */ + private int mInitFailNum; private boolean isStop = false; /** @@ -126,7 +133,7 @@ public class DownloadGroupUtil extends AbsGroupUtil implements IUtil { @Override public void onFail(String url, String errorMsg, boolean needRetry) { if (isStop) return; - ALog.e(TAG, "任务【" + url + "】初始化失败。"); + ALog.e(TAG, String.format("任务【%s】初始化失败", url)); DownloadTaskEntity te = mExeMap.get(url); if (te != null) { mFailMap.put(url, te); @@ -154,7 +161,11 @@ public class DownloadGroupUtil extends AbsGroupUtil implements IUtil { */ private void checkStartFlow() { synchronized (DownloadGroupUtil.class) { - if (mInitCompleteNum + mInitFailNum >= mGroupSize || !isNeedLoadFileSize) { + if (mInitFailNum == mGroupSize){ + closeTimer(false); + mListener.onFail(true); + } + if (mInitCompleteNum + mInitFailNum == mGroupSize || !isNeedLoadFileSize) { startRunningFlow(); updateFileSize(); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpThreadTask.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpThreadTask.java index d9dc4b4c..547e2aa4 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpThreadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpThreadTask.java @@ -61,15 +61,9 @@ class FtpThreadTask extends AbsFtpThreadTask InputStream is = null; try { - ALog.d(TAG, "任务【" - + mConfig.TEMP_FILE.getName() - + "】线程__" - + mConfig.THREAD_ID - + "__开始下载【开始位置 : " - + mConfig.START_LOCATION - + ",结束位置:" - + mConfig.END_LOCATION - + "】"); + ALog.d(TAG, + String.format("任务【%s】线程__%s__开始下载【开始位置 : %s,结束位置:%s】", mConfig.TEMP_FILE.getName(), + mConfig.THREAD_ID, mConfig.START_LOCATION, mConfig.END_LOCATION)); client = createClient(); if (client == null) return; if (mConfig.START_LOCATION > 0) { @@ -78,18 +72,20 @@ class FtpThreadTask extends AbsFtpThreadTask //发送第二次指令时,还需要再做一次判断 int reply = client.getReplyCode(); if (!FTPReply.isPositivePreliminary(reply) && reply != FTPReply.COMMAND_OK) { - fail(mChildCurrentLocation, "获取文件信息错误,错误码为:" + reply + ",msg:" + client.getReplyString(), + fail(mChildCurrentLocation, + String.format("获取文件信息错误,错误码为:%s,msg:%s", reply, client.getReplyString()), null); client.disconnect(); return; } String remotePath = new String(mTaskEntity.getUrlEntity().remotePath.getBytes(charSet), SERVER_CHARSET); - ALog.i(TAG, "remotePath【" + remotePath + "】"); + ALog.i(TAG, String.format("remotePath【%s】", remotePath)); is = client.retrieveFileStream(remotePath); reply = client.getReplyCode(); if (!FTPReply.isPositivePreliminary(reply)) { - fail(mChildCurrentLocation, "获取流失败,错误码为:" + reply + ",msg:" + client.getReplyString(), + fail(mChildCurrentLocation, + String.format("获取流失败,错误码为:%s,msg:%s", reply, client.getReplyString()), null); client.disconnect(); return; @@ -101,11 +97,12 @@ class FtpThreadTask extends AbsFtpThreadTask readNormal(is); } - if (isBreak()){ + if (isBreak()) { return; } - ALog.i(TAG, "任务【" + mConfig.TEMP_FILE.getName() + "】线程__" + mConfig.THREAD_ID + "__下载完毕"); - writeConfig(true, 1); + ALog.i(TAG, + String.format("任务【%s】线程__%s__下载完毕", mConfig.TEMP_FILE.getName(), mConfig.THREAD_ID)); + writeConfig(true, mConfig.END_LOCATION); STATE.COMPLETE_THREAD_NUM++; if (STATE.isComplete()) { STATE.TASK_RECORD.deleteData(); @@ -117,7 +114,7 @@ class FtpThreadTask extends AbsFtpThreadTask mListener.onFail(false); } } catch (IOException e) { - fail(mChildCurrentLocation, "下载失败【" + mConfig.URL + "】", e); + fail(mChildCurrentLocation, String.format("下载失败【%s】", mConfig.URL), e); } catch (Exception e) { fail(mChildCurrentLocation, "获取流失败", e); } finally { @@ -162,7 +159,7 @@ class FtpThreadTask extends AbsFtpThreadTask } catch (InterruptedException e) { e.printStackTrace(); } catch (IOException e) { - fail(mChildCurrentLocation, "下载失败【" + mConfig.URL + "】", e); + fail(mChildCurrentLocation, String.format("下载失败【%s】", mConfig.URL), e); } finally { try { if (fos != null) { @@ -206,7 +203,7 @@ class FtpThreadTask extends AbsFtpThreadTask } } } catch (IOException e) { - fail(mChildCurrentLocation, "下载失败【" + mConfig.URL + "】", e); + fail(mChildCurrentLocation, String.format("下载失败【%s】", mConfig.URL), e); } catch (InterruptedException e) { e.printStackTrace(); } finally { @@ -219,8 +216,4 @@ class FtpThreadTask extends AbsFtpThreadTask } } } - - @Override protected String getTaskType() { - return "FTP_DOWNLOAD"; - } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java index 6e290f81..77419656 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java @@ -67,18 +67,12 @@ final class HttpThreadTask extends AbsThreadTask { FTPClient client = null; BufferedRandomAccessFile file = null; try { - ALog.d(TAG, "任务【" - + mConfig.TEMP_FILE.getName() - + "】线程__" - + mConfig.THREAD_ID - + "__开始上传【开始位置 : " - + mConfig.START_LOCATION - + ",结束位置:" - + mConfig.END_LOCATION - + "】"); + ALog.d(TAG, + String.format("任务【%s】线程__%s__开始上传【开始位置 : %s,结束位置:%s】", mConfig.TEMP_FILE.getName(), + mConfig.THREAD_ID, mConfig.START_LOCATION, mConfig.END_LOCATION)); client = createClient(); if (client == null) return; initPath(); @@ -85,8 +79,9 @@ class FtpThreadTask extends AbsFtpThreadTask { if (isBreak()) { return; } - ALog.i(TAG, "任务【" + mConfig.TEMP_FILE.getName() + "】线程__" + mConfig.THREAD_ID + "__上传完毕"); - writeConfig(true, 1); + ALog.i(TAG, + String.format("任务【%s】线程__%s__上传完毕", mConfig.TEMP_FILE.getName(), mConfig.THREAD_ID)); + writeConfig(true, mConfig.END_LOCATION); STATE.COMPLETE_THREAD_NUM++; if (STATE.isComplete()) { STATE.TASK_RECORD.deleteData(); @@ -98,7 +93,7 @@ class FtpThreadTask extends AbsFtpThreadTask { mListener.onFail(false); } } catch (IOException e) { - fail(mChildCurrentLocation, "上传失败【" + mConfig.URL + "】", e); + fail(mChildCurrentLocation, String.format("上传失败【%s】", mConfig.URL), e); } catch (Exception e) { fail(mChildCurrentLocation, "获取流失败", e); } finally { @@ -127,7 +122,7 @@ class FtpThreadTask extends AbsFtpThreadTask { throws IOException { try { - ALog.d(TAG, "remotePath【" + remotePath + "】"); + ALog.d(TAG, String.format("remotePath【%s】", remotePath)); client.storeFile(remotePath, new FtpFISAdapter(bis), new OnFtpInputStreamListener() { boolean isStoped = false; @@ -155,7 +150,8 @@ class FtpThreadTask extends AbsFtpThreadTask { int reply = client.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { if (reply != FTPReply.TRANSFER_ABORTED) { - fail(mChildCurrentLocation, "上传文件错误,错误码为:" + reply + ",msg:" + client.getReplyString(), + fail(mChildCurrentLocation, + String.format("上传文件错误,错误码为:%s,msg:%s", reply, client.getReplyString()), null); } if (client.isConnected()) { @@ -163,8 +159,4 @@ class FtpThreadTask extends AbsFtpThreadTask { } } } - - @Override protected String getTaskType() { - return "FTP_UPLOAD"; - } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/HttpThreadTask.java b/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/HttpThreadTask.java index e0c4c641..ac14c2a7 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/HttpThreadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/HttpThreadTask.java @@ -62,7 +62,7 @@ class HttpThreadTask extends AbsThreadTask { @Override public void run() { File uploadFile = new File(mEntity.getFilePath()); if (!uploadFile.exists()) { - ALog.e(TAG, "【" + mEntity.getFilePath() + "】,文件不存在。"); + ALog.e(TAG, String.format("【%s】,文件不存在。", mEntity.getFilePath())); fail(); return; } @@ -212,8 +212,4 @@ class HttpThreadTask extends AbsThreadTask { mOutputStream.close(); return response.toString(); } - - @Override protected String getTaskType() { - return "HTTP_UPLOAD"; - } } diff --git a/Aria/src/main/java/com/arialyy/aria/util/ALog.java b/Aria/src/main/java/com/arialyy/aria/util/ALog.java index d4b2dc96..5e5d4b7e 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/ALog.java +++ b/Aria/src/main/java/com/arialyy/aria/util/ALog.java @@ -117,6 +117,9 @@ public class ALog { * 将异常信息转换为字符串 */ public static String getExceptionString(Throwable ex) { + if (ex == null) { + return ""; + } StringBuilder err = new StringBuilder(); err.append("ExceptionDetailed:\n"); err.append("====================Exception Info====================\n"); diff --git a/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java b/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java index 122d115f..176ad2b6 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java @@ -246,53 +246,30 @@ public class CommonUtil { * @param url 输入的url{@code String url = "ftp://z:z@dygod18.com:21211/[电影天堂www.dy2018.com]猩球崛起3:终极之战BD国英双语中英双字.mkv";} */ public static FtpUrlEntity getFtpUrlInfo(String url) { + Uri uri = Uri.parse(url); + + String userInfo = uri.getUserInfo(), remotePath = uri.getPath(); + ALog.d(TAG, + String.format("scheme = %s, user = %s, host = %s, port = %s, path = %s", uri.getScheme(), + userInfo, uri.getHost(), uri.getPort(), remotePath)); + FtpUrlEntity entity = new FtpUrlEntity(); entity.url = url; - //String regex = "(\\w+)://(.*):(\\d*)/(.*)"; - String regex = Regular.REG_FTP_URL; - Pattern p = Pattern.compile(regex); - Matcher m = p.matcher(url); - if (m.find() && m.groupCount() > 0) { - entity.protocol = m.group(1); - String str = m.group(2); - if (str.contains("@")) { - entity.needLogin = true; - //String hostReg = "(\\w+):?(\\w+)?@(.*)"; - String hostReg = Regular.REG_FTP_HOST_NAME; - Pattern hp = Pattern.compile(hostReg); - Matcher hm = hp.matcher(str); - if (hm.find() && hm.groupCount() > 0) { - entity.user = hm.group(1); - entity.password = TextUtils.isEmpty(hm.group(2)) ? "" : hm.group(2); - entity.hostName = hm.group(3); - } + entity.hostName = uri.getHost(); + entity.port = uri.getPort() == -1 ? "21" : String.valueOf(uri.getPort()); + if (!TextUtils.isEmpty(userInfo)) { + String[] temp = userInfo.split(":"); + if (temp.length == 2) { + entity.user = temp[0]; + entity.password = temp[1]; } else { - entity.hostName = str; + entity.user = userInfo; } - entity.port = m.group(3); - //entity.remotePath = TextUtils.isEmpty(m.group(4)) ? "/" : "/" + m.group(4); - entity.remotePath = TextUtils.isEmpty(m.group(4)) ? "/" : m.group(4); } + entity.remotePath = TextUtils.isEmpty(remotePath) ? "/" : remotePath; return entity; } - /** - * 通过url获取FTP文件的remotePath - * - * @return remotePath。如果没有找到,返回"" - */ - public static String getRemotePath(String url) { - String remotePath = null; - String regex = Regular.REG_FTP_URL; - Pattern p = Pattern.compile(regex); - Matcher m = p.matcher(url); - if (m.find() && m.groupCount() > 0) { - return TextUtils.isEmpty(m.group(4)) ? "" : "/" + m.group(4); - } - ALog.w(TAG, "链接【" + url + "】没有找到remotePath"); - return ""; - } - /** * 转换Url * @@ -300,25 +277,27 @@ public class CommonUtil { * @return 转换后的地址 */ public static String convertUrl(String url) { - if (hasDoubleCharacter(url)) { - //预先处理空格,URLEncoder只会把空格转换为+ - url = url.replaceAll(" ", "%20"); - //匹配双字节字符(包括汉字在内) - String regex = Regular.REG_DOUBLE_CHAR_AND_SPACE; - Pattern p = Pattern.compile(regex); - Matcher m = p.matcher(url); - Set strs = new HashSet<>(); - while (m.find()) { - strs.add(m.group()); - } - try { - for (String str : strs) { - url = url.replaceAll(str, URLEncoder.encode(str, "UTF-8")); - } - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); - } - } + Uri uri = Uri.parse(url); + url = uri.toString(); + //if (hasDoubleCharacter(url)) { + // //预先处理空格,URLEncoder只会把空格转换为+ + // url = url.replaceAll(" ", "%20"); + // //匹配双字节字符(包括汉字在内) + // String regex = Regular.REG_DOUBLE_CHAR_AND_SPACE; + // Pattern p = Pattern.compile(regex); + // Matcher m = p.matcher(url); + // Set strs = new HashSet<>(); + // while (m.find()) { + // strs.add(m.group()); + // } + // try { + // for (String str : strs) { + // url = url.replaceAll(str, URLEncoder.encode(str, "UTF-8")); + // } + // } catch (UnsupportedEncodingException e) { + // e.printStackTrace(); + // } + //} return url; } diff --git a/Aria/src/main/java/com/arialyy/aria/util/Regular.java b/Aria/src/main/java/com/arialyy/aria/util/Regular.java index 829b5dda..ac364a1d 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/Regular.java +++ b/Aria/src/main/java/com/arialyy/aria/util/Regular.java @@ -5,15 +5,6 @@ package com.arialyy.aria.util; * 正则表达式 */ public interface Regular { - /** - * ftp地址 - */ - String REG_FTP_URL = "(\\w+)://(.*):(\\d*)/(.*)"; - - /** - * ftp主机、用户、密码分割 - */ - String REG_FTP_HOST_NAME = "(\\w+):?(\\w+)?@(.*)"; /** * 获取文件名 diff --git a/README.md b/README.md index 1558e602..e3380213 100644 --- a/README.md +++ b/README.md @@ -126,8 +126,7 @@ protected void onCreate(Bundle savedInstanceState) { ## 其他 有任何问题,可以在[issues](https://github.com/AriaLyy/Aria/issues)给我留言反馈。
- 在提交问题前,希望你已经查看过[wiki](https://github.com/AriaLyy/Aria/wiki)或搜索过[issues](https://github.com/AriaLyy/Aria/issues)。
- Aria交流群:524329160(加群条件:对aria有过star、commit、issues) + 在提交问题前,希望你已经查看过[wiki](https://aria.laoyuyu.me/aria_doc/)或搜索过[issues](https://github.com/AriaLyy/Aria/issues)。
*** License diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index a81e9694..d7856d59 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -14,8 +14,9 @@ android:supportsRtl="true" android:theme="@style/AppTheme.NoActionBar"> - + + @@ -33,7 +34,7 @@ - + diff --git a/app/src/main/assets/aria_config.xml b/app/src/main/assets/aria_config.xml index de9e2732..cb5efb86 100644 --- a/app/src/main/assets/aria_config.xml +++ b/app/src/main/assets/aria_config.xml @@ -22,7 +22,7 @@ 3、从3.4.1开始,如果线程数为1,文件初始化时将不再预占用对应长度的空间,下载多少byte,则占多大的空间; 对于采用多线程的任务或旧任务,依然采用原来的文件空间占用方式; --> - + diff --git a/app/src/main/java/com/arialyy/simple/download/group/DownloadGroupActivity.java b/app/src/main/java/com/arialyy/simple/download/group/DownloadGroupActivity.java index dc18256b..21b27176 100644 --- a/app/src/main/java/com/arialyy/simple/download/group/DownloadGroupActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/group/DownloadGroupActivity.java @@ -84,7 +84,7 @@ public class DownloadGroupActivity extends BaseActivity { String[] urls; int index = 0; //String URL = "http://static.gaoshouyou.com/d/12/0d/7f120f50c80d2e7b8c4ba24ece4f9cdd.apk"; - String URL = "http://58.213.157.242:8081/sims_file/rest/v1/file/mshd_touchscreen_ms/guideFile/41c33556-dc4a-4d78-bb76-b9f627f94448.mp4/%E5%85%AB%E5%8D%A6%E6%B4%B2%E5%8D%97%E4%BA%AC%E5%86%9C%E4%B8%9A%E5%98%89%E5%B9%B4%E5%8D%8E0511.mp4"; + //String URL = "http://58.213.157.242:8081/sims_file/rest/v1/file/mshd_touchscreen_ms/guideFile/41c33556-dc4a-4d78-bb76-b9f627f94448.mp4/%E5%85%AB%E5%8D%A6%E6%B4%B2%E5%8D%97%E4%BA%AC%E5%86%9C%E4%B8%9A%E5%98%89%E5%B9%B4%E5%8D%8E0511.mp4"; //String URL = "http://d1.showself.com/download/showself_android-s236279_release.apk"; //String URL = "http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk"; //private final String URL = "ftp://192.168.29.140:21/download/AriaPrj.rar"; //String URL = "https://dl.genymotion.com/releases/genymotion-2.12.1/genymotion-2.12.1-vbox.exe"; + //String URL = "ftp://192.168.29.140:21/download/SDK_Demo-release.apk"; + String URL = "ftp://z:z@dygod18.com:21211/[电影天堂www.dy2018.com]猩球崛起3:终极之战BD国英双语中英双字.mkv"; @Override protected int setLayoutId() { return R.layout.activity_test; @@ -49,8 +51,8 @@ public class AnyRunActivity extends BaseActivity { //} //for (int i = 0; i < 10; i++) { - //module.startFtp(URL); - module.start(URL); + module.startFtp(URL); + //module.start(URL); //} //List list = Aria.download(this).getTotalTaskList(); //ALog.d(TAG, "size ==> " + list.size()); diff --git a/app/src/main/java/com/arialyy/simple/test/TestActivity.java b/app/src/main/java/com/arialyy/simple/test/TestActivity.java index 760a1a9d..4c680de5 100644 --- a/app/src/main/java/com/arialyy/simple/test/TestActivity.java +++ b/app/src/main/java/com/arialyy/simple/test/TestActivity.java @@ -1,5 +1,6 @@ package com.arialyy.simple.test; +import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.view.View; @@ -7,6 +8,7 @@ import com.arialyy.annotations.Upload; import com.arialyy.aria.core.Aria; import com.arialyy.aria.core.common.RequestEnum; import com.arialyy.aria.core.upload.UploadTask; +import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; import com.arialyy.simple.R; import com.arialyy.simple.base.BaseActivity; @@ -22,8 +24,8 @@ public class TestActivity extends BaseActivity { //String URL = "http://58.210.9.131/tpk/sipgt//TDLYZTGH.tpk"; //chunked 下载 //private final String URL = "ftp://192.168.1.3:21/download//AriaPrj.rar"; private final String FILE_PATH = "/mnt/sdcard/AriaPrj.rar"; - //private final String URL = "ftp://192.168.29.140:21/aa//你好"; - private final String URL = "ftp://192.168.29.140:21/upload/"; + private final String URL = "ftp://192.168.1.2:21/aa//你好"; + @Override protected int setLayoutId() { return R.layout.activity_test; @@ -81,6 +83,8 @@ public class TestActivity extends BaseActivity { .setUploadUrl(URL) .setExtendField("韩寒哈大双") .start(); + //Uri uri = Uri.parse("ftp://z:z@dygod18.com:21211/[电影天堂www.dy2018.com]猩球崛起3:终极之战BD国英双语中英双字.mkv"); + //ALog.d(TAG, "sh = " + uri.getScheme() + ", user = " + uri.getUserInfo() + ", host = " + uri.getHost() + ", port = " + uri.getPort() + " remotePath = " + uri.getPath()); break; case R.id.stop: Aria.upload(this).loadFtp(FILE_PATH).stop(); diff --git a/aria/src/main/AndroidManifest.xml b/aria/src/main/AndroidManifest.xml index a50dd348..722c94c4 100644 --- a/aria/src/main/AndroidManifest.xml +++ b/aria/src/main/AndroidManifest.xml @@ -1,6 +1,7 @@ +