diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/http/HttpHeaderDelegate.java b/Aria/src/main/java/com/arialyy/aria/core/common/http/HttpHeaderDelegate.java index 281be7b9..9982cdfa 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/http/HttpHeaderDelegate.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/http/HttpHeaderDelegate.java @@ -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 - implements IHttpHeaderTarget { + implements IHttpHeaderDelegate { private static final String TAG = "HttpHeaderDelegate"; private TARGET mTarget; @@ -76,18 +75,6 @@ public class HttpHeaderDelegate 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 } } - public void setRequestMode(AbsTaskEntity taskEntity, RequestEnum requestEnum) { - taskEntity.setRequestEnum(requestEnum); - } - public void addHeaders(AbsTaskEntity taskEntity, Map headers) { /* 两个map比较逻辑 diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/http/PostDelegate.java b/Aria/src/main/java/com/arialyy/aria/core/common/http/PostDelegate.java new file mode 100644 index 00000000..f06f3ae9 --- /dev/null +++ b/Aria/src/main/java/com/arialyy/aria/core/common/http/PostDelegate.java @@ -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 implements IPostDelegate, 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 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 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(); + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/AbsDownloadTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/AbsDownloadTarget.java index f254db27..4662be8d 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/AbsDownloadTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/AbsDownloadTarget.java @@ -40,6 +40,11 @@ abstract class AbsDownloadTarget //设置文件保存路径,如果新文件路径和旧文件路径不同,则修改路径 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; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java index 9a35d94b..2380f54f 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java @@ -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 implements - IHttpHeaderTarget { + IHttpHeaderDelegate { private HttpHeaderDelegate mDelegate; /** * 子任务下载地址, @@ -74,6 +75,13 @@ public class DownloadGroupTarget extends BaseGroupTarget im mDelegate = new HttpHeaderDelegate<>(this); } + /** + * Post处理 + */ + public PostDelegate asPost() { + return new PostDelegate<>(this); + } + /** * 更新组合任务下载地址 * @@ -178,6 +186,12 @@ public class DownloadGroupTarget extends BaseGroupTarget 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 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 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); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java index 341d4d52..312f7aff 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java @@ -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); } /** diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java index b20ccebd..14ad617d 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java @@ -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 - implements IHttpHeaderTarget { - private HttpHeaderDelegate mDelegate; - + implements IHttpHeaderDelegate { + private HttpHeaderDelegate mHeaderDelegate; DownloadTarget(DownloadEntity entity, String targetName) { this(entity.getUrl(), targetName); @@ -38,7 +38,14 @@ public class DownloadTarget extends BaseNormalTarget 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 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 return HTTP; } - /** * 设置URL的代理 * @@ -97,21 +118,17 @@ public class DownloadTarget extends BaseNormalTarget */ @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 headers) { - return mDelegate.addHeaders(headers); + return mHeaderDelegate.addHeaders(headers); } - @CheckResult - @Override public DownloadTarget setRequestMode(RequestEnum requestEnum) { - return mDelegate.setRequestMode(requestEnum); - } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTaskEntity.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTaskEntity.java index 0d046d50..f23f5f30 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTaskEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTaskEntity.java @@ -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 { onUpdate = ActionPolicy.CASCADE, onDelete = ActionPolicy.CASCADE) private String key; - - public DownloadTaskEntity() { } @@ -112,6 +111,4 @@ public class DownloadTaskEntity extends AbsNormalTaskEntity { public void setGroupTask(boolean groupTask) { isGroupTask = groupTask; } - - } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/ConnectionHelp.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/ConnectionHelp.java index a7b985a7..e5a78c25 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/ConnectionHelp.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/ConnectionHelp.java @@ -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 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, ") 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 c65b3800..fc87f934 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 @@ -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 params = mTaskEntity.getParams(); + if (params != null) { + OutputStreamWriter dos = new OutputStreamWriter(conn.getOutputStream()); + Set 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 + "-"); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java index 20575ff0..7e9534a3 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java @@ -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 params = mTaskEntity.getParams(); + if (params != null) { + OutputStreamWriter dos = new OutputStreamWriter(conn.getOutputStream()); + Set 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); diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java index 96d44748..3eb6a485 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java @@ -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; 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 eba12e79..3e7331aa 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 @@ -70,6 +70,11 @@ public abstract class AbsTaskEntity extends DbEntity { */ @Ignore private boolean isNewTask = false; + /** + * 请求参数 + */ + @Ignore private Map params; + /** * 任务状态,和Entity的state同步 */ @@ -256,4 +261,12 @@ public abstract class AbsTaskEntity extends DbEntity { public void setCode(int code) { this.code = code; } + + public Map getParams() { + return params; + } + + public void setParams(Map params) { + this.params = params; + } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/IHttpHeaderTarget.java b/Aria/src/main/java/com/arialyy/aria/core/inf/IHttpHeaderDelegate.java similarity index 88% rename from Aria/src/main/java/com/arialyy/aria/core/inf/IHttpHeaderTarget.java rename to Aria/src/main/java/com/arialyy/aria/core/inf/IHttpHeaderDelegate.java index 29ae6f52..80307556 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/IHttpHeaderTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/IHttpHeaderDelegate.java @@ -26,7 +26,7 @@ import java.util.Map; * Created by laoyuyu on 2018/3/9. * HTTP Header功能接口 */ -public interface IHttpHeaderTarget { +public interface IHttpHeaderDelegate { /** * 给url请求添加Header数据 @@ -47,14 +47,6 @@ public interface IHttpHeaderTarget { @CheckResult TARGET addHeaders(Map headers); - /** - * 设置HTTP请求类型 - * - * @param requestEnum {@link RequestEnum} - */ - @CheckResult - TARGET setRequestMode(RequestEnum requestEnum); - @CheckResult TARGET setUrlProxy(Proxy proxy); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/IPostDelegate.java b/Aria/src/main/java/com/arialyy/aria/core/inf/IPostDelegate.java new file mode 100644 index 00000000..b5ace80a --- /dev/null +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/IPostDelegate.java @@ -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 { + + /** + * 设置Post请求参数 + */ + TARGET setParams(Map params); + + TARGET setParam(String key, String value); +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java index a2be1ed4..63c3de42 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java @@ -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); } /** diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTarget.java b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTarget.java index 734ff20a..fdd91f7d 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTarget.java @@ -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 - implements IHttpHeaderTarget { + implements IHttpHeaderDelegate { private HttpHeaderDelegate mDelegate; UploadTarget(String filePath, String targetName) { @@ -46,6 +46,13 @@ public class UploadTarget extends BaseNormalTarget mDelegate = new HttpHeaderDelegate<>(this); } + /** + * Post处理 + */ + public PostDelegate asPost() { + return new PostDelegate<>(this); + } + /** * 设置userAgent */ @@ -87,11 +94,6 @@ public class UploadTarget extends BaseNormalTarget 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); } diff --git a/Aria/src/main/java/com/arialyy/aria/orm/DelegateWrapper.java b/Aria/src/main/java/com/arialyy/aria/orm/DelegateWrapper.java index 1c909eeb..e87c5b9d 100644 --- a/Aria/src/main/java/com/arialyy/aria/orm/DelegateWrapper.java +++ b/Aria/src/main/java/com/arialyy/aria/orm/DelegateWrapper.java @@ -56,13 +56,6 @@ public class DelegateWrapper { return INSTANCE; } - ///** - // * 保存关联数据 - // */ - //void saveRelationData(AbsWrapper wrapper){ - // - //} - /** * 查询关联表数据 * diff --git a/DEV_LOG.md b/DEV_LOG.md index e911c570..717d4384 100644 --- a/DEV_LOG.md +++ b/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) // 传递多参数 + .start(); + ``` + v_3.5 - fix bug https://github.com/AriaLyy/Aria/issues/302 - fix bug https://github.com/AriaLyy/Aria/issues/283 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 6ac0832b..5ba749c1 100644 --- a/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java @@ -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 { @@ -50,13 +51,14 @@ public class SingleTaskActivity extends BaseActivity { //"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 { //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 { 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 { //.useServerFileName(true) //.setRequestMode(RequestEnum.GET) .setFilePath(path) + .asPost().setParams("key", "value") //.setExtendField("{\n" // + "\"id\":\"你的样子\"\n< > " // + "}") diff --git a/app/src/main/java/com/arialyy/simple/test/AnyRunnModule.java b/app/src/main/java/com/arialyy/simple/test/AnyRunnModule.java index 5388313f..06d3ff43 100644 --- a/app/src/main/java/com/arialyy/simple/test/AnyRunnModule.java +++ b/app/src/main/java/com/arialyy/simple/test/AnyRunnModule.java @@ -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(); diff --git a/app/src/main/java/com/arialyy/simple/upload/HttpUploadActivity.java b/app/src/main/java/com/arialyy/simple/upload/HttpUploadActivity.java index 0304e8eb..048800c9 100644 --- a/app/src/main/java/com/arialyy/simple/upload/HttpUploadActivity.java +++ b/app/src/main/java/com/arialyy/simple/upload/HttpUploadActivity.java @@ -56,7 +56,7 @@ public class HttpUploadActivity extends BaseActivity { "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(); } diff --git a/build.gradle b/build.gradle index f2f07861..365135f9 100644 --- a/build.gradle +++ b/build.gradle @@ -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 下载框架' diff --git a/py/download.py b/py/download.py new file mode 100644 index 00000000..e42927df --- /dev/null +++ b/py/download.py @@ -0,0 +1,21 @@ +# coding=utf-8 + +import os +from flask import Flask, send_from_directory, request + +app = Flask(__name__) + + +@app.route("/download/", 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) # 需要关闭防火墙 diff --git a/py/upload.py b/py/upload.py new file mode 100644 index 00000000..7dd9ad97 --- /dev/null +++ b/py/upload.py @@ -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 = ''' + + Upload File +

图片上传

+
+ + +
+ ''' + + +def allowed_file(filename): + return '.' in filename and \ + filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS + + +@app.route('/uploads/') +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 + '
' + return '200' + return '405' + + +if __name__ == '__main__': + app.run(host='0.0.0.0', port=5000)