|
|
@@ -30,15 +30,12 @@ public class DownloadUtils {
|
|
|
&& Environment.getExternalStorageDirectory().canWrite()) {
|
|
|
cacheDir = ContextUtil.getApplication().getExternalCacheDir();
|
|
|
} else {
|
|
|
- cacheDir = ContextUtil.getApplication().getCacheDir();
|
|
|
+ cacheDir = ContextUtil.getApplication().getExternalCacheDir();
|
|
|
+// cacheDir = ContextUtil.getApplication().getCacheDir();
|
|
|
}
|
|
|
return cacheDir;
|
|
|
}
|
|
|
|
|
|
- public static void downLoad(String url, final File rootFile, final String fileName,
|
|
|
- final FileDownLoadObserver<File> fileDownLoadObserver) {
|
|
|
- downLoad(new OkHttpClient(), url, rootFile, fileName, fileDownLoadObserver);
|
|
|
- }
|
|
|
|
|
|
public static void downLoad(OkHttpClient client, String url, final File rootFile, final String fileName,
|
|
|
final FileDownLoadObserver<File> fileDownLoadObserver) {
|
|
|
@@ -77,4 +74,51 @@ public class DownloadUtils {
|
|
|
}
|
|
|
|
|
|
|
|
|
+ public static void downLoadCompareBeforeLocal(OkHttpClient client, String url, final File localFile, final File rootFile, final String fileName,
|
|
|
+ final FileDownLoadObserver<File> fileDownLoadObserver) {
|
|
|
+ Observable.create((ObservableOnSubscribe<Object>) emitter -> {
|
|
|
+ Request request = new Request.Builder()
|
|
|
+ .url(url)
|
|
|
+ .build();
|
|
|
+ try (Response response = client.newCall(request).execute()) {
|
|
|
+ if (!response.isSuccessful()) {
|
|
|
+ emitter.onError(new IOException("Failed to download audio: " + response));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ ResponseBody body = response.body();
|
|
|
+ if (body == null) {
|
|
|
+ emitter.onError(new IOException("Response body is null"));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+// AtmobLog.d("zk", String.format("文件类型为%s,文件大小为%d", response.body().contentType().toString(), response.body().contentLength()));
|
|
|
+ if (localFile == null || !localFile.exists() || localFile.length() != body.contentLength()) {
|
|
|
+ emitter.onNext(body);
|
|
|
+ } else {
|
|
|
+ emitter.onNext(localFile);
|
|
|
+ }
|
|
|
+ emitter.onComplete();
|
|
|
+ } catch (IOException e) {
|
|
|
+ emitter.onError(e);
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .map(data -> {
|
|
|
+ if (data instanceof File) {
|
|
|
+ return (File) data;
|
|
|
+ } else {
|
|
|
+ ResponseBody responseBody = (ResponseBody) data;
|
|
|
+ return fileDownLoadObserver.saveFile(responseBody,
|
|
|
+ rootFile.getPath(), fileName);
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .subscribeOn(Schedulers.io())
|
|
|
+ .observeOn(Schedulers.io())
|
|
|
+ .map(file -> {
|
|
|
+ if (file == null) {
|
|
|
+ throw new IOException("file is null");
|
|
|
+ }
|
|
|
+ return file;
|
|
|
+ })
|
|
|
+ .subscribe(fileDownLoadObserver);
|
|
|
+ }
|
|
|
+
|
|
|
}
|