diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/HttpOption.java b/Aria/src/main/java/com/arialyy/aria/core/common/HttpOption.java index f46ff768..a431f301 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/HttpOption.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/HttpOption.java @@ -17,6 +17,7 @@ package com.arialyy.aria.core.common; import android.text.TextUtils; import com.arialyy.aria.core.processor.IHttpFileLenAdapter; +import com.arialyy.aria.core.processor.IHttpFileNameAdapter; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CheckUtil; import java.net.Proxy; @@ -35,6 +36,7 @@ public class HttpOption extends BaseOption { private Proxy proxy; private boolean useServerFileName = false; private IHttpFileLenAdapter fileLenAdapter; + private IHttpFileNameAdapter fileNameAdapter; private String attachment; public HttpOption() { @@ -168,4 +170,12 @@ public class HttpOption extends BaseOption { this.fileLenAdapter = fileLenAdapter; return this; } + public HttpOption setFilNameAdapter(IHttpFileNameAdapter fileNameAdapter) { + if (fileNameAdapter == null) { + throw new IllegalArgumentException("adapter为空"); + } + CheckUtil.checkMemberClass(fileNameAdapter.getClass()); + this.fileNameAdapter = fileNameAdapter; + return this; + } } diff --git a/HttpComponent/src/main/java/com/arialyy/aria/http/HttpTaskOption.java b/HttpComponent/src/main/java/com/arialyy/aria/http/HttpTaskOption.java index 7c30d6eb..6da0b544 100644 --- a/HttpComponent/src/main/java/com/arialyy/aria/http/HttpTaskOption.java +++ b/HttpComponent/src/main/java/com/arialyy/aria/http/HttpTaskOption.java @@ -20,6 +20,8 @@ import android.text.TextUtils; import com.arialyy.aria.core.common.RequestEnum; import com.arialyy.aria.core.inf.ITaskOption; import com.arialyy.aria.core.processor.IHttpFileLenAdapter; +import com.arialyy.aria.core.processor.IHttpFileNameAdapter; + import java.lang.ref.SoftReference; import java.net.CookieManager; import java.net.Proxy; @@ -81,17 +83,23 @@ public final class HttpTaskOption implements ITaskOption { private SoftReference fileLenAdapter; + private SoftReference fileNameAdapter; + public IHttpFileLenAdapter getFileLenAdapter() { return fileLenAdapter == null ? null : fileLenAdapter.get(); } - + public IHttpFileNameAdapter getFileNameAdapter() { + return fileNameAdapter == null ? null : fileNameAdapter.get(); + } /** * 如果是匿名内部类,完成后需要将adapter设置为空,否则会出现内存泄漏 */ public void setFileLenAdapter(IHttpFileLenAdapter fileLenAdapter) { this.fileLenAdapter = new SoftReference<>(fileLenAdapter); } - + public void setFileNameAdapter(IHttpFileNameAdapter fileNameAdapter) { + this.fileNameAdapter = new SoftReference<>(fileNameAdapter); + } public Map getFormFields() { return formFields; } @@ -179,4 +187,5 @@ public final class HttpTaskOption implements ITaskOption { public void setParams(Map params) { this.params = params; } + } diff --git a/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDFileInfoTask.java b/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDFileInfoTask.java index eb4a6be0..49ef09ad 100644 --- a/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDFileInfoTask.java +++ b/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDFileInfoTask.java @@ -158,10 +158,21 @@ final class HttpDFileInfoTask implements IInfoTask, Runnable { if (!TextUtils.isEmpty(disposition)) { mEntity.setDisposition(CommonUtil.encryptBASE64(disposition)); handleContentDisposition(disposition); - } else { - ALog.w(TAG, "Content-Disposition对于端字段为空,使用服务器端文件名失败"); + } else{ + if(taskOption.getFileNameAdapter()!=null) { + String newName =taskOption.getFileNameAdapter().handleFileName(headers,mEntity.getKey()); + mEntity.setServerFileName(newName); + renameFile(newName); + }else if(conn.getHeaderField("Content-Type")!=null){ + String contentType=conn.getHeaderField("Content-Type"); + String type=contentType.substring(contentType.indexOf(File.separator)+1); + String newName =mEntity.getFileName()+File.separator+type; + mEntity.setServerFileName(newName); + renameFile(newName); + } } } + CookieManager msCookieManager = new CookieManager(); List cookiesHeader = headers.get("Set-Cookie"); diff --git a/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDGInfoTask.java b/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDGInfoTask.java index 8c8cdd21..7710805d 100644 --- a/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDGInfoTask.java +++ b/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDGInfoTask.java @@ -155,6 +155,11 @@ public final class HttpDGInfoTask implements IInfoTask { // 设置属性 subOption.setFileLenAdapter(groupOption.getFileLenAdapter()); + subOption.setFileNameAdapter(groupOption.getFileNameAdapter()); + subOption.setUseServerFileName(groupOption.isUseServerFileName()); + + + subOption.setFileNameAdapter(groupOption.getFileNameAdapter()); subOption.setRequestEnum(groupOption.getRequestEnum()); subOption.setHeaders(groupOption.getHeaders()); subOption.setProxy(groupOption.getProxy()); diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/TaskOptionParams.java b/PublicComponent/src/main/java/com/arialyy/aria/core/TaskOptionParams.java index 0e188919..3b85d586 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/TaskOptionParams.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/TaskOptionParams.java @@ -22,6 +22,7 @@ import com.arialyy.aria.core.processor.FtpInterceptHandler; import com.arialyy.aria.core.processor.IBandWidthUrlConverter; import com.arialyy.aria.core.processor.IFtpUploadInterceptor; import com.arialyy.aria.core.processor.IHttpFileLenAdapter; +import com.arialyy.aria.core.processor.IHttpFileNameAdapter; import com.arialyy.aria.core.processor.IKeyUrlConverter; import com.arialyy.aria.core.processor.ILiveTsUrlConverter; import com.arialyy.aria.core.processor.ITsMergeHandler; @@ -59,6 +60,7 @@ public class TaskOptionParams { PROCESSORES.add(IBandWidthUrlConverter.class); PROCESSORES.add(IFtpUploadInterceptor.class); PROCESSORES.add(IHttpFileLenAdapter.class); + PROCESSORES.add(IHttpFileNameAdapter.class); PROCESSORES.add(ILiveTsUrlConverter.class); PROCESSORES.add(ITsMergeHandler.class); PROCESSORES.add(IVodTsUrlConverter.class); diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/processor/IHttpFileNameAdapter.java b/PublicComponent/src/main/java/com/arialyy/aria/core/processor/IHttpFileNameAdapter.java new file mode 100644 index 00000000..f6b40239 --- /dev/null +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/processor/IHttpFileNameAdapter.java @@ -0,0 +1,37 @@ +/* + * 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.processor; + +import com.arialyy.aria.core.download.DownloadEntity; +import com.arialyy.aria.core.inf.IEventHandler; + +import java.net.URLConnection; +import java.util.List; +import java.util.Map; + +/** + * Http文件名适配器 + */ +public interface IHttpFileNameAdapter extends IEventHandler { + + /** + * 根据header中的数据获取文件名字 + * @param headers header参数{@link URLConnection#getHeaderFields()} + * @param key 这里如果传entity可能会导致entity属性变更而其他地方没有改变,所以就传了个key 减少可操作范围 + * @return 文件长度 + */ + String handleFileName(Map> headers, String key); +} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/util/DbDataHelper.java b/PublicComponent/src/main/java/com/arialyy/aria/util/DbDataHelper.java index 93168562..bf4d306e 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/util/DbDataHelper.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/util/DbDataHelper.java @@ -105,7 +105,11 @@ public class DbDataHelper { entity.setUrl(url); entity.setFilePath(groupHash + "_" + i); int lastIndex = url.lastIndexOf(File.separator); - entity.setFileName(url.substring(lastIndex + 1)); + //去除url末尾携带的的参数 + int endIndex = url.lastIndexOf("?"); + if(endIndex<0)endIndex=url.length(); + + entity.setFileName(url.substring(lastIndex + 1,endIndex)); entity.setGroupHash(groupHash); entity.setGroupChild(true); list.add(entity); diff --git a/app/src/main/java/com/arialyy/simple/MainActivity.java b/app/src/main/java/com/arialyy/simple/MainActivity.java index f434e8b1..3769f716 100644 --- a/app/src/main/java/com/arialyy/simple/MainActivity.java +++ b/app/src/main/java/com/arialyy/simple/MainActivity.java @@ -142,7 +142,7 @@ public class MainActivity extends BaseActivity { T.showShort(MainActivity.this, "没有文件读写权限"); finish(); } - }, Manifest.permission.WRITE_EXTERNAL_STORAGE); + }, Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE); } } }