diff --git a/Aria/build.gradle b/Aria/build.gradle index 7120c44d..ad5e8af1 100644 --- a/Aria/build.gradle +++ b/Aria/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' diff --git a/Aria/src/main/java/com/arialyy/aria/core/FtpUrlEntity.java b/Aria/src/main/java/com/arialyy/aria/core/FtpUrlEntity.java new file mode 100644 index 00000000..1d4efd45 --- /dev/null +++ b/Aria/src/main/java/com/arialyy/aria/core/FtpUrlEntity.java @@ -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; +} 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 98aaee5f..1604adf4 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 @@ -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 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 { 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; } } 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 95bd3b88..d476f12c 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 @@ -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; } } 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 011afd04..8324e09f 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 @@ -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); 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 88319a74..ca9f891a 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 @@ -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); 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 a730eb57..ebd6dc6c 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 @@ -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(); 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 d40580ea..e3edec7c 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,7 @@ class FtpDirInfoThread extends AbsFtpInfoThread + "】"); 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(); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpFileInfoThread.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpFileInfoThread.java index 4063c773..55cd8f4f 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpFileInfoThread.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpFileInfoThread.java @@ -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); } } } \ No newline at end of file diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/SimpleDownloadUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/SimpleDownloadUtil.java index 6ea8f4fd..743226d2 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/SimpleDownloadUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/SimpleDownloadUtil.java @@ -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); } }); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTaskEntity.java b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTaskEntity.java index 60096d20..c8ef20fc 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTaskEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTaskEntity.java @@ -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 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} 重新刷新下载信息 diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsUploadTarget.java b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsUploadTarget.java index a45ccf4c..e3aec7ba 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsUploadTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsUploadTarget.java @@ -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 { 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; } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpFileInfoThread.java b/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpFileInfoThread.java index 9920d912..21384d18 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpFileInfoThread.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpFileInfoThread.java @@ -38,9 +38,7 @@ class FtpFileInfoThread extends AbsFtpInfoThread @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(); } /** diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/SimpleUploadUtil.java b/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/SimpleUploadUtil.java index 4c1b45c8..753be0b9 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/SimpleUploadUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/SimpleUploadUtil.java @@ -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(); } 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 ffc1bc1c..b8b2b0c6 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java @@ -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 * diff --git a/Aria/src/main/java/com/arialyy/aria/util/Regular.java b/Aria/src/main/java/com/arialyy/aria/util/Regular.java new file mode 100644 index 00000000..1ee57d83 --- /dev/null +++ b/Aria/src/main/java/com/arialyy/aria/util/Regular.java @@ -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])"; + +} diff --git a/app/build.gradle b/app/build.gradle index 906b4c90..3c077956 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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() +} diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 00d1d08c..6730d35c 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -36,6 +36,7 @@ + diff --git a/app/src/main/assets/aria_config.xml b/app/src/main/assets/aria_config.xml index 32cf6bd1..e88bed32 100644 --- a/app/src/main/assets/aria_config.xml +++ b/app/src/main/assets/aria_config.xml @@ -11,7 +11,7 @@ - + diff --git a/app/src/main/java/com/arialyy/simple/MainActivity.java b/app/src/main/java/com/arialyy/simple/MainActivity.java index dc0012a7..52291d32 100644 --- a/app/src/main/java/com/arialyy/simple/MainActivity.java +++ b/app/src/main/java/com/arialyy/simple/MainActivity.java @@ -47,7 +47,7 @@ public class MainActivity extends BaseActivity { @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 { case R.id.ftp_upload: startActivity(new Intent(this, FtpUploadActivity.class)); break; + case R.id.kotlin_download: + //startActivity(new Intent(this, KotlinDownloadActivity.class)); + break; } } - } 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 736ca01a..c68c2844 100644 --- a/app/src/main/java/com/arialyy/simple/download/FtpDownloadActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/FtpDownloadActivity.java @@ -35,7 +35,9 @@ import java.io.File; * Ftp下载测试 */ public class FtpDownloadActivity extends BaseActivity { - 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 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() + } +} \ No newline at end of file diff --git a/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java b/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java index f1588ffc..1ffbad77 100644 --- a/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java @@ -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 { } @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 { 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() { diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 177b1590..ed4dec5c 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -57,5 +57,13 @@ style="?buttonBarButtonStyle" /> +