Permalink
Browse files

fix bug #319

新增201 重定向支持 #318
修复使用`useServerFileName(true)`中含有`"`导致的文件后缀名错误问题
修改下载线程的优先级为: Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
  • Loading branch information...
AriaLyy committed Nov 14, 2018
1 parent e22e2dc commit a625bd27a3b6f0ee99c587ae18a6e3130d754109
Showing with 84 additions and 64 deletions.
  1. +2 −2 Aria/build.gradle
  2. +3 −0 Aria/src/main/java/com/arialyy/aria/core/common/AbsThreadTask.java
  3. +7 −5 Aria/src/main/java/com/arialyy/aria/core/common/ftp/AbsFtpInfoThread.java
  4. +4 −4 Aria/src/main/java/com/arialyy/aria/core/common/ftp/AbsFtpThreadTask.java
  5. +1 −3 Aria/src/main/java/com/arialyy/aria/core/common/ftp/SSLSessionReuseFTPSClient.java
  6. +1 −1 Aria/src/main/java/com/arialyy/aria/core/download/downloader/ConnectionHelp.java
  7. +1 −1 Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirInfoThread.java
  8. +1 −1 Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpFileInfoThread.java
  9. +2 −2 Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpThreadTask.java
  10. +6 −2 Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpFileInfoThread.java
  11. +1 −1 Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java
  12. +1 −4 Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java
  13. +1 −1 Aria/src/main/java/com/arialyy/aria/core/scheduler/ISchedulerListener.java
  14. +1 −1 Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpFileInfoThread.java
  15. +3 −3 Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpThreadTask.java
  16. +22 −19 Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java
  17. +6 −2 Aria/src/main/java/com/arialyy/aria/util/ErrorHelp.java
  18. +1 −1 AriaAnnotations/build.gradle
  19. +1 −1 AriaCompiler/build.gradle
  20. +1 −1 AriaFtpPlug/build.gradle
  21. +4 −1 AriaFtpPlug/src/main/java/aria/apache/commons/net/util/TrustManagerUtils.java
  22. +5 −0 DEV_LOG.md
  23. +6 −5 app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java
  24. +2 −2 build.gradle
  25. +1 −1 gradle.properties
@@ -26,8 +26,8 @@ dependencies {
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile project(':AriaAnnotations')
compile 'com.arialyy.aria:aria-ftp-plug:1.0.3'
compile 'com.arialyy.aria:aria-ftp-plug:1.0.4'
// compile project(':AriaFtpPlug')
}
//apply from: 'bintray-release.gradle'
apply from: 'bintray-release.gradle'
@@ -15,6 +15,7 @@
*/
package com.arialyy.aria.core.common;
import android.os.Process;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.inf.AbsNormalEntity;
import com.arialyy.aria.core.inf.AbsTaskEntity;
@@ -74,6 +75,7 @@
private Thread mConfigThread = new Thread(new Runnable() {
@Override public void run() {
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
final long currentTemp = mChildCurrentLocation;
writeConfig(false, currentTemp);
}
@@ -460,6 +462,7 @@ protected void writeConfig(boolean isComplete, final long record) {
@Override public AbsThreadTask call() throws Exception {
isInterrupted = false;
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
return this;
}
}
@@ -15,7 +15,13 @@
*/
package com.arialyy.aria.core.common.ftp;
import android.os.Process;
import android.text.TextUtils;
import aria.apache.commons.net.ftp.FTP;
import aria.apache.commons.net.ftp.FTPClient;
import aria.apache.commons.net.ftp.FTPFile;
import aria.apache.commons.net.ftp.FTPReply;
import aria.apache.commons.net.ftp.FTPSClient;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.FtpUrlEntity;
import com.arialyy.aria.core.common.OnFileInfoCallback;
@@ -37,11 +43,6 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.net.ssl.SSLContext;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.net.ftp.FTPSClient;
/**
* Created by Aria.Lao on 2017/7/25.
@@ -78,6 +79,7 @@ public AbsFtpInfoThread(TASK_ENTITY taskEntity, OnFileInfoCallback callback) {
protected abstract String setRemotePath();
@Override public void run() {
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
FTPClient client = null;
try {
client = createFtpClient();
@@ -16,6 +16,10 @@
package com.arialyy.aria.core.common.ftp;
import android.text.TextUtils;
import aria.apache.commons.net.ftp.FTP;
import aria.apache.commons.net.ftp.FTPClient;
import aria.apache.commons.net.ftp.FTPReply;
import aria.apache.commons.net.ftp.FTPSClient;
import com.arialyy.aria.core.FtpUrlEntity;
import com.arialyy.aria.core.common.AbsThreadTask;
import com.arialyy.aria.core.common.ProtocolType;
@@ -30,10 +34,6 @@
import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.net.ssl.SSLContext;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.net.ftp.FTPSClient;
/**
* Created by lyy on 2017/9/26.
@@ -1,6 +1,6 @@
package com.arialyy.aria.core.common.ftp;
import com.arialyy.aria.util.ALog;
import aria.apache.commons.net.ftp.FTPSClient;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
@@ -10,7 +10,6 @@
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSessionContext;
import javax.net.ssl.SSLSocket;
import org.apache.commons.net.ftp.FTPSClient;
public class SSLSessionReuseFTPSClient extends FTPSClient {
@@ -40,7 +39,6 @@ protected void _prepareDataSocket_(final Socket socket) throws IOException {
String.valueOf(socket.getPort())).toLowerCase(Locale.ROOT), session);
method.invoke(cache, String.format("%s:%s", socket.getInetAddress().getHostAddress(),
String.valueOf(socket.getPort())).toLowerCase(Locale.ROOT), session);
ALog.d("tag", "GGGG");
} catch (NoSuchFieldException e) {
throw new IOException(e);
} catch (Exception e) {
@@ -111,7 +111,7 @@ static HttpURLConnection setConnectParam(DownloadTaskEntity entity, HttpURLConne
}
}
if (conn.getRequestProperty("Accept-Language") == null) {
conn.setRequestProperty("Accept-Language", "UTF-8");
conn.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,ja;q=0.7");
}
if (conn.getRequestProperty("Accept-Encoding") == null) {
conn.setRequestProperty("Accept-Encoding", "identity");
@@ -15,6 +15,7 @@
*/
package com.arialyy.aria.core.download.downloader;
import aria.apache.commons.net.ftp.FTPFile;
import com.arialyy.aria.core.FtpUrlEntity;
import com.arialyy.aria.core.common.CompleteInfo;
import com.arialyy.aria.core.common.OnFileInfoCallback;
@@ -27,7 +28,6 @@
import com.arialyy.aria.util.CommonUtil;
import java.nio.charset.Charset;
import java.util.ArrayList;
import org.apache.commons.net.ftp.FTPFile;
/**
* Created by Aria.Lao on 2017/7/25.
@@ -15,14 +15,14 @@
*/
package com.arialyy.aria.core.download.downloader;
import aria.apache.commons.net.ftp.FTPFile;
import com.arialyy.aria.core.common.CompleteInfo;
import com.arialyy.aria.core.common.OnFileInfoCallback;
import com.arialyy.aria.core.common.ftp.AbsFtpInfoThread;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.download.DownloadTaskEntity;
import com.arialyy.aria.exception.AriaIOException;
import com.arialyy.aria.util.CommonUtil;
import org.apache.commons.net.ftp.FTPFile;
/**
* Created by Aria.Lao on 2017/7/25.
@@ -15,6 +15,8 @@
*/
package com.arialyy.aria.core.download.downloader;
import aria.apache.commons.net.ftp.FTPClient;
import aria.apache.commons.net.ftp.FTPReply;
import com.arialyy.aria.core.common.StateConstance;
import com.arialyy.aria.core.common.SubThreadConfig;
import com.arialyy.aria.core.common.ftp.AbsFtpThreadTask;
@@ -32,8 +34,6 @@
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
/**
* Created by Aria.Lao on 2017/7/24.
@@ -15,6 +15,7 @@
*/
package com.arialyy.aria.core.download.downloader;
import android.os.Process;
import android.text.TextUtils;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.common.CompleteInfo;
@@ -59,6 +60,7 @@
}
@Override public void run() {
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
HttpURLConnection conn = null;
try {
URL url = new URL(CommonUtil.convertUrl(mEntity.getUrl()));
@@ -144,7 +146,7 @@ private void handleConnect(HttpURLConnection conn) throws IOException {
if (info.startsWith("filename") && info.contains("=")) {
String[] temp = info.split("=");
if (temp.length > 1) {
String newName = URLDecoder.decode(temp[1], "utf-8");
String newName = URLDecoder.decode(temp[1], "utf-8").replaceAll("\"", "");
mEntity.setServerFileName(newName);
renameFile(newName);
break;
@@ -191,11 +193,13 @@ private void handleConnect(HttpURLConnection conn) throws IOException {
} else if (code == HttpURLConnection.HTTP_MOVED_TEMP
|| code == HttpURLConnection.HTTP_MOVED_PERM
|| code == HttpURLConnection.HTTP_SEE_OTHER
|| code == HttpURLConnection.HTTP_CREATED // 201 跳转
|| code == 307) {
handleUrlReTurn(conn, conn.getHeaderField("Location"));
} else {
failDownload(new AriaIOException(TAG,
String.format("任务下载失败,errorCode:%s, url: %s", code, mEntity.getUrl())), true);
String.format("任务下载失败,errorCode:%s, errorMsg: %s, url: %s", code,
conn.getResponseMessage(), mEntity.getUrl())), true);
}
if (end) {
mTaskEntity.setChunked(isChunked);
@@ -76,7 +76,7 @@
int state = task.getState();
if (task.isRunning() || (state != IEntity.STATE_COMPLETE
&& state != IEntity.STATE_CANCEL)) {
task.stop();
task.stop(TaskSchedulerType.TYPE_STOP_NOT_NEXT);
}
}
}
@@ -263,7 +263,6 @@ private void handleFailTask(final TASK task) {
if (!task.needRetry || task.isStop() || task.isCancel()) {
callback(FAIL, task);
mQueue.removeTaskFormQueue(task.getKey());
ALog.d(TAG, "fail_next");
startNextTask(task);
return;
}
@@ -287,7 +286,6 @@ private void handleFailTask(final TASK task) {
callback(FAIL, task);
mQueue.removeTaskFormQueue(task.getKey());
startNextTask(task);
ALog.d(TAG, "retry_next");
TEManager.getInstance().removeTEntity(task.getKey());
return;
}
@@ -306,7 +304,6 @@ private void handleFailTask(final TASK task) {
} else {
mQueue.removeTaskFormQueue(task.getKey());
startNextTask(task);
ALog.d(TAG, "retry_next_1");
TEManager.getInstance().removeTEntity(task.getKey());
}
}
@@ -324,7 +321,7 @@ private void startNextTask(TASK oldTask) {
TASK newTask = mQueue.getNextTask();
if (newTask == null) {
if (mQueue.getCurrentExePoolNum() == 0) {
ALog.i(TAG, "没有下一任务");
ALog.i(TAG, "没有等待中的任务");
}
return;
}
@@ -61,7 +61,7 @@
/**
* 任务失败
*
* @deprecated @see{{@link #onTaskFail(ITask, Exception)}}
* @deprecated {@link #onTaskFail(ITask, Exception)}
*/
@Deprecated
void onTaskFail(TASK task);
@@ -15,6 +15,7 @@
*/
package com.arialyy.aria.core.upload.uploader;
import aria.apache.commons.net.ftp.FTPFile;
import com.arialyy.aria.core.common.ftp.AbsFtpInfoThread;
import com.arialyy.aria.core.common.CompleteInfo;
import com.arialyy.aria.core.common.OnFileInfoCallback;
@@ -25,7 +26,6 @@
import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.DbHelper;
import java.util.ArrayList;
import org.apache.commons.net.ftp.FTPFile;
/**
* Created by Aria.Lao on 2017/9/26.
@@ -15,6 +15,9 @@
*/
package com.arialyy.aria.core.upload.uploader;
import aria.apache.commons.net.ftp.FTPClient;
import aria.apache.commons.net.ftp.FTPReply;
import aria.apache.commons.net.ftp.OnFtpInputStreamListener;
import com.arialyy.aria.core.common.StateConstance;
import com.arialyy.aria.core.common.SubThreadConfig;
import com.arialyy.aria.core.common.ftp.AbsFtpThreadTask;
@@ -27,9 +30,6 @@
import com.arialyy.aria.util.BufferedRandomAccessFile;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.net.ftp.OnFtpInputStreamListener;
/**
* Created by Aria.Lao on 2017/7/28.
@@ -54,14 +54,17 @@
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -352,25 +355,25 @@ public static FtpUrlEntity getFtpUrlInfo(String url) {
public static String convertUrl(String url) {
Uri uri = Uri.parse(url);
url = uri.toString();
//if (hasDoubleCharacter(url)) {
// //预先处理空格,URLEncoder只会把空格转换为+
// url = url.replaceAll(" ", "%20");
// //匹配双字节字符(包括汉字在内)
// String regex = Regular.REG_DOUBLE_CHAR_AND_SPACE;
// Pattern p = Pattern.compile(regex);
// Matcher m = p.matcher(url);
// Set<String> strs = new HashSet<>();
// while (m.find()) {
// strs.add(m.group());
// }
// try {
// for (String str : strs) {
// url = url.replaceAll(str, URLEncoder.encode(str, "UTF-8"));
// }
// } catch (UnsupportedEncodingException e) {
// e.printStackTrace();
// }
//}
if (hasDoubleCharacter(url)) {
//预先处理空格,URLEncoder只会把空格转换为+
url = url.replaceAll(" ", "%20");
//匹配双字节字符(包括汉字在内)
String regex = Regular.REG_DOUBLE_CHAR_AND_SPACE;
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(url);
Set<String> strs = new HashSet<>();
while (m.find()) {
strs.add(m.group());
}
try {
for (String str : strs) {
url = url.replaceAll(str, URLEncoder.encode(str, "UTF-8"));
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return url;
}
@@ -47,7 +47,7 @@ public static void saveError(String tag, String msg, String ex) {
*/
private static String getLogPath() {
String path = String.format("%slog/AriaCrash_%s.log", CommonUtil.getAppPath(AriaManager.APP),
getData("yyyy-MM-dd_HH:mm:ss"));
getData("yyyy-MM-dd_HH_mm_ss"));
File log = new File(path);
if (!log.getParentFile().exists()) {
@@ -76,7 +76,11 @@ private static int writeLogToFile(String tag, String message) {
stringBuffer.append("\n\n");
PrintWriter writer = null;
try {
writer = new PrintWriter(new FileWriter(getLogPath(), true));
File file = new File(getLogPath());
if(!file.exists()){
CommonUtil.createFile(file.getPath());
}
writer = new PrintWriter(new FileWriter(file.getPath(), true));
writer.append(stringBuffer);
writer.flush();
} catch (Exception e) {
@@ -11,4 +11,4 @@ dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
//apply from: 'bintray-release.gradle'
apply from: 'bintray-release.gradle'
Oops, something went wrong.

0 comments on commit a625bd2

Please sign in to comment.