修复无法自动识别含有账号的ftpurl地址 https://github.com/AriaLyy/Aria/issues/146
This commit is contained in:
@@ -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'
|
||||
|
53
Aria/src/main/java/com/arialyy/aria/core/FtpUrlEntity.java
Normal file
53
Aria/src/main/java/com/arialyy/aria/core/FtpUrlEntity.java
Normal file
@@ -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;
|
||||
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();
|
||||
}
|
||||
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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* ftp 用户登录信息
|
||||
* 设置上传路径
|
||||
*
|
||||
* @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 用户登录信。
|
||||
* 设置登录信息需要在设置上传链接之后{@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
|
||||
*
|
||||
|
28
Aria/src/main/java/com/arialyy/aria/util/Regular.java
Normal file
28
Aria/src/main/java/com/arialyy/aria/util/Regular.java
Normal file
@@ -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])";
|
||||
|
||||
}
|
Reference in New Issue
Block a user