修复无法自动识别含有账号的ftpurl地址 https://github.com/AriaLyy/Aria/issues/146

pull/330/head
AriaLyy 7 years ago
parent fd57d3ffe9
commit 29bcf0aa15
  1. 4
      Aria/build.gradle
  2. 53
      Aria/src/main/java/com/arialyy/aria/core/FtpUrlEntity.java
  3. 160
      Aria/src/main/java/com/arialyy/aria/core/common/AbsFtpInfoThread.java
  4. 26
      Aria/src/main/java/com/arialyy/aria/core/common/AbsFtpThreadTask.java
  5. 2
      Aria/src/main/java/com/arialyy/aria/core/common/OnFileInfoCallback.java
  6. 20
      Aria/src/main/java/com/arialyy/aria/core/download/FtpDirDownloadTarget.java
  7. 21
      Aria/src/main/java/com/arialyy/aria/core/download/FtpDownloadTarget.java
  8. 8
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java
  9. 2
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java
  10. 4
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirDownloadUtil.java
  11. 4
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirInfoThread.java
  12. 3
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpFileInfoThread.java
  13. 6
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpThreadTask.java
  14. 14
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpFileInfoThread.java
  15. 12
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/SimpleDownloadUtil.java
  16. 6
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsTaskEntity.java
  17. 4
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsUploadTarget.java
  18. 31
      Aria/src/main/java/com/arialyy/aria/core/upload/FtpUploadTarget.java
  19. 4
      Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpFileInfoThread.java
  20. 4
      Aria/src/main/java/com/arialyy/aria/core/upload/uploader/SimpleUploadUtil.java
  21. 38
      Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java
  22. 28
      Aria/src/main/java/com/arialyy/aria/util/Regular.java
  23. 14
      app/build.gradle
  24. 1
      app/src/main/AndroidManifest.xml
  25. 2
      app/src/main/assets/aria_config.xml
  26. 6
      app/src/main/java/com/arialyy/simple/MainActivity.java
  27. 13
      app/src/main/java/com/arialyy/simple/download/FtpDownloadActivity.java
  28. 73
      app/src/main/java/com/arialyy/simple/download/KotlinDownloadActivity.kt
  29. 7
      app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java
  30. 8
      app/src/main/res/layout/activity_main.xml
  31. 2
      build.gradle

@ -23,8 +23,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'

@ -0,0 +1,53 @@
package com.arialyy.aria.core;
import java.net.InetAddress;
/**
* Created by Aria.Lao on 2017/10/24.
* ftp url 信息链接实体
*/
public class FtpUrlEntity {
public String remotePath;
public String account;
/**
* 原始url
*/
public String url;
/**
* ftp协议
*/
public String protocol;
/**
* 用户
*/
public String user;
/**
* 密码
*/
public String password;
/**
* 端口
*/
public String port;
/**
* 主机域名
*/
public String hostName;
/**
* 是否需要登录
*/
public boolean needLogin = false;
/**
* 有效的ip地址
*/
public InetAddress validAddr;
}

@ -18,12 +18,15 @@ package com.arialyy.aria.core.common;
import android.text.TextUtils;
import android.util.Log;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.download.DownloadGroupEntity;
import com.arialyy.aria.core.FtpUrlEntity;
import com.arialyy.aria.core.inf.AbsEntity;
import com.arialyy.aria.core.inf.AbsTaskEntity;
import com.arialyy.aria.core.upload.UploadEntity;
import com.arialyy.aria.util.Regular;
import java.io.IOException;
import java.net.InetAddress;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
@ -42,7 +45,6 @@ public abstract class AbsFtpInfoThread<ENTITY extends AbsEntity, TASK_ENTITY ext
private int mConnectTimeOut;
protected OnFileInfoCallback mCallback;
protected long mSize = 0;
protected String mServerIp, mPort;
protected String charSet = "UTF-8";
private boolean isUpload = false;
@ -71,7 +73,18 @@ public abstract class AbsFtpInfoThread<ENTITY extends AbsEntity, TASK_ENTITY ext
if (client == null) return;
String remotePath =
new String(setRemotePath().getBytes(charSet), AbsFtpThreadTask.SERVER_CHARSET);
FTPFile[] files_1 = client.listFiles();
for (FTPFile tf : files_1) {
Log.d(TAG, tf.getName());
}
FTPFile[] files = client.listFiles(remotePath);
boolean isExist = files.length != 0;
if (!isExist && !isUpload) {
client.disconnect();
failDownload("文件不存在,任务链接【" + mTaskEntity.urlEntity.url + "】", false);
return;
}
mSize = getFileSize(files, client, remotePath);
int reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
@ -80,7 +93,7 @@ public abstract class AbsFtpInfoThread<ENTITY extends AbsEntity, TASK_ENTITY ext
mTaskEntity.isNewTask = true;
} else {
client.disconnect();
failDownload("获取文件信息错误,错误码为:" + reply);
failDownload("获取文件信息错误,错误码为:" + reply, true);
return;
}
}
@ -91,7 +104,7 @@ public abstract class AbsFtpInfoThread<ENTITY extends AbsEntity, TASK_ENTITY ext
mTaskEntity.update();
onPreComplete(reply);
} catch (IOException e) {
failDownload(e.getMessage());
failDownload(e.getMessage(), true);
} finally {
if (client != null) {
try {
@ -103,6 +116,20 @@ public abstract class AbsFtpInfoThread<ENTITY extends AbsEntity, TASK_ENTITY ext
}
}
/**
* 检查文件是否存在
*
* @return {@code true}存在
*/
private boolean checkFileExist(FTPFile[] ftpFiles, String fileName) {
for (FTPFile ff : ftpFiles){
if (ff.getName().equals(fileName)){
return true;
}
}
return false;
}
public void start() {
new Thread(this).start();
}
@ -114,50 +141,89 @@ public abstract class AbsFtpInfoThread<ENTITY extends AbsEntity, TASK_ENTITY ext
/**
* 创建FTP客户端
*/
private FTPClient createFtpClient() throws IOException {
String url = "";
if (mEntity instanceof DownloadEntity) {
url = ((DownloadEntity) mEntity).getUrl();
} else if (mEntity instanceof UploadEntity) {
url = ((UploadEntity) mEntity).getUrl();
} else if (mEntity instanceof DownloadGroupEntity) {
url = mEntity.getKey();
} else {
failDownload("未知实体");
Log.e(TAG, "未知实体");
return null;
}
String[] pp = url.split("/")[2].split(":");
mServerIp = pp[0];
mPort = pp[1];
FTPClient client = new FTPClient();
// 连接服务器
client.connect(mServerIp, Integer.parseInt(mPort));
if (!TextUtils.isEmpty(mTaskEntity.account)) {
client.login(mTaskEntity.userName, mTaskEntity.userPw);
} else {
client.login(mTaskEntity.userName, mTaskEntity.userPw, mTaskEntity.account);
}
int reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
client.disconnect();
failDownload("无法连接到ftp服务器,错误码为:" + reply);
return null;
}
// 开启服务器对UTF-8的支持,如果服务器支持就用UTF-8编码
charSet = "UTF-8";
if (!TextUtils.isEmpty(mTaskEntity.charSet) || !FTPReply.isPositiveCompletion(
client.sendCommand("OPTS UTF8", "ON"))) {
charSet = mTaskEntity.charSet;
private FTPClient createFtpClient() {
FTPClient client = null;
final FtpUrlEntity urlEntity = mTaskEntity.urlEntity;
try {
Pattern p = Pattern.compile(Regular.REG_IP_V4);
Matcher m = p.matcher(urlEntity.hostName);
if (m.find() && m.groupCount() > 0) {
client = new FTPClient();
InetAddress ip = InetAddress.getByName(urlEntity.hostName);
client.connect(ip, Integer.parseInt(urlEntity.port));
mTaskEntity.urlEntity.validAddr = ip;
} else {
InetAddress[] ips = InetAddress.getAllByName(urlEntity.hostName);
client = connect(new FTPClient(), ips, 0, Integer.parseInt(urlEntity.port));
}
if (client == null) {
failDownload("链接失败", false);
return null;
}
boolean loginSuccess = true;
if (urlEntity.needLogin) {
if (TextUtils.isEmpty(urlEntity.account)) {
loginSuccess = client.login(urlEntity.user, urlEntity.password);
} else {
loginSuccess = client.login(urlEntity.user, urlEntity.password, urlEntity.account);
}
}
if (!loginSuccess) {
failDownload("登录失败", false);
return null;
}
int reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
client.disconnect();
failDownload("无法连接到ftp服务器,错误码为:" + reply, true);
return null;
}
// 开启服务器对UTF-8的支持,如果服务器支持就用UTF-8编码
charSet = "UTF-8";
if (!TextUtils.isEmpty(mTaskEntity.charSet) || !FTPReply.isPositiveCompletion(
client.sendCommand("OPTS UTF8", "ON"))) {
Log.d(TAG, "FTP 服务器不支持开启UTF8编码,尝试使用Aria手动设置的编码");
charSet = mTaskEntity.charSet;
}
client.setControlEncoding(charSet);
client.setDataTimeout(10 * 1000);
client.enterLocalPassiveMode();
client.setFileType(FTP.BINARY_FILE_TYPE);
client.setControlKeepAliveTimeout(5);
} catch (IOException e) {
e.printStackTrace();
}
client.setControlEncoding(charSet);
client.setDataTimeout(10 * 1000);
client.enterLocalPassiveMode();
client.setFileType(FTP.BINARY_FILE_TYPE);
client.setControlKeepAliveTimeout(5);
return client;
}
/**
* 连接到ftp服务器
*/
private FTPClient connect(FTPClient client, InetAddress[] ips, int index, int port) {
try {
client.connect(ips[index], port);
mTaskEntity.urlEntity.validAddr = ips[index];
return client;
} catch (IOException e) {
//e.printStackTrace();
if (index + 1 >= ips.length) {
Log.e(TAG, "遇到[ECONNREFUSED-连接被服务器拒绝]错误,已没有其他地址,链接失败");
return null;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
Log.e(TAG, "遇到[ECONNREFUSED-连接被服务器拒绝]错误,正在尝试下一个地址");
return connect(new FTPClient(), ips, index + 1, port);
}
}
/**
* 遍历FTP服务器上对应文件或文件夹大小
*
@ -188,10 +254,10 @@ public abstract class AbsFtpInfoThread<ENTITY extends AbsEntity, TASK_ENTITY ext
protected void handleFile(String remotePath, FTPFile ftpFile) {
}
private void failDownload(String errorMsg) {
private void failDownload(String errorMsg, boolean needRetry) {
Log.e(TAG, errorMsg);
if (mCallback != null) {
mCallback.onFail(mEntity.getKey(), errorMsg);
mCallback.onFail(mEntity.getKey(), errorMsg, needRetry);
}
}
}

@ -16,9 +16,11 @@
package com.arialyy.aria.core.common;
import android.text.TextUtils;
import com.arialyy.aria.core.FtpUrlEntity;
import com.arialyy.aria.core.inf.AbsNormalEntity;
import com.arialyy.aria.core.inf.AbsTaskEntity;
import com.arialyy.aria.core.inf.IEventListener;
import com.arialyy.aria.util.CommonUtil;
import java.io.IOException;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
@ -45,18 +47,18 @@ public abstract class AbsFtpThreadTask<ENTITY extends AbsNormalEntity, TASK_ENTI
* 构建FTP客户端
*/
protected FTPClient createClient() throws IOException {
String url = mEntity.getUrl();
String[] pp = url.split("/")[2].split(":");
serverIp = pp[0];
port = pp[1];
FTPClient client = new FTPClient();
// 连接服务器
client.connect(serverIp, Integer.parseInt(port));
if (!TextUtils.isEmpty(mTaskEntity.account)) {
client.login(mTaskEntity.userName, mTaskEntity.userPw);
} else {
client.login(mTaskEntity.userName, mTaskEntity.userPw, mTaskEntity.account);
final FtpUrlEntity urlEntity = mTaskEntity.urlEntity;
client.connect(urlEntity.validAddr, Integer.parseInt(urlEntity.port));
if (urlEntity.needLogin) {
if (TextUtils.isEmpty(urlEntity.account)) {
client.login(urlEntity.user, urlEntity.password);
} else {
client.login(urlEntity.user, urlEntity.password, urlEntity.account);
}
}
int reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
client.disconnect();
@ -70,12 +72,10 @@ public abstract class AbsFtpThreadTask<ENTITY extends AbsNormalEntity, TASK_ENTI
charSet = mTaskEntity.charSet;
}
client.setControlEncoding(charSet);
client.setDataTimeout(STATE.READ_TIME_OUT);
client.setDataTimeout(10 * 1000);
client.enterLocalPassiveMode();
client.setFileType(FTP.BINARY_FILE_TYPE);
client.setBufferSize(mBufSize);
client.setControlKeepAliveTimeout(5);
//client.setCopyStreamListener();
return client;
}
}

@ -13,5 +13,5 @@ public interface OnFileInfoCallback {
*
* @param errorMsg 错误信息
*/
void onFail(String url, String errorMsg);
void onFail(String url, String errorMsg, boolean needRetry);
}

@ -19,6 +19,7 @@ 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.CommonUtil;
/**
* Created by Aria.Lao on 2017/7/26.
@ -27,22 +28,12 @@ import com.arialyy.aria.orm.DbEntity;
public class FtpDirDownloadTarget
extends BaseGroupTarget<FtpDirDownloadTarget, DownloadGroupTaskEntity> {
private final String TAG = "FtpDirDownloadTarget";
private String serverIp, remotePath;
private int port;
FtpDirDownloadTarget(String url, String targetName) {
init(url);
String[] pp = url.split("/")[2].split(":");
mTargetName = targetName;
serverIp = pp[0];
port = Integer.parseInt(pp[1]);
mTaskEntity.urlEntity = CommonUtil.getFtpUrlInfo(url);
mTaskEntity.requestType = AbsTaskEntity.FTP_DIR;
mTaskEntity.serverIp = serverIp;
mTaskEntity.port = port;
remotePath = url.substring(url.indexOf(pp[1]) + pp[1].length(), url.length());
if (TextUtils.isEmpty(remotePath)) {
throw new NullPointerException("ftp服务器地址不能为null");
}
}
private void init(String key) {
@ -92,9 +83,10 @@ public class FtpDirDownloadTarget
Log.e(TAG, "密码不能为null");
return this;
}
mTaskEntity.userName = userName;
mTaskEntity.userPw = password;
mTaskEntity.account = account;
mTaskEntity.urlEntity.needLogin = true;
mTaskEntity.urlEntity.user = userName;
mTaskEntity.urlEntity.password = password;
mTaskEntity.urlEntity.account = account;
return this;
}
}

@ -28,8 +28,6 @@ import java.io.File;
*/
public class FtpDownloadTarget extends DownloadTarget {
private final String TAG = "FtpDownloadTarget";
private String serverIp, remotePath;
private int port;
FtpDownloadTarget(String url, String targetName) {
this(url, targetName, false);
@ -37,19 +35,11 @@ public class FtpDownloadTarget extends DownloadTarget {
FtpDownloadTarget(String url, String targetName, boolean refreshInfo) {
super(url, targetName);
String[] pp = url.split("/")[2].split(":");
this.serverIp = pp[0];
this.port = Integer.parseInt(pp[1]);
mTaskEntity.requestType = AbsTaskEntity.FTP;
remotePath = url.substring(url.indexOf(pp[1]) + pp[1].length(), url.length());
if (TextUtils.isEmpty(remotePath)) {
throw new NullPointerException("ftp服务器地址不能为null");
}
int lastIndex = url.lastIndexOf("/");
mTaskEntity.serverIp = serverIp;
mTaskEntity.port = port;
mEntity.setFileName(url.substring(lastIndex + 1, url.length()));
mTaskEntity.urlEntity = CommonUtil.getFtpUrlInfo(url);
mTaskEntity.refreshInfo = refreshInfo;
mTaskEntity.requestType = AbsTaskEntity.FTP;
}
/**
@ -116,9 +106,10 @@ public class FtpDownloadTarget extends DownloadTarget {
Log.e(TAG, "密码不能为null");
return this;
}
mTaskEntity.userName = userName;
mTaskEntity.userPw = password;
mTaskEntity.account = account;
mTaskEntity.urlEntity.needLogin = true;
mTaskEntity.urlEntity.user = userName;
mTaskEntity.urlEntity.password = password;
mTaskEntity.urlEntity.account = account;
return this;
}
}

@ -387,9 +387,7 @@ public abstract class AbsGroupUtil implements IUtil {
if (taskEntity != null) {
taskEntity.entity = entity;
//ftp登录的
taskEntity.userName = mTaskEntity.userName;
taskEntity.userPw = mTaskEntity.userPw;
taskEntity.account = mTaskEntity.account;
taskEntity.urlEntity = mTaskEntity.urlEntity;
mTasksMap.put(entity.getUrl(), taskEntity);
return taskEntity;
}
@ -403,9 +401,7 @@ public abstract class AbsGroupUtil implements IUtil {
taskEntity.isGroupTask = true;
taskEntity.requestType = mTaskEntity.requestType;
//ftp登录的
taskEntity.userName = mTaskEntity.userName;
taskEntity.userPw = mTaskEntity.userPw;
taskEntity.account = mTaskEntity.account;
taskEntity.urlEntity = mTaskEntity.urlEntity;
taskEntity.key = entity.getDownloadPath();
taskEntity.save();
mTasksMap.put(entity.getUrl(), taskEntity);

@ -105,7 +105,7 @@ public class DownloadGroupUtil extends AbsGroupUtil implements IUtil {
}
}
@Override public void onFail(String url, String errorMsg) {
@Override public void onFail(String url, String errorMsg, boolean needRetry) {
DownloadTaskEntity te = mExeMap.get(url);
if (te != null) {
mFailMap.put(url, te);

@ -49,8 +49,8 @@ public class FtpDirDownloadUtil extends AbsGroupUtil {
}
}
@Override public void onFail(String url, String errorMsg) {
mListener.onFail(true);
@Override public void onFail(String url, String errorMsg, boolean needRetry) {
mListener.onFail(needRetry);
ErrorHelp.saveError("FTP_DIR", mTaskEntity.getEntity(), "", errorMsg);
}
}).start();

@ -37,7 +37,7 @@ class FtpDirInfoThread extends AbsFtpInfoThread<DownloadGroupEntity, DownloadGro
@Override protected String setRemotePath() {
String url = mEntity.getKey();
return url.substring(url.indexOf(mPort) + mPort.length(), url.length());
return mTaskEntity.urlEntity.remotePath;
}
@Override protected void handleFile(String remotePath, FTPFile ftpFile) {
@ -53,7 +53,7 @@ class FtpDirInfoThread extends AbsFtpInfoThread<DownloadGroupEntity, DownloadGro
private void addEntity(String remotePath, FTPFile ftpFile) {
DownloadEntity entity = new DownloadEntity();
entity.setUrl("ftp://" + mTaskEntity.serverIp + ":" + mTaskEntity.port + remotePath);
entity.setUrl(mTaskEntity.urlEntity.url);
entity.setDownloadPath(mEntity.getDirPath() + "/" + remotePath);
int lastIndex = remotePath.lastIndexOf("/");
String fileName = lastIndex < 0 ? CommonUtil.keyToHashKey(remotePath)

@ -31,8 +31,7 @@ class FtpFileInfoThread extends AbsFtpInfoThread<DownloadEntity, DownloadTaskEnt
}
@Override protected String setRemotePath() {
String url = mEntity.getUrl();
return url.substring(url.indexOf(mPort) + mPort.length(), url.length());
return mTaskEntity.urlEntity.remotePath;
}
@Override protected void onPreComplete(int code) {

@ -57,11 +57,9 @@ class FtpThreadTask extends AbsFtpThreadTask<DownloadEntity, DownloadTaskEntity>
+ "】");
client = createClient();
if (client == null) return;
String url = mEntity.getUrl();
String remotePath = new String(
url.substring(url.indexOf(port) + port.length(), url.length()).getBytes(charSet),
SERVER_CHARSET);
client.setRestartOffset(mConfig.START_LOCATION);
String remotePath =
new String(mTaskEntity.urlEntity.remotePath.getBytes(charSet), SERVER_CHARSET);
is = client.retrieveFileStream(remotePath);
//发送第二次指令时,还需要再做一次判断
int reply = client.getReplyCode();

@ -61,7 +61,7 @@ class HttpFileInfoThread implements Runnable {
+ "】\n【filePath:"
+ mEntity.getDownloadPath()
+ "】\n"
+ CommonUtil.getPrintException(e));
+ CommonUtil.getPrintException(e), true);
} finally {
if (conn != null) {
conn.disconnect();
@ -103,7 +103,7 @@ class HttpFileInfoThread implements Runnable {
mTaskEntity.isSupportBP = false;
isComplete = true;
} else if (code == HttpURLConnection.HTTP_NOT_FOUND) {
failDownload("任务【" + mEntity.getUrl() + "】下载失败,错误码:404");
failDownload("任务【" + mEntity.getUrl() + "】下载失败,错误码:404", true);
} else if (code == HttpURLConnection.HTTP_MOVED_TEMP
|| code == HttpURLConnection.HTTP_MOVED_PERM
|| code == HttpURLConnection.HTTP_SEE_OTHER) {
@ -112,7 +112,7 @@ class HttpFileInfoThread implements Runnable {
mEntity.setRedirectUrl(mTaskEntity.redirectUrl);
handle302Turn(conn);
} else {
failDownload("任务【" + mEntity.getUrl() + "】下载失败,错误码:" + code);
failDownload("任务【" + mEntity.getUrl() + "】下载失败,错误码:" + code, true);
}
if (isComplete) {
if (onFileInfoListener != null) {
@ -130,7 +130,7 @@ class HttpFileInfoThread implements Runnable {
Log.d(TAG, "30x跳转,location【 " + mTaskEntity.redirectUrlKey + "】" + "新url为【" + newUrl + "】");
if (TextUtils.isEmpty(newUrl) || newUrl.equalsIgnoreCase("null")) {
if (onFileInfoListener != null) {
onFileInfoListener.onFail(mEntity.getUrl(), "获取重定向链接失败");
onFileInfoListener.onFail(mEntity.getUrl(), "获取重定向链接失败", false);
}
return;
}
@ -156,16 +156,16 @@ class HttpFileInfoThread implements Runnable {
mTaskEntity.isNewTask = true;
}
if (len < 0) {
failDownload("任务【" + mEntity.getUrl() + "】下载失败,文件长度小于0");
failDownload("任务【" + mEntity.getUrl() + "】下载失败,文件长度小于0", true);
return false;
}
return true;
}
private void failDownload(String errorMsg) {
private void failDownload(String errorMsg, boolean needRetry) {
Log.e(TAG, errorMsg);
if (onFileInfoListener != null) {
onFileInfoListener.onFail(mEntity.getUrl(), errorMsg);
onFileInfoListener.onFail(mEntity.getUrl(), errorMsg, needRetry);
}
}
}

@ -83,8 +83,8 @@ public class SimpleDownloadUtil implements IUtil, Runnable {
mDownloader.setMaxSpeed(maxSpeed);
}
private void failDownload(String msg) {
mListener.onFail(true);
private void failDownload(String msg, boolean needRetry) {
mListener.onFail(needRetry);
ErrorHelp.saveError("HTTP_DOWNLOAD", mTaskEntity.getEntity(), msg, "");
}
@ -108,8 +108,8 @@ public class SimpleDownloadUtil implements IUtil, Runnable {
mDownloader.start();
}
@Override public void onFail(String url, String errorMsg) {
failDownload(errorMsg);
@Override public void onFail(String url, String errorMsg, boolean needRetry) {
failDownload(errorMsg, needRetry);
}
});
case AbsTaskEntity.HTTP:
@ -118,8 +118,8 @@ public class SimpleDownloadUtil implements IUtil, Runnable {
mDownloader.start();
}
@Override public void onFail(String url, String errorMsg) {
failDownload(errorMsg);
@Override public void onFail(String url, String errorMsg, boolean needRetry) {
failDownload(errorMsg, needRetry);
}
});
}

@ -15,6 +15,7 @@
*/
package com.arialyy.aria.core.inf;
import com.arialyy.aria.core.FtpUrlEntity;
import com.arialyy.aria.core.common.RequestEnum;
import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.orm.Ignore;
@ -48,8 +49,9 @@ public abstract class AbsTaskEntity<ENTITY extends AbsEntity> extends DbEntity {
/**
* 账号和密码
*/
@Ignore public String userName, userPw, account, serverIp;
@Ignore public int port;
@Ignore public FtpUrlEntity urlEntity;
//@Ignore public String userName, userPw, account, serverIp;
//@Ignore public int port;
/**
* 刷新信息 {@code true} 重新刷新下载信息

@ -22,6 +22,7 @@ import com.arialyy.aria.core.upload.UploadTask;
import com.arialyy.aria.core.upload.UploadTaskEntity;
import com.arialyy.aria.util.CheckUtil;
import com.arialyy.aria.util.CommonUtil;
import com.arialyy.aria.util.Regular;
import java.util.regex.Pattern;
/**
@ -53,7 +54,8 @@ public abstract class AbsUploadTarget<TARGET extends AbsUploadTarget, ENTITY ext
UploadEntity entity = UploadEntity.findFirst(UploadEntity.class, "filePath=?", filePath);
if (entity == null) {
entity = new UploadEntity();
String regex = "[/|\\\\|//]";
//String regex = "[/|\\\\|//]";
String regex = Regular.REG_FILE_NAME;
Pattern p = Pattern.compile(regex);
String[] strs = p.split(filePath);
String fileName = strs[strs.length - 1];

@ -15,11 +15,15 @@
*/
package com.arialyy.aria.core.upload;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;
import com.arialyy.aria.core.FtpUrlEntity;
import com.arialyy.aria.core.inf.AbsTaskEntity;
import com.arialyy.aria.core.inf.AbsUploadTarget;
import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.util.CheckUtil;
import com.arialyy.aria.util.CommonUtil;
import java.io.File;
/**
@ -29,6 +33,7 @@ import java.io.File;
public class FtpUploadTarget
extends AbsUploadTarget<FtpUploadTarget, UploadEntity, UploadTaskEntity> {
private final String TAG = "FtpUploadTarget";
private FtpUrlEntity mUrlEntity;
FtpUploadTarget(String filePath, String targetName) {
this.mTargetName = targetName;
@ -45,13 +50,25 @@ public class FtpUploadTarget
File file = new File(filePath);
mEntity.setFileName(file.getName());
mEntity.setFileSize(file.length());
}
//暂时不支持断点续传上传
//mTaskEntity.isSupportBP = false;
/**
* 设置上传路径
*
* @param uploadUrl 上传路径
*/
public FtpUploadTarget setUploadUrl(@NonNull String uploadUrl) {
CheckUtil.checkDownloadUrl(uploadUrl);
mTaskEntity.urlEntity = CommonUtil.getFtpUrlInfo(uploadUrl);
if (mEntity.getUrl().equals(uploadUrl)) return this;
mEntity.setUrl(uploadUrl);
mEntity.update();
return this;
}
/**
* ftp 用户登录信息
* ftp 用户登录信
* 设置登录信息需要在设置上传链接之后{@link #setUploadUrl(String)}
*
* @param userName ftp用户名
* @param password ftp用户密码
@ -62,6 +79,7 @@ public class FtpUploadTarget
/**
* ftp 用户登录信息
* 设置登录信息需要在设置上传链接之后{@link #setUploadUrl(String)}
*
* @param userName ftp用户名
* @param password ftp用户密码
@ -75,9 +93,10 @@ public class FtpUploadTarget
Log.e(TAG, "密码不能为null");
return this;
}
mTaskEntity.userName = userName;
mTaskEntity.userPw = password;
mTaskEntity.account = account;
mTaskEntity.urlEntity.needLogin = true;
mTaskEntity.urlEntity.user = userName;
mTaskEntity.urlEntity.password = password;
mTaskEntity.urlEntity.account = account;
return this;
}
}

@ -38,9 +38,7 @@ class FtpFileInfoThread extends AbsFtpInfoThread<UploadEntity, UploadTaskEntity>
@Override protected String setRemotePath() {
String url = mEntity.getUrl();
return url.substring(url.indexOf(mPort) + mPort.length(), url.length())
+ "/"
+ mEntity.getFileName();
return mTaskEntity.urlEntity.remotePath + "/" + mEntity.getFileName();
}
/**

@ -56,8 +56,8 @@ public class SimpleUploadUtil implements IUtil, Runnable {
}
}
@Override public void onFail(String url, String errorMsg) {
mListener.onFail(true);
@Override public void onFail(String url, String errorMsg, boolean needRetry) {
mListener.onFail(needRetry);
}
}).start();
}

@ -24,6 +24,7 @@ import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.FtpUrlEntity;
import com.arialyy.aria.core.command.ICmd;
import com.arialyy.aria.core.command.group.AbsGroupCmd;
import com.arialyy.aria.core.command.group.GroupCmdFactory;
@ -33,8 +34,8 @@ import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.download.DownloadGroupEntity;
import com.arialyy.aria.core.download.DownloadGroupTaskEntity;
import com.arialyy.aria.core.download.DownloadTaskEntity;
import com.arialyy.aria.core.inf.AbsTaskEntity;
import com.arialyy.aria.core.inf.AbsGroupTaskEntity;
import com.arialyy.aria.core.inf.AbsTaskEntity;
import com.arialyy.aria.core.upload.UploadEntity;
import com.arialyy.aria.core.upload.UploadTaskEntity;
import com.arialyy.aria.orm.DbEntity;
@ -74,6 +75,41 @@ import java.util.regex.Pattern;
public class CommonUtil {
private static final String TAG = "CommonUtil";
/**
* 分割获取url协议ip/域名端口内容
*
* @param url 输入的url{@code String url = "ftp://z:z@dygod18.com:21211/[电影天堂www.dy2018.com]猩球崛起3:终极之战BD国英双语中英双字.mkv";}
*/
public static FtpUrlEntity getFtpUrlInfo(String url) {
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);
}
} else {
entity.hostName = str;
}
entity.port = m.group(3);
entity.remotePath = TextUtils.isEmpty(m.group(4)) ? "/" : "/" + m.group(4);
}
return entity;
}
/**
* 转换Url
*

@ -0,0 +1,28 @@
package com.arialyy.aria.util;
/**
* Created by Aria.Lao on 2017/10/24.
* 正则表达式
*/
public interface Regular {
/**
* ftp地址
*/
String REG_FTP_URL = "(\\w+)://(.*):(\\d*)/(.*)";
/**
* ftp主机用户密码分割
*/
String REG_FTP_HOST_NAME = "(\\w+):?(\\w+)?@(.*)";
/**
* 获取文件名
*/
String REG_FILE_NAME = "[/|\\\\|//]";
/**
* IPV4地址匹配
*/
String REG_IP_V4 = "(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])";
}

@ -1,4 +1,7 @@
apply plugin: 'com.android.application'
//apply plugin: 'kotlin-android'
//apply plugin: 'kotlin-android-extensions'
//apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 23
@ -30,6 +33,11 @@ android {
abortOnError false
}
// sourceSets {
// main.kotlin.srcDirs += 'src/main/kotlin'
// main.java.srcDirs += 'src/main/java'
// }
}
dependencies {
@ -43,6 +51,10 @@ dependencies {
compile 'com.arialyy.frame:MVVM2:2.2.0'
compile project(':Aria')
compile project(':AriaCompiler')
// compile 'com.arialyy.aria:aria-core:3.2.13'
// compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
// compile 'com.arialyy.aria:aria-core:3.2.13'
// annotationProcessor 'com.arialyy.aria:aria-compiler:3.2.13'
}
repositories {
mavenCentral()
}

@ -36,6 +36,7 @@
<activity android:name=".download.group.FTPDirDownloadActivity"/>
<activity android:name=".upload.HttpUploadActivity"/>
<activity android:name=".upload.FtpUploadActivity"/>
<activity android:name=".download.KotlinDownloadActivity"/>
<service android:name=".download.service_download.DownloadService"/>
</application>

@ -11,7 +11,7 @@
<maxTaskNum value="1"/>
<!--设置下载失败,重试次数,默认为10-->
<reTryNum value="10"/>
<reTryNum value="2"/>
<!--设置重试间隔,单位为毫秒,默认2000毫秒-->
<reTryInterval value="5000"/>

@ -47,7 +47,7 @@ public class MainActivity extends BaseActivity<ActivityMainBinding> {
@OnClick({
R.id.download, R.id.upload, R.id.download_task_group, R.id.ftp_download,
R.id.ftp_dir_download, R.id.ftp_upload
R.id.ftp_dir_download, R.id.ftp_upload, R.id.kotlin_download
}) public void funcation(View view) {
switch (view.getId()) {
case R.id.download:
@ -68,7 +68,9 @@ public class MainActivity extends BaseActivity<ActivityMainBinding> {
case R.id.ftp_upload:
startActivity(new Intent(this, FtpUploadActivity.class));
break;
case R.id.kotlin_download:
//startActivity(new Intent(this, KotlinDownloadActivity.class));
break;
}
}
}

@ -35,7 +35,9 @@ import java.io.File;
* Ftp下载测试
*/
public class FtpDownloadActivity extends BaseActivity<ActivityFtpDownloadBinding> {
private final String URL = "ftp://172.18.104.229:21/haha/测试ss123/ftp_test.apk";
private final String URL = "ftp://d:d@dygodj8.com:12311/[电影天堂www.dy2018.com]光辉岁月BD韩语中字.rmvb";
//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";
@Override protected void init(Bundle savedInstanceState) {
super.init(savedInstanceState);
@ -56,11 +58,10 @@ public class FtpDownloadActivity extends BaseActivity<ActivityFtpDownloadBinding
public void onClick(View view) {
switch (view.getId()) {
case R.id.start:
Aria.download(this)
.loadFtp(URL, true)
.login("lao", "123456")
.setDownloadPath("/mnt/sdcard/")
.start();
Aria.download(this).loadFtp(URL, true)
.charSet("GBK")
//.login("lao", "123456")
.setDownloadPath("/mnt/sdcard/").start();
break;
case R.id.stop:
Aria.download(this).loadFtp(URL).stop();

@ -0,0 +1,73 @@
package com.arialyy.simple.download
import android.os.Bundle
import android.os.Environment
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.View
import com.arialyy.annotations.Download
import com.arialyy.aria.core.Aria
import com.arialyy.aria.core.download.DownloadTask
import com.arialyy.simple.R
import com.arialyy.simple.base.BaseActivity
/**
* Created by Aria.Lao on 2017/10/23.
*/
class KotlinDownloadActivity : AppCompatActivity() {
private val DOWNLOAD_URL = "http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk"
private val TAG = "KotlinDownloadActivity";
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(setLayoutId())
}
fun setLayoutId(): Int {
return R.layout.activity_single
}
fun init(savedInstanceState: Bundle?) {
title = "单任务下载"
// val target = Aria.download(this).load(DOWNLOAD_URL)
// binding.progress = target.getPercent()
// if (target.getTaskState() == IEntity.STATE_STOP) {
// mStart.setText("恢复")
// mStart.setTextColor(resources.getColor(android.R.color.holo_blue_light))
// setBtState(true)
// } else if (target.isDownloading()) {
// setBtState(false)
// }
// binding.fileSize = target.getConvertFileSize()
Aria.download(this).register()
}
@Download.onTaskRunning protected fun running(task: DownloadTask) {
Log.d(TAG, task.percent.toString() + "")
val len = task.fileSize
// if (len == 0L) {
// binding.progress = 0
// } else {
// binding.progress = task.percent
// }
// binding.speed = task.convertSpeed
}
fun onClick(view: View) {
when (view.id) {
R.id.start -> startD()
R.id.stop -> Aria.download(this).load(DOWNLOAD_URL).stop()
R.id.cancel -> Aria.download(this).load(DOWNLOAD_URL).cancel()
}
}
private fun startD() {
Aria.download(this)
.load(DOWNLOAD_URL, true)
.addHeader("groupName", "value")
.setDownloadPath(Environment.getExternalStorageDirectory().path + "/hhhhhhhh.apk")
.start()
}
}

@ -18,6 +18,7 @@ package com.arialyy.simple.download;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
@ -125,6 +126,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
}
@Download.onTaskRunning(DOWNLOAD_URL) protected void running(DownloadTask task) {
long len = task.getFileSize();
if (len == 0) {
getBinding().setProgress(0);
@ -195,11 +197,6 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
public void onClick(View view) {
switch (view.getId()) {
case R.id.start:
startD();
startD();
startD();
startD();
startD();
startD();
//new Thread(new Runnable() {
// @Override public void run() {

@ -57,5 +57,13 @@
style="?buttonBarButtonStyle"
/>
<Button
android:id="@+id/kotlin_download"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="kotlin 下载"
style="?buttonBarButtonStyle"
/>
</LinearLayout>
</layout>

@ -1,5 +1,6 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
// ext.kotlin_version = '1.1.51'
repositories {
jcenter()
mavenCentral()
@ -8,6 +9,7 @@ buildscript {
// classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.android.tools.build:gradle:2.2.2'
classpath 'com.novoda:bintray-release:0.5.0'
// classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7'
// classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
// NOTE: Do not place your application dependencies here; they belong

Loading…
Cancel
Save