2727import java .util .concurrent .atomic .AtomicBoolean ;
2828import java .util .concurrent .atomic .AtomicInteger ;
2929import java .util .concurrent .locks .ReentrantLock ;
30+ import java .util .regex .Matcher ;
31+ import java .util .regex .Pattern ;
3032
3133/**
3234 * Created by cenxiaozhong on 2017/5/13.
@@ -176,7 +178,7 @@ private void preDownload(String url, String contentDisposition, long contentLeng
176178
177179
178180 if (ExecuteTasksMap .getInstance ().contains (url )) {
179- if (mAgentWebUIController .get ()!= null ){
181+ if (mAgentWebUIController .get () != null ) {
180182 mAgentWebUIController .get ().showMessage (
181183 mDownLoadMsgConfig .getTaskHasBeenExist (), TAG .concat ("|preDownload" ));
182184 }
@@ -271,15 +273,50 @@ private File getFile(String contentDisposition, String url) {
271273 if (filename .length () > 64 ) {
272274 filename = filename .substring (filename .length () - 64 , filename .length ());
273275 }
274- LogUtils .i (TAG , "filename:" + filename );
276+ LogUtils .i (TAG , "filename:" + filename + " contentDisposition:" + contentDisposition );
275277 return AgentWebUtils .createFileByName (mContext , filename , false );
278+
279+
280+ /* String fileName = getFileName(contentDisposition);
281+ if (TextUtils.isEmpty(fileName)) {
282+ fileName = url.substring(url.lastIndexOf('/') + 1);
283+ }
284+ if (fileName.startsWith("\"")) {
285+ fileName = fileName.substring(1);
286+ }
287+ if (fileName.endsWith("\"")) {
288+ fileName = fileName.substring(0, fileName.length() - 1);
289+ }
290+
291+ if (TextUtils.isEmpty(fileName)) {
292+ fileName = AgentWebUtils.md5(url);
293+ }
294+ LogUtils.i(TAG, "======> download file name:" + fileName);
295+ if (fileName.length() > 64) {
296+ fileName = fileName.substring(fileName.length() - 64, fileName.length());
297+ }
298+ LogUtils.i(TAG, "filename:" + fileName);
299+ return AgentWebUtils.createFileByName(mContext, fileName, false);
300+ */
276301 } catch (Exception e ) {
277302 e .printStackTrace ();
278303 }
279304
280305 return null ;
281306 }
282307
308+ private String getFileName (String contentDisposition ) {
309+ if (TextUtils .isEmpty (contentDisposition )) {
310+ return "" ;
311+ }
312+ Matcher m = Pattern .compile (".*filename=(.*)" ).matcher (contentDisposition .toLowerCase ());
313+ if (m .find ()) {
314+ return m .group (1 );
315+ } else {
316+ return "" ;
317+ }
318+ }
319+
283320 @ Override
284321 public void success (String path ) {
285322
@@ -290,7 +327,7 @@ public void success(String path) {
290327 return ;
291328 }
292329 for (DownLoadResultListener mDownLoadResultListener : mDownLoadResultListeners ) {
293- if (mDownLoadResultListener != null ){
330+ if (mDownLoadResultListener != null ) {
294331 mDownLoadResultListener .success (path );
295332 }
296333 }
@@ -303,7 +340,7 @@ public void error(String path, String resUrl, String cause, Throwable e) {
303340 ExecuteTasksMap .getInstance ().removeTask (path );
304341
305342 if (AgentWebUtils .isEmptyCollection (mDownLoadResultListeners )) {
306- if (mAgentWebUIController .get ()!= null ){
343+ if (mAgentWebUIController .get () != null ) {
307344 mAgentWebUIController .get ().showMessage (mDownLoadMsgConfig .getDownLoadFail (), TAG .concat ("|error" ));
308345 }
309346 return ;
0 commit comments