优化taskExists方法
添加`post`参数请求支持
This commit is contained in:
@@ -17,10 +17,9 @@ package com.arialyy.aria.core.common.http;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
import android.text.TextUtils;
|
||||
import com.arialyy.aria.core.common.RequestEnum;
|
||||
import com.arialyy.aria.core.inf.AbsTarget;
|
||||
import com.arialyy.aria.core.inf.AbsTaskEntity;
|
||||
import com.arialyy.aria.core.inf.IHttpHeaderTarget;
|
||||
import com.arialyy.aria.core.inf.IHttpHeaderDelegate;
|
||||
import com.arialyy.aria.util.ALog;
|
||||
import java.net.Proxy;
|
||||
import java.util.Collection;
|
||||
@@ -32,7 +31,7 @@ import java.util.Set;
|
||||
* HTTP header参数设置委托类
|
||||
*/
|
||||
public class HttpHeaderDelegate<TARGET extends AbsTarget>
|
||||
implements IHttpHeaderTarget<TARGET> {
|
||||
implements IHttpHeaderDelegate<TARGET> {
|
||||
private static final String TAG = "HttpHeaderDelegate";
|
||||
private TARGET mTarget;
|
||||
|
||||
@@ -76,18 +75,6 @@ public class HttpHeaderDelegate<TARGET extends AbsTarget>
|
||||
return mTarget;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置请求类型,POST或GET,默认为在GET
|
||||
* 只试用于HTTP请求
|
||||
*
|
||||
* @param requestEnum {@link RequestEnum}
|
||||
*/
|
||||
@Override
|
||||
public TARGET setRequestMode(RequestEnum requestEnum) {
|
||||
setRequestMode(mTarget.getTaskEntity(), requestEnum);
|
||||
return mTarget;
|
||||
}
|
||||
|
||||
@Override public TARGET setUrlProxy(Proxy proxy) {
|
||||
mTarget.getTaskEntity().setProxy(proxy);
|
||||
return mTarget;
|
||||
@@ -101,10 +88,6 @@ public class HttpHeaderDelegate<TARGET extends AbsTarget>
|
||||
}
|
||||
}
|
||||
|
||||
public void setRequestMode(AbsTaskEntity taskEntity, RequestEnum requestEnum) {
|
||||
taskEntity.setRequestEnum(requestEnum);
|
||||
}
|
||||
|
||||
public void addHeaders(AbsTaskEntity taskEntity, Map<String, String> headers) {
|
||||
/*
|
||||
两个map比较逻辑
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.arialyy.aria.core.common.http;
|
||||
|
||||
import android.text.TextUtils;
|
||||
import com.arialyy.aria.core.common.RequestEnum;
|
||||
import com.arialyy.aria.core.download.DownloadGroupTarget;
|
||||
import com.arialyy.aria.core.download.DownloadGroupTaskEntity;
|
||||
import com.arialyy.aria.core.download.DownloadTaskEntity;
|
||||
import com.arialyy.aria.core.inf.AbsTarget;
|
||||
import com.arialyy.aria.core.inf.IPostDelegate;
|
||||
import com.arialyy.aria.core.inf.ITarget;
|
||||
import com.arialyy.aria.util.ALog;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* post处理委托类
|
||||
*/
|
||||
public class PostDelegate<TARGET extends AbsTarget> implements IPostDelegate<TARGET>, ITarget {
|
||||
private static final String TAG = "PostDelegate";
|
||||
private TARGET mTarget;
|
||||
|
||||
public PostDelegate(TARGET target) {
|
||||
mTarget = target;
|
||||
mTarget.getTaskEntity().setRequestEnum(RequestEnum.POST);
|
||||
}
|
||||
|
||||
@Override public TARGET setParams(Map<String, String> params) {
|
||||
mTarget.getTaskEntity().setParams(params);
|
||||
if (mTarget instanceof DownloadGroupTarget) {
|
||||
for (DownloadTaskEntity subTask : ((DownloadGroupTaskEntity) mTarget.getTaskEntity()).getSubTaskEntities()) {
|
||||
subTask.setParams(params);
|
||||
}
|
||||
}
|
||||
return mTarget;
|
||||
}
|
||||
|
||||
@Override public TARGET setParam(String key, String value) {
|
||||
if (TextUtils.isEmpty(key) || TextUtils.isEmpty(value)) {
|
||||
ALog.d(TAG, "key 或value 为空");
|
||||
return mTarget;
|
||||
}
|
||||
Map<String, String> params = mTarget.getTaskEntity().getParams();
|
||||
if (params == null) {
|
||||
params = new HashMap<>();
|
||||
mTarget.getTaskEntity().setParams(params);
|
||||
}
|
||||
params.put(key, value);
|
||||
if (mTarget instanceof DownloadGroupTarget) {
|
||||
for (DownloadTaskEntity subTask : ((DownloadGroupTaskEntity) mTarget.getTaskEntity()).getSubTaskEntities()) {
|
||||
subTask.setParams(params);
|
||||
}
|
||||
}
|
||||
return mTarget;
|
||||
}
|
||||
|
||||
@Override public void start() {
|
||||
mTarget.start();
|
||||
}
|
||||
|
||||
@Override public void stop() {
|
||||
mTarget.stop();
|
||||
}
|
||||
|
||||
@Override public void resume() {
|
||||
mTarget.resume();
|
||||
}
|
||||
|
||||
@Override public void cancel() {
|
||||
mTarget.cancel();
|
||||
}
|
||||
}
|
||||
@@ -40,6 +40,11 @@ abstract class AbsDownloadTarget<TARGET extends AbsTarget, ENTITY extends AbsEnt
|
||||
*/
|
||||
String mTempFilePath;
|
||||
|
||||
/**
|
||||
* {@code true}强制下载,不考虑文件路径是否被占用
|
||||
*/
|
||||
boolean forceDownload = false;
|
||||
|
||||
/**
|
||||
* 将任务设置为最高优先级任务,最高优先级任务有以下特点:
|
||||
* 1、在下载队列中,有且只有一个最高优先级任务
|
||||
|
||||
@@ -164,7 +164,7 @@ abstract class BaseNormalTarget<TARGET extends BaseNormalTarget>
|
||||
|
||||
//设置文件保存路径,如果新文件路径和旧文件路径不同,则修改路径
|
||||
if (!filePath.equals(mEntity.getDownloadPath())) {
|
||||
if (DbEntity.checkDataExist(DownloadEntity.class, "downloadPath=?", filePath)) {
|
||||
if (DbEntity.checkDataExist(DownloadEntity.class, "downloadPath=?", filePath) && !forceDownload) {
|
||||
ALog.e(TAG, "下载失败,保存路径【" + filePath + "】已经被其它任务占用,请设置其它保存路径");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -20,7 +20,8 @@ import android.support.annotation.NonNull;
|
||||
import android.text.TextUtils;
|
||||
import com.arialyy.aria.core.common.RequestEnum;
|
||||
import com.arialyy.aria.core.common.http.HttpHeaderDelegate;
|
||||
import com.arialyy.aria.core.inf.IHttpHeaderTarget;
|
||||
import com.arialyy.aria.core.common.http.PostDelegate;
|
||||
import com.arialyy.aria.core.inf.IHttpHeaderDelegate;
|
||||
import com.arialyy.aria.core.manager.TEManager;
|
||||
import com.arialyy.aria.orm.DbEntity;
|
||||
import com.arialyy.aria.util.ALog;
|
||||
@@ -38,7 +39,7 @@ import java.util.Set;
|
||||
* 下载任务组
|
||||
*/
|
||||
public class DownloadGroupTarget extends BaseGroupTarget<DownloadGroupTarget> implements
|
||||
IHttpHeaderTarget<DownloadGroupTarget> {
|
||||
IHttpHeaderDelegate<DownloadGroupTarget> {
|
||||
private HttpHeaderDelegate<DownloadGroupTarget> mDelegate;
|
||||
/**
|
||||
* 子任务下载地址,
|
||||
@@ -74,6 +75,13 @@ public class DownloadGroupTarget extends BaseGroupTarget<DownloadGroupTarget> im
|
||||
mDelegate = new HttpHeaderDelegate<>(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Post处理
|
||||
*/
|
||||
public PostDelegate asPost() {
|
||||
return new PostDelegate<>(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新组合任务下载地址
|
||||
*
|
||||
@@ -178,6 +186,12 @@ public class DownloadGroupTarget extends BaseGroupTarget<DownloadGroupTarget> im
|
||||
return false;
|
||||
}
|
||||
|
||||
if (mTaskEntity.getRequestEnum() == RequestEnum.POST) {
|
||||
for (DownloadTaskEntity subTask : mTaskEntity.getSubTaskEntities()) {
|
||||
subTask.setRequestEnum(RequestEnum.POST);
|
||||
}
|
||||
}
|
||||
|
||||
mEntity.save();
|
||||
mTaskEntity.save();
|
||||
|
||||
@@ -262,7 +276,8 @@ public class DownloadGroupTarget extends BaseGroupTarget<DownloadGroupTarget> im
|
||||
if (!newName.equals(entity.getFileName())) {
|
||||
String oldPath = mEntity.getDirPath() + "/" + entity.getFileName();
|
||||
String newPath = mEntity.getDirPath() + "/" + newName;
|
||||
if (DbEntity.checkDataExist(DownloadEntity.class, "downloadPath=? or isComplete='true'", newPath)) {
|
||||
if (DbEntity.checkDataExist(DownloadEntity.class, "downloadPath=? or isComplete='true'",
|
||||
newPath)) {
|
||||
ALog.w(TAG, String.format("更新文件名失败,路径【%s】已存在或文件已下载", newPath));
|
||||
return;
|
||||
}
|
||||
@@ -313,14 +328,6 @@ public class DownloadGroupTarget extends BaseGroupTarget<DownloadGroupTarget> im
|
||||
return mDelegate.addHeaders(headers);
|
||||
}
|
||||
|
||||
@CheckResult
|
||||
@Override public DownloadGroupTarget setRequestMode(RequestEnum requestEnum) {
|
||||
for (DownloadTaskEntity subTask : mTaskEntity.getSubTaskEntities()) {
|
||||
subTask.setRequestEnum(requestEnum);
|
||||
}
|
||||
return mDelegate.setRequestMode(requestEnum);
|
||||
}
|
||||
|
||||
@CheckResult
|
||||
@Override public DownloadGroupTarget setUrlProxy(Proxy proxy) {
|
||||
return mDelegate.setUrlProxy(proxy);
|
||||
|
||||
@@ -288,7 +288,7 @@ public class DownloadReceiver extends AbsReceiver {
|
||||
* @return {@code true}存在,{@code false} 不存在
|
||||
*/
|
||||
public boolean taskExists(String downloadUrl) {
|
||||
return DownloadEntity.findFirst(DownloadEntity.class, "url=?", downloadUrl) != null;
|
||||
return DbEntity.checkDataExist(DownloadTaskEntity.class, "url=?", downloadUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -301,8 +301,7 @@ public class DownloadReceiver extends AbsReceiver {
|
||||
return false;
|
||||
}
|
||||
String groupName = CommonUtil.getMd5Code(urls);
|
||||
return DownloadGroupEntity.findFirst(DownloadGroupEntity.class, "groupName=?", groupName)
|
||||
!= null;
|
||||
return DbEntity.checkDataExist(DownloadGroupEntity.class, "groupName=?", groupName);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -19,7 +19,8 @@ import android.support.annotation.CheckResult;
|
||||
import android.support.annotation.NonNull;
|
||||
import com.arialyy.aria.core.common.RequestEnum;
|
||||
import com.arialyy.aria.core.common.http.HttpHeaderDelegate;
|
||||
import com.arialyy.aria.core.inf.IHttpHeaderTarget;
|
||||
import com.arialyy.aria.core.common.http.PostDelegate;
|
||||
import com.arialyy.aria.core.inf.IHttpHeaderDelegate;
|
||||
import java.net.Proxy;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -28,9 +29,8 @@ import java.util.Map;
|
||||
* https://github.com/AriaLyy/Aria
|
||||
*/
|
||||
public class DownloadTarget extends BaseNormalTarget<DownloadTarget>
|
||||
implements IHttpHeaderTarget<DownloadTarget> {
|
||||
private HttpHeaderDelegate<DownloadTarget> mDelegate;
|
||||
|
||||
implements IHttpHeaderDelegate<DownloadTarget> {
|
||||
private HttpHeaderDelegate<DownloadTarget> mHeaderDelegate;
|
||||
|
||||
DownloadTarget(DownloadEntity entity, String targetName) {
|
||||
this(entity.getUrl(), targetName);
|
||||
@@ -38,7 +38,14 @@ public class DownloadTarget extends BaseNormalTarget<DownloadTarget>
|
||||
|
||||
DownloadTarget(String url, String targetName) {
|
||||
initTarget(url, targetName);
|
||||
mDelegate = new HttpHeaderDelegate<>(this);
|
||||
mHeaderDelegate = new HttpHeaderDelegate<>(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Post处理
|
||||
*/
|
||||
public PostDelegate asPost() {
|
||||
return new PostDelegate<>(this);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -78,6 +85,21 @@ public class DownloadTarget extends BaseNormalTarget<DownloadTarget>
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置文件存储路径,如果需要修改新的文件名,修改路径便可。
|
||||
* 如:原文件路径 /mnt/sdcard/test.zip
|
||||
* 如果需要将test.zip改为game.zip,只需要重新设置文件路径为:/mnt/sdcard/game.zip
|
||||
*
|
||||
* @param filePath 路径必须为文件路径,不能为文件夹路径
|
||||
* @param forceDownload {@code true}强制下载,不考虑未见路径是否被占用
|
||||
*/
|
||||
@CheckResult
|
||||
public DownloadTarget setFilePath(@NonNull String filePath, boolean forceDownload) {
|
||||
mTempFilePath = filePath;
|
||||
this.forceDownload = forceDownload;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从header中获取文件描述信息
|
||||
*/
|
||||
@@ -89,7 +111,6 @@ public class DownloadTarget extends BaseNormalTarget<DownloadTarget>
|
||||
return HTTP;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 设置URL的代理
|
||||
*
|
||||
@@ -97,21 +118,17 @@ public class DownloadTarget extends BaseNormalTarget<DownloadTarget>
|
||||
*/
|
||||
@CheckResult
|
||||
@Override public DownloadTarget setUrlProxy(Proxy proxy) {
|
||||
return mDelegate.setUrlProxy(proxy);
|
||||
return mHeaderDelegate.setUrlProxy(proxy);
|
||||
}
|
||||
|
||||
@CheckResult
|
||||
@Override public DownloadTarget addHeader(@NonNull String key, @NonNull String value) {
|
||||
return mDelegate.addHeader(key, value);
|
||||
return mHeaderDelegate.addHeader(key, value);
|
||||
}
|
||||
|
||||
@CheckResult
|
||||
@Override public DownloadTarget addHeaders(Map<String, String> headers) {
|
||||
return mDelegate.addHeaders(headers);
|
||||
return mHeaderDelegate.addHeaders(headers);
|
||||
}
|
||||
|
||||
@CheckResult
|
||||
@Override public DownloadTarget setRequestMode(RequestEnum requestEnum) {
|
||||
return mDelegate.setRequestMode(requestEnum);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ import com.arialyy.aria.orm.annotation.Foreign;
|
||||
import com.arialyy.aria.orm.annotation.Ignore;
|
||||
import com.arialyy.aria.orm.annotation.NoNull;
|
||||
import com.arialyy.aria.orm.annotation.Primary;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Created by lyy on 2017/1/23.
|
||||
@@ -60,8 +61,6 @@ public class DownloadTaskEntity extends AbsNormalTaskEntity<DownloadEntity> {
|
||||
onUpdate = ActionPolicy.CASCADE, onDelete = ActionPolicy.CASCADE)
|
||||
private String key;
|
||||
|
||||
|
||||
|
||||
public DownloadTaskEntity() {
|
||||
}
|
||||
|
||||
@@ -112,6 +111,4 @@ public class DownloadTaskEntity extends AbsNormalTaskEntity<DownloadEntity> {
|
||||
public void setGroupTask(boolean groupTask) {
|
||||
isGroupTask = groupTask;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -100,6 +100,8 @@ class ConnectionHelp {
|
||||
static HttpURLConnection setConnectParam(DownloadTaskEntity entity, HttpURLConnection conn) {
|
||||
if (entity.getRequestEnum() == RequestEnum.POST) {
|
||||
conn.setDoInput(true);
|
||||
conn.setDoOutput(true);
|
||||
conn.setUseCaches(false);
|
||||
}
|
||||
Set<String> keys = null;
|
||||
if (entity.getHeaders() != null && entity.getHeaders().size() > 0) {
|
||||
@@ -108,7 +110,9 @@ class ConnectionHelp {
|
||||
conn.setRequestProperty(key, entity.getHeaders().get(key));
|
||||
}
|
||||
}
|
||||
|
||||
if (conn.getRequestProperty("Accept-Language") == null) {
|
||||
conn.setRequestProperty("Accept-Language", "UTF-8");
|
||||
}
|
||||
if (conn.getRequestProperty("Accept-Encoding") == null) {
|
||||
conn.setRequestProperty("Accept-Encoding", "identity");
|
||||
}
|
||||
@@ -126,7 +130,7 @@ class ConnectionHelp {
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
|
||||
}
|
||||
if (conn.getRequestProperty("Accept") == null) {
|
||||
StringBuilder accept = new StringBuilder();
|
||||
//StringBuilder accept = new StringBuilder();
|
||||
//accept
|
||||
//.append("image/gif, ")
|
||||
//.append("image/jpeg, ")
|
||||
|
||||
@@ -19,6 +19,7 @@ import android.text.TextUtils;
|
||||
import com.arialyy.aria.core.AriaManager;
|
||||
import com.arialyy.aria.core.common.CompleteInfo;
|
||||
import com.arialyy.aria.core.common.OnFileInfoCallback;
|
||||
import com.arialyy.aria.core.common.RequestEnum;
|
||||
import com.arialyy.aria.core.download.DownloadEntity;
|
||||
import com.arialyy.aria.core.download.DownloadTaskEntity;
|
||||
import com.arialyy.aria.util.ALog;
|
||||
@@ -28,9 +29,13 @@ import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.net.URLDecoder;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 下载文件信息获取
|
||||
@@ -76,6 +81,23 @@ class HttpFileInfoThread implements Runnable {
|
||||
}
|
||||
|
||||
private void handleConnect(HttpURLConnection conn) throws IOException {
|
||||
if (mTaskEntity.getRequestEnum() == RequestEnum.POST) {
|
||||
Map<String, String> params = mTaskEntity.getParams();
|
||||
if (params != null) {
|
||||
OutputStreamWriter dos = new OutputStreamWriter(conn.getOutputStream());
|
||||
Set<String> keys = params.keySet();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (String key : keys) {
|
||||
sb.append(key).append("=").append(URLEncoder.encode(params.get(key))).append("&");
|
||||
}
|
||||
String url = sb.toString();
|
||||
url = url.substring(0, url.length() - 1);
|
||||
dos.write(url);
|
||||
dos.flush();
|
||||
dos.close();
|
||||
}
|
||||
}
|
||||
|
||||
long len = conn.getContentLength();
|
||||
if (len < 0) {
|
||||
String temp = conn.getHeaderField("Content-Length");
|
||||
@@ -122,7 +144,7 @@ class HttpFileInfoThread implements Runnable {
|
||||
if (temp.length > 1) {
|
||||
String newName = URLDecoder.decode(temp[1], "utf-8");
|
||||
mEntity.setServerFileName(newName);
|
||||
fileRename(newName);
|
||||
renameFile(newName);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -184,7 +206,7 @@ class HttpFileInfoThread implements Runnable {
|
||||
/**
|
||||
* 重命名文件
|
||||
*/
|
||||
private void fileRename(String newName) {
|
||||
private void renameFile(String newName) {
|
||||
if (TextUtils.isEmpty(newName)) {
|
||||
ALog.w(TAG, "重命名失败【服务器返回的文件名为空】");
|
||||
return;
|
||||
@@ -219,7 +241,8 @@ class HttpFileInfoThread implements Runnable {
|
||||
mEntity.setRedirect(true);
|
||||
mEntity.setRedirectUrl(newUrl);
|
||||
String cookies = conn.getHeaderField("Set-Cookie");
|
||||
conn = (HttpURLConnection) new URL(newUrl).openConnection();
|
||||
URL url = new URL(CommonUtil.convertUrl(newUrl));
|
||||
conn = ConnectionHelp.handleConnection(url, mTaskEntity);
|
||||
conn = ConnectionHelp.setConnectParam(mTaskEntity, conn);
|
||||
conn.setRequestProperty("Cookie", cookies);
|
||||
conn.setRequestProperty("Range", "bytes=" + 0 + "-");
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
package com.arialyy.aria.core.download.downloader;
|
||||
|
||||
import com.arialyy.aria.core.common.AbsThreadTask;
|
||||
import com.arialyy.aria.core.common.RequestEnum;
|
||||
import com.arialyy.aria.core.common.StateConstance;
|
||||
import com.arialyy.aria.core.common.SubThreadConfig;
|
||||
import com.arialyy.aria.core.download.DownloadEntity;
|
||||
@@ -28,13 +29,17 @@ import java.io.BufferedInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.Channels;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.nio.channels.ReadableByteChannel;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Created by lyy on 2017/1/18.
|
||||
@@ -83,6 +88,23 @@ final class HttpThreadTask extends AbsThreadTask<DownloadEntity, DownloadTaskEnt
|
||||
conn.setConnectTimeout(mConnectTimeOut);
|
||||
conn.setReadTimeout(mReadTimeOut); //设置读取流的等待时间,必须设置该参数
|
||||
conn.connect();
|
||||
if (mTaskEntity.getRequestEnum() == RequestEnum.POST) {
|
||||
Map<String, String> params = mTaskEntity.getParams();
|
||||
if (params != null) {
|
||||
OutputStreamWriter dos = new OutputStreamWriter(conn.getOutputStream());
|
||||
Set<String> keys = params.keySet();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (String key : keys) {
|
||||
sb.append(key).append("=").append(URLEncoder.encode(params.get(key))).append("&");
|
||||
}
|
||||
String paramStr = sb.toString();
|
||||
paramStr = paramStr.substring(0, paramStr.length() - 1);
|
||||
dos.write(paramStr);
|
||||
dos.flush();
|
||||
dos.close();
|
||||
}
|
||||
}
|
||||
|
||||
is = new BufferedInputStream(ConnectionHelp.convertInputStream(conn));
|
||||
if (isOpenDynamicFile) {
|
||||
readDynamicFile(is);
|
||||
|
||||
@@ -22,6 +22,7 @@ import com.arialyy.aria.core.command.ICmd;
|
||||
import com.arialyy.aria.core.command.normal.CancelCmd;
|
||||
import com.arialyy.aria.core.command.normal.NormalCmdFactory;
|
||||
import com.arialyy.aria.core.common.TaskRecord;
|
||||
import com.arialyy.aria.core.common.http.PostDelegate;
|
||||
import com.arialyy.aria.core.download.DownloadGroupEntity;
|
||||
import com.arialyy.aria.core.download.DownloadGroupTaskEntity;
|
||||
import com.arialyy.aria.core.download.DownloadTaskEntity;
|
||||
|
||||
@@ -70,6 +70,11 @@ public abstract class AbsTaskEntity<ENTITY extends AbsEntity> extends DbEntity {
|
||||
*/
|
||||
@Ignore private boolean isNewTask = false;
|
||||
|
||||
/**
|
||||
* 请求参数
|
||||
*/
|
||||
@Ignore private Map<String, String> params;
|
||||
|
||||
/**
|
||||
* 任务状态,和Entity的state同步
|
||||
*/
|
||||
@@ -256,4 +261,12 @@ public abstract class AbsTaskEntity<ENTITY extends AbsEntity> extends DbEntity {
|
||||
public void setCode(int code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public Map<String, String> getParams() {
|
||||
return params;
|
||||
}
|
||||
|
||||
public void setParams(Map<String, String> params) {
|
||||
this.params = params;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ import java.util.Map;
|
||||
* Created by laoyuyu on 2018/3/9.
|
||||
* HTTP Header功能接口
|
||||
*/
|
||||
public interface IHttpHeaderTarget<TARGET extends ITarget> {
|
||||
public interface IHttpHeaderDelegate<TARGET extends ITarget> {
|
||||
|
||||
/**
|
||||
* 给url请求添加Header数据
|
||||
@@ -47,14 +47,6 @@ public interface IHttpHeaderTarget<TARGET extends ITarget> {
|
||||
@CheckResult
|
||||
TARGET addHeaders(Map<String, String> headers);
|
||||
|
||||
/**
|
||||
* 设置HTTP请求类型
|
||||
*
|
||||
* @param requestEnum {@link RequestEnum}
|
||||
*/
|
||||
@CheckResult
|
||||
TARGET setRequestMode(RequestEnum requestEnum);
|
||||
|
||||
@CheckResult
|
||||
TARGET setUrlProxy(Proxy proxy);
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.arialyy.aria.core.inf;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* post 通用处理接口
|
||||
*/
|
||||
public interface IPostDelegate<TARGET extends ITarget> {
|
||||
|
||||
/**
|
||||
* 设置Post请求参数
|
||||
*/
|
||||
TARGET setParams(Map<String, String> params);
|
||||
|
||||
TARGET setParam(String key, String value);
|
||||
}
|
||||
@@ -89,7 +89,7 @@ public class UploadReceiver extends AbsReceiver {
|
||||
* @return {@code true}存在,{@code false} 不存在
|
||||
*/
|
||||
public boolean taskExists(String filePath) {
|
||||
return DbEntity.findFirst(UploadEntity.class, "filePath=?", filePath) != null;
|
||||
return DbEntity.checkDataExist(UploadTaskEntity.class, "key=?", filePath);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -17,10 +17,10 @@ package com.arialyy.aria.core.upload;
|
||||
|
||||
import android.support.annotation.CheckResult;
|
||||
import android.support.annotation.NonNull;
|
||||
import com.arialyy.aria.core.common.RequestEnum;
|
||||
import com.arialyy.aria.core.common.http.HttpHeaderDelegate;
|
||||
import com.arialyy.aria.core.common.http.PostDelegate;
|
||||
import com.arialyy.aria.core.inf.AbsTaskEntity;
|
||||
import com.arialyy.aria.core.inf.IHttpHeaderTarget;
|
||||
import com.arialyy.aria.core.inf.IHttpHeaderDelegate;
|
||||
import java.net.Proxy;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -29,7 +29,7 @@ import java.util.Map;
|
||||
* http 单文件上传
|
||||
*/
|
||||
public class UploadTarget extends BaseNormalTarget<UploadTarget>
|
||||
implements IHttpHeaderTarget<UploadTarget> {
|
||||
implements IHttpHeaderDelegate<UploadTarget> {
|
||||
private HttpHeaderDelegate<UploadTarget> mDelegate;
|
||||
|
||||
UploadTarget(String filePath, String targetName) {
|
||||
@@ -46,6 +46,13 @@ public class UploadTarget extends BaseNormalTarget<UploadTarget>
|
||||
mDelegate = new HttpHeaderDelegate<>(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Post处理
|
||||
*/
|
||||
public PostDelegate asPost() {
|
||||
return new PostDelegate<>(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置userAgent
|
||||
*/
|
||||
@@ -87,11 +94,6 @@ public class UploadTarget extends BaseNormalTarget<UploadTarget>
|
||||
return mDelegate.addHeaders(headers);
|
||||
}
|
||||
|
||||
@CheckResult
|
||||
@Override public UploadTarget setRequestMode(RequestEnum requestEnum) {
|
||||
return mDelegate.setRequestMode(requestEnum);
|
||||
}
|
||||
|
||||
@Override public UploadTarget setUrlProxy(Proxy proxy) {
|
||||
return mDelegate.setUrlProxy(proxy);
|
||||
}
|
||||
|
||||
@@ -56,13 +56,6 @@ public class DelegateWrapper {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
///**
|
||||
// * 保存关联数据
|
||||
// */
|
||||
//void saveRelationData(AbsWrapper wrapper){
|
||||
//
|
||||
//}
|
||||
|
||||
/**
|
||||
* 查询关联表数据
|
||||
*
|
||||
|
||||
12
DEV_LOG.md
12
DEV_LOG.md
@@ -1,4 +1,16 @@
|
||||
## 开发日志
|
||||
+ v_3.5.1
|
||||
- 优化`taskExists`方法
|
||||
- 添加`post`参数请求支持
|
||||
```java
|
||||
Aria.download(SingleTaskActivity.this)
|
||||
.load(DOWNLOAD_URL)
|
||||
.setFilePath(path)
|
||||
.asPost() // post请求
|
||||
.setParam("key", "value") //传递参数
|
||||
//.setParams(Map<String, String>) // 传递多参数
|
||||
.start();
|
||||
```
|
||||
+ v_3.5
|
||||
- fix bug https://github.com/AriaLyy/Aria/issues/302
|
||||
- fix bug https://github.com/AriaLyy/Aria/issues/283
|
||||
|
||||
@@ -42,6 +42,7 @@ import com.arialyy.simple.R;
|
||||
import com.arialyy.simple.base.BaseActivity;
|
||||
import com.arialyy.simple.databinding.ActivitySingleBinding;
|
||||
import java.io.File;
|
||||
import java.util.Map;
|
||||
|
||||
public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
|
||||
|
||||
@@ -50,13 +51,14 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
|
||||
//"https://atom-installer.github.com/v1.13.0/AtomSetup.exe?s=1484074138&ext=.exe";
|
||||
//"http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk";
|
||||
//"https://yizi-kejian.oss-cn-beijing.aliyuncs.com/qimeng/package1/qmtable11.zip";
|
||||
"http://rs.0.gaoshouyou.com/d/04/1e/400423a7551e1f3f0eb1812afa1f9b44.apk";
|
||||
//"http://rs.0.gaoshouyou.com/d/04/1e/400423a7551e1f3f0eb1812afa1f9b44.apk";
|
||||
//"http://58.210.9.131/tpk/sipgt//TDLYZTGH.tpk"; //chunked 下载
|
||||
//"https://static.donguo.me//video/ip/course/pfys_1.mp4";
|
||||
//"https://www.baidu.com/link?url=_LFCuTPtnzFxVJByJ504QymRywIA1Z_T5xUxe9ZLuxcGM0C_RcdpWyB1eGjbJC-e5wv5wAKM4WmLMAS5KeF6EZJHB8Va3YqZUiaErqK_pxm&wd=&eqid=e8583fe70002d126000000065a99f864";
|
||||
//"https://d.pcs.baidu.com/file/a02c89a2d479d4fd2756f3313d42491d?fid=4232431903-250528-1114369760340736&dstime=1525491372&rt=sh&sign=FDtAERVY-DCb740ccc5511e5e8fedcff06b081203-3C13vkOkuk4TqXvVYW05zj1K0ao%3D&expires=8h&chkv=1&chkbd=0&chkpc=et&dp-logid=8651730921842106225&dp-callid=0&r=165533013";
|
||||
//"http://apk500.bce.baidu-mgame.com/game/67000/67734/20170622040827_oem_5502845.apk?r=1";
|
||||
//"https://dl.genymotion.com/releases/genymotion-2.12.1/genymotion-2.12.1-vbox.exe";
|
||||
"http://9.9.9.59:5000/download/CentOS-7-x86_64-Minimal-1804.iso";
|
||||
@Bind(R.id.start) Button mStart;
|
||||
@Bind(R.id.stop) Button mStop;
|
||||
@Bind(R.id.cancel) Button mCancel;
|
||||
@@ -234,6 +236,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
|
||||
//startActivity(new Intent(this, SingleTaskActivity.class));
|
||||
//Aria.download(this).unRegister();
|
||||
//Aria.download(this).load(DOWNLOAD_URL).removeRecord();
|
||||
//Log.d(TAG, Aria.download(this).taskExists(DOWNLOAD_URL) + "");
|
||||
break;
|
||||
case R.id.cancel:
|
||||
Aria.download(this).load(DOWNLOAD_URL).cancel(true);
|
||||
@@ -245,7 +248,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
|
||||
private void startD() {
|
||||
//Aria.get(this).setLogLevel(ALog.LOG_CLOSE);
|
||||
//Aria.download(this).load("aaaa.apk");
|
||||
String path = Environment.getExternalStorageDirectory().getPath() + "/ggsg8.apk";
|
||||
String path = Environment.getExternalStorageDirectory().getPath() + "/ggsg9.apk";
|
||||
//File file = new File(path);
|
||||
//if (file.exists()){
|
||||
// file.delete();
|
||||
@@ -260,6 +263,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
|
||||
//.useServerFileName(true)
|
||||
//.setRequestMode(RequestEnum.GET)
|
||||
.setFilePath(path)
|
||||
.asPost().setParams("key", "value")
|
||||
//.setExtendField("{\n"
|
||||
// + "\"id\":\"你的样子\"\n< > "
|
||||
// + "}")
|
||||
|
||||
@@ -67,7 +67,6 @@ public class AnyRunnModule {
|
||||
String path = Environment.getExternalStorageDirectory().getPath() + "/mmm2.mp4";
|
||||
Aria.download(this)
|
||||
.load(url)
|
||||
.setRequestMode(RequestEnum.GET)
|
||||
.setFilePath(path)
|
||||
.resetState()
|
||||
.start();
|
||||
|
||||
@@ -56,7 +56,7 @@ public class HttpUploadActivity extends BaseActivity<ActivityUploadBinding> {
|
||||
"http://lib-test.xzxyun.com:8042/Api/upload?data={\"type\":\"1\",\"fileType\":\".apk\"}")
|
||||
//.setUploadUrl("http://192.168.1.6:8080/upload/sign_file/").setAttachment("file")
|
||||
//.addHeader("iplanetdirectorypro", "11a09102fb934ad0bc206f9c611d7933")
|
||||
.setRequestMode(RequestEnum.POST)
|
||||
.asPost()
|
||||
.start();
|
||||
}
|
||||
|
||||
|
||||
@@ -39,7 +39,7 @@ task clean(type: Delete) {
|
||||
ext {
|
||||
userOrg = 'arialyy'
|
||||
groupId = 'com.arialyy.aria'
|
||||
publishVersion = '3.5_dev1'
|
||||
publishVersion = '3.5'
|
||||
// publishVersion = '1.0.3' //FTP插件
|
||||
repoName='maven'
|
||||
desc = 'android 下载框架'
|
||||
|
||||
21
py/download.py
Normal file
21
py/download.py
Normal file
@@ -0,0 +1,21 @@
|
||||
# coding=utf-8
|
||||
|
||||
import os
|
||||
from flask import Flask, send_from_directory, request
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
|
||||
@app.route("/download/<path:filename>", methods=['POST', 'GET'])
|
||||
def downloader(filename):
|
||||
"""
|
||||
不支持断点的下载
|
||||
"""
|
||||
data = request.values.get('key')
|
||||
print data
|
||||
dirpath = 'D:/test'
|
||||
return send_from_directory(dirpath, filename, as_attachment=True) # as_attachment=True 一定要写,不然会变成打开,而不是下载
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(host='0.0.0.0', debug=True) # 需要关闭防火墙
|
||||
62
py/upload.py
Normal file
62
py/upload.py
Normal file
@@ -0,0 +1,62 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import os
|
||||
from flask import Flask, request, url_for, send_from_directory
|
||||
from werkzeug import secure_filename
|
||||
|
||||
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif', 'rar', 'apk'])
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config['UPLOAD_FOLDER'] = 'd:/db/'
|
||||
app.config['MAX_CONTENT_LENGTH'] = 1600 * 1024 * 1024
|
||||
|
||||
"""
|
||||
可以选择这个扩展
|
||||
Flask-Uploads
|
||||
"""
|
||||
|
||||
html = '''
|
||||
<!DOCTYPE html>
|
||||
<title>Upload File</title>
|
||||
<h1>图片上传</h1>
|
||||
<form method=post enctype=multipart/form-data>
|
||||
<input type=file name=file>
|
||||
<input type=submit value=上传>
|
||||
</form>
|
||||
'''
|
||||
|
||||
|
||||
def allowed_file(filename):
|
||||
return '.' in filename and \
|
||||
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
|
||||
|
||||
|
||||
@app.route('/uploads/<filename>')
|
||||
def uploaded_file(filename):
|
||||
return send_from_directory(app.config['UPLOAD_FOLDER'],
|
||||
filename)
|
||||
|
||||
|
||||
@app.route('/', methods=['GET', 'POST'])
|
||||
def test():
|
||||
return'test'
|
||||
|
||||
|
||||
@app.route('/upload/', methods=['GET', 'POST'])
|
||||
def upload_file():
|
||||
print 'upload'
|
||||
if request.method == 'POST':
|
||||
print 'post'
|
||||
file = request.files['file']
|
||||
print file
|
||||
if file and allowed_file(file.filename):
|
||||
print 'start save'
|
||||
filename = secure_filename(file.filename)
|
||||
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
|
||||
# file_url = url_for('uploaded_file', filename=filename)
|
||||
# return html + '<br><img src=' + file_url + '>'
|
||||
return '200'
|
||||
return '405'
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(host='0.0.0.0', port=5000)
|
||||
Reference in New Issue
Block a user