AriaLyy 7 years ago
parent f5422eb287
commit b7b55e42fc
  1. 44
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java
  2. 10
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java
  3. 5
      Aria/src/main/java/com/arialyy/aria/core/download/FtpDownloadTarget.java
  4. 4
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsFtpInfoThread.java
  5. 15
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/Downloader.java
  6. 2
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirInfoThread.java
  7. 8
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpFileInfoThread.java
  8. 8
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpFileInfoThread.java
  9. 4
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/SimpleDownloadUtil.java
  10. 10
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsTaskEntity.java
  11. 5
      app/src/main/assets/aria_config.xml
  12. 2
      app/src/main/java/com/arialyy/simple/download/FtpDownloadActivity.java
  13. 2
      app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java
  14. 2
      app/src/main/java/com/arialyy/simple/download/group/FTPDirDownloadActivity.java
  15. 2
      aria/src/main/java/com/arialyy/aria/core/Configuration.java

@ -16,16 +16,14 @@
package com.arialyy.aria.core.download; package com.arialyy.aria.core.download;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.command.normal.NormalCmdFactory;
import com.arialyy.aria.core.common.ProxyHelper;
import com.arialyy.aria.core.inf.AbsEntity; import com.arialyy.aria.core.inf.AbsEntity;
import com.arialyy.aria.core.inf.AbsReceiver; import com.arialyy.aria.core.inf.AbsReceiver;
import com.arialyy.aria.core.inf.IReceiver;
import com.arialyy.aria.core.command.normal.NormalCmdFactory;
import com.arialyy.aria.core.scheduler.DownloadGroupSchedulers; import com.arialyy.aria.core.scheduler.DownloadGroupSchedulers;
import com.arialyy.aria.core.scheduler.DownloadSchedulers; import com.arialyy.aria.core.scheduler.DownloadSchedulers;
import com.arialyy.aria.core.scheduler.ISchedulerListener; import com.arialyy.aria.core.scheduler.ISchedulerListener;
import com.arialyy.aria.core.common.ProxyHelper;
import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.util.CheckUtil; import com.arialyy.aria.util.CheckUtil;
import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.CommonUtil;
@ -53,17 +51,40 @@ public class DownloadReceiver extends AbsReceiver {
/** /**
* 使用下载实体执行下载操作 * 使用下载实体执行下载操作
*
* @param entity 下载实体
*/ */
public DownloadTarget load(DownloadEntity entity) { public DownloadTarget load(DownloadEntity entity) {
return new DownloadTarget(entity, targetName); return load(entity, false);
}
/**
* 使用下载实体执行下载操作
*
* @param refreshInfo 是否刷新下载信息
*/
public DownloadTarget load(DownloadEntity entity, boolean refreshInfo) {
return new DownloadTarget(entity, targetName, refreshInfo);
} }
/** /**
* 加载Httphttps单任务下载地址 * 加载Httphttps单任务下载地址
*
* @param url 下载地址
*/ */
public DownloadTarget load(@NonNull String url) { public DownloadTarget load(@NonNull String url) {
return load(url, false);
}
/**
* 加载Httphttps单任务下载地址
*
* @param url 下载地址
* @param refreshInfo 是否刷新下载信息
*/
public DownloadTarget load(@NonNull String url, boolean refreshInfo) {
CheckUtil.checkDownloadUrl(url); CheckUtil.checkDownloadUrl(url);
return new DownloadTarget(url, targetName); return new DownloadTarget(url, targetName, refreshInfo);
} }
/** /**
@ -78,8 +99,17 @@ public class DownloadReceiver extends AbsReceiver {
* 加载ftp单任务下载地址 * 加载ftp单任务下载地址
*/ */
public FtpDownloadTarget loadFtp(@NonNull String url) { public FtpDownloadTarget loadFtp(@NonNull String url) {
return loadFtp(url, false);
}
/**
* 加载ftp单任务下载地址
*
* @param refreshInfo 是否刷新下载信息
*/
public FtpDownloadTarget loadFtp(@NonNull String url, boolean refreshInfo) {
CheckUtil.checkDownloadUrl(url); CheckUtil.checkDownloadUrl(url);
return new FtpDownloadTarget(url, targetName); return new FtpDownloadTarget(url, targetName, refreshInfo);
} }
/** /**

@ -33,16 +33,26 @@ public class DownloadTarget
protected String url; protected String url;
DownloadTarget(DownloadEntity entity, String targetName) { DownloadTarget(DownloadEntity entity, String targetName) {
this(entity, targetName, false);
}
DownloadTarget(DownloadEntity entity, String targetName, boolean refreshInfo) {
this.url = entity.getUrl(); this.url = entity.getUrl();
mTargetName = targetName; mTargetName = targetName;
initTask(entity); initTask(entity);
mTaskEntity.refreshInfo = refreshInfo;
} }
DownloadTarget(String url, String targetName) { DownloadTarget(String url, String targetName) {
this(url, targetName, false);
}
DownloadTarget(String url, String targetName, boolean refreshInfo) {
this.url = url; this.url = url;
mTargetName = targetName; mTargetName = targetName;
DownloadEntity entity = getEntity(url); DownloadEntity entity = getEntity(url);
initTask(entity); initTask(entity);
mTaskEntity.refreshInfo = refreshInfo;
} }
private void initTask(DownloadEntity entity) { private void initTask(DownloadEntity entity) {

@ -32,6 +32,10 @@ public class FtpDownloadTarget extends DownloadTarget {
private int port; private int port;
FtpDownloadTarget(String url, String targetName) { FtpDownloadTarget(String url, String targetName) {
this(url, targetName, false);
}
FtpDownloadTarget(String url, String targetName, boolean refreshInfo) {
super(url, targetName); super(url, targetName);
String[] pp = url.split("/")[2].split(":"); String[] pp = url.split("/")[2].split(":");
this.serverIp = pp[0]; this.serverIp = pp[0];
@ -45,6 +49,7 @@ public class FtpDownloadTarget extends DownloadTarget {
mTaskEntity.serverIp = serverIp; mTaskEntity.serverIp = serverIp;
mTaskEntity.port = port; mTaskEntity.port = port;
mEntity.setFileName(url.substring(lastIndex + 1, url.length())); mEntity.setFileName(url.substring(lastIndex + 1, url.length()));
mTaskEntity.refreshInfo = refreshInfo;
} }
/** /**

@ -40,6 +40,7 @@ abstract class AbsFtpInfoThread<ENTITY extends AbsEntity, TASK_ENTITY extends Ab
protected TASK_ENTITY mTaskEntity; protected TASK_ENTITY mTaskEntity;
private int mConnectTimeOut; private int mConnectTimeOut;
private OnFileInfoCallback mCallback; private OnFileInfoCallback mCallback;
protected long mSize = 0;
AbsFtpInfoThread(TASK_ENTITY taskEntity, OnFileInfoCallback callback) { AbsFtpInfoThread(TASK_ENTITY taskEntity, OnFileInfoCallback callback) {
mTaskEntity = taskEntity; mTaskEntity = taskEntity;
@ -82,8 +83,7 @@ abstract class AbsFtpInfoThread<ENTITY extends AbsEntity, TASK_ENTITY extends Ab
client.setFileType(FTP.BINARY_FILE_TYPE); client.setFileType(FTP.BINARY_FILE_TYPE);
FTPFile[] files = FTPFile[] files =
client.listFiles(new String(remotePath.getBytes(charSet), AbsThreadTask.SERVER_CHARSET)); client.listFiles(new String(remotePath.getBytes(charSet), AbsThreadTask.SERVER_CHARSET));
long size = getFileSize(files, client, remotePath); mSize = getFileSize(files, client, remotePath);
mEntity.setFileSize(size);
reply = client.getReplyCode(); reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) { if (!FTPReply.isPositiveCompletion(reply)) {
client.disconnect(); client.disconnect();

@ -40,22 +40,25 @@ class Downloader extends AbsFileer<DownloadEntity, DownloadTaskEntity> {
} }
@Override protected void checkTask() { @Override protected void checkTask() {
if (!mTaskEntity.isSupportBP) {
isNewTask = true;
return;
}
mConfigFile = new File(mContext.getFilesDir().getPath() mConfigFile = new File(mContext.getFilesDir().getPath()
+ AriaManager.DOWNLOAD_TEMP_DIR + AriaManager.DOWNLOAD_TEMP_DIR
+ mEntity.getFileName() + mEntity.getFileName()
+ ".properties"); + ".properties");
mTempFile = new File(mEntity.getDownloadPath()); mTempFile = new File(mEntity.getDownloadPath());
if (!mTaskEntity.isSupportBP) {
isNewTask = true;
return;
}
if (mTaskEntity.isNewTask) {
isNewTask = true;
return;
}
if (!mConfigFile.exists()) { //记录文件被删除,则重新下载 if (!mConfigFile.exists()) { //记录文件被删除,则重新下载
isNewTask = true; isNewTask = true;
CommonUtil.createFile(mConfigFile.getPath()); CommonUtil.createFile(mConfigFile.getPath());
} else if (!mTempFile.exists()) { } else if (!mTempFile.exists()) {
isNewTask = true; isNewTask = true;
} else if (DbEntity.findFirst(DownloadEntity.class, "url=?", mEntity.getDownloadUrl()) } else if (DbEntity.findFirst(DownloadEntity.class, "url=?", mEntity.getUrl()) == null) {
== null) {
isNewTask = true; isNewTask = true;
} else { } else {
isNewTask = checkConfigFile(); isNewTask = checkConfigFile();

@ -28,7 +28,6 @@ import org.apache.commons.net.ftp.FTPFile;
* 获取ftp文件夹信息 * 获取ftp文件夹信息
*/ */
class FtpDirInfoThread extends AbsFtpInfoThread<DownloadGroupEntity, DownloadGroupTaskEntity> { class FtpDirInfoThread extends AbsFtpInfoThread<DownloadGroupEntity, DownloadGroupTaskEntity> {
private long mSize = 0;
FtpDirInfoThread(DownloadGroupTaskEntity taskEntity, OnFileInfoCallback callback) { FtpDirInfoThread(DownloadGroupTaskEntity taskEntity, OnFileInfoCallback callback) {
super(taskEntity, callback); super(taskEntity, callback);
@ -36,7 +35,6 @@ class FtpDirInfoThread extends AbsFtpInfoThread<DownloadGroupEntity, DownloadGro
@Override void handleFile(String remotePath, FTPFile ftpFile) { @Override void handleFile(String remotePath, FTPFile ftpFile) {
super.handleFile(remotePath, ftpFile); super.handleFile(remotePath, ftpFile);
mSize += ftpFile.getSize();
addEntity(remotePath, ftpFile); addEntity(remotePath, ftpFile);
} }

@ -27,4 +27,12 @@ class FtpFileInfoThread extends AbsFtpInfoThread<DownloadEntity, DownloadTaskEnt
FtpFileInfoThread(DownloadTaskEntity taskEntity, OnFileInfoCallback callback) { FtpFileInfoThread(DownloadTaskEntity taskEntity, OnFileInfoCallback callback) {
super(taskEntity, callback); super(taskEntity, callback);
} }
@Override protected void onPreComplete() {
super.onPreComplete();
if (mSize != mTaskEntity.getEntity().getFileSize()) {
mTaskEntity.isNewTask = true;
}
mEntity.setFileSize(mSize);
}
} }

@ -25,7 +25,6 @@ import java.io.IOException;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.net.URLEncoder;
/** /**
* 下载文件信息获取 * 下载文件信息获取
@ -147,12 +146,15 @@ class HttpFileInfoThread implements Runnable {
} }
/** /**
* 检查长度是否合法 * 检查长度是否合法并且检查新获取的文件长度是否和数据库的文件长度一直如果不一致则表示该任务为新任务
* *
* @param len 从服务器获取的文件长度 * @param len 从服务器获取的文件长度
* @return true, 合法 * @return {@code true}合法
*/ */
private boolean checkLen(long len) { private boolean checkLen(long len) {
if (len != mEntity.getFileSize()) {
mTaskEntity.isNewTask = true;
}
if (len < 0) { if (len < 0) {
failDownload("任务【" + mEntity.getUrl() + "】下载失败,文件长度小于0"); failDownload("任务【" + mEntity.getUrl() + "】下载失败,文件长度小于0");
return false; return false;

@ -23,7 +23,7 @@ import com.arialyy.aria.core.inf.IDownloadListener;
/** /**
* Created by lyy on 2015/8/25. * Created by lyy on 2015/8/25.
* HTTP单任务下载工具 * HTTP\FTP单任务下载工具
*/ */
public class SimpleDownloadUtil implements IUtil, Runnable { public class SimpleDownloadUtil implements IUtil, Runnable {
private static final String TAG = "SimpleDownloadUtil"; private static final String TAG = "SimpleDownloadUtil";
@ -87,7 +87,7 @@ public class SimpleDownloadUtil implements IUtil, Runnable {
@Override public void run() { @Override public void run() {
mListener.onPre(); mListener.onPre();
if (mTaskEntity.getEntity().getFileSize() <= 1) { if (mTaskEntity.getEntity().getFileSize() <= 1 || mTaskEntity.refreshInfo) {
new Thread(createInfoThread()).start(); new Thread(createInfoThread()).start();
} else { } else {
mDownloader.start(); mDownloader.start();

@ -50,6 +50,16 @@ public abstract class AbsTaskEntity<ENTITY extends AbsEntity> extends DbEntity {
@Ignore public String userName, userPw, account, serverIp; @Ignore public String userName, userPw, account, serverIp;
@Ignore public int port; @Ignore public int port;
/**
* 刷新信息 {@code true} 重新刷新下载信息
*/
@Ignore public boolean refreshInfo = false;
/**
* 是否是新任务{@code true} 新任务
*/
@Ignore public boolean isNewTask = false;
/** /**
* 请求类型 * 请求类型
* {@link AbsTaskEntity#HTTP}{@link AbsTaskEntity#FTP} * {@link AbsTaskEntity#HTTP}{@link AbsTaskEntity#FTP}

@ -31,7 +31,7 @@
<!--是否需要转换速度单位,转换完成后为:1b/s、1kb/s、1mb/s、1gb/s、1tb/s,如果不需要将返回byte长度--> <!--是否需要转换速度单位,转换完成后为:1b/s、1kb/s、1mb/s、1gb/s、1tb/s,如果不需要将返回byte长度-->
<convertSpeed value="true"/> <convertSpeed value="true"/>
<!--执行队列类型,见com.arialyy.aria.core.QueueMod,默认类型为now--> <!--执行队列类型,见com.arialyy.aria.core.QueueMod,默认类型为wait-->
<queueMod value="wait"/> <queueMod value="wait"/>
</download> </download>
@ -51,6 +51,9 @@
<!--设置url连接超时时间,单位为毫秒,默认5000毫秒--> <!--设置url连接超时时间,单位为毫秒,默认5000毫秒-->
<connectTimeOut value="5000"/> <connectTimeOut value="5000"/>
<!--执行队列类型,见com.arialyy.aria.core.QueueMod,默认类型为wait-->
<queueMod value="wait"/>
</upload> </upload>
</aria> </aria>

@ -59,7 +59,7 @@ public class FtpDownloadActivity extends BaseActivity<ActivityFtpDownloadBinding
switch (view.getId()) { switch (view.getId()) {
case R.id.start: case R.id.start:
Aria.download(this) Aria.download(this)
.loadFtp(URL) .loadFtp(URL, true)
.login("lao", "123456") .login("lao", "123456")
.setDownloadPath("/mnt/sdcard/") .setDownloadPath("/mnt/sdcard/")
.start(); .start();

@ -187,7 +187,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
switch (view.getId()) { switch (view.getId()) {
case R.id.start: case R.id.start:
Aria.download(SingleTaskActivity.this) Aria.download(SingleTaskActivity.this)
.load(DOWNLOAD_URL) .load(DOWNLOAD_URL, true)
.addHeader("groupName", "value") .addHeader("groupName", "value")
.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/gggg.apk") .setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/gggg.apk")
.start(); .start();

@ -35,7 +35,7 @@ import com.arialyy.simple.widget.SubStateLinearLayout;
* Created by Aria.Lao on 2017/7/6. * Created by Aria.Lao on 2017/7/6.
*/ */
public class FTPDirDownloadActivity extends BaseActivity<ActivityDownloadGroupBinding> { public class FTPDirDownloadActivity extends BaseActivity<ActivityDownloadGroupBinding> {
private static final String dir = "ftp://172.18.104.129:21/haha/"; private static final String dir = "ftp://172.18.104.66:21/haha/";
@Bind(R.id.child_list) SubStateLinearLayout mChildList; @Bind(R.id.child_list) SubStateLinearLayout mChildList;

@ -70,7 +70,7 @@ class Configuration {
* *
* @see QueueMod * @see QueueMod
*/ */
String queueMod = "now"; String queueMod = "wait";
public String getQueueMod() { public String getQueueMod() {
return queueMod; return queueMod;

Loading…
Cancel
Save