文件下载提交

This commit is contained in:
frank wang 2025-10-15 12:07:56 +08:00
parent 31ffe13029
commit 14f462050c
1 changed files with 37 additions and 27 deletions

View File

@ -38,6 +38,7 @@ public class FirstActivity extends BaseActivity {
// 扩展的 MIME 类型映射 // 扩展的 MIME 类型映射
private static final Map<String, String> MIME_TYPES = new HashMap<>(); private static final Map<String, String> MIME_TYPES = new HashMap<>();
static { static {
MIME_TYPES.put("js", "text/javascript"); MIME_TYPES.put("js", "text/javascript");
MIME_TYPES.put("mjs", "text/javascript"); MIME_TYPES.put("mjs", "text/javascript");
@ -237,6 +238,7 @@ public class FirstActivity extends BaseActivity {
// || url.endsWith(".xlsx") // || url.endsWith(".xlsx")
// || url.contains("/api/file/"); // || url.contains("/api/file/");
} }
private long currentDownloadId = -1; // 保存当前下载任务ID private long currentDownloadId = -1; // 保存当前下载任务ID
private void downloadFile(String url) { private void downloadFile(String url) {
@ -274,7 +276,7 @@ public class FirstActivity extends BaseActivity {
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
if (dm != null) { if (dm != null) {
currentDownloadId = dm.enqueue(request); currentDownloadId = dm.enqueue(request);
Toast.makeText(this, "文件已开始下载,请等待", Toast.LENGTH_SHORT).show(); Toast.makeText(this, "文件已开始下载,请等待" + currentDownloadId, Toast.LENGTH_SHORT).show();
startDownloadProgressMonitor(dm, currentDownloadId); startDownloadProgressMonitor(dm, currentDownloadId);
} else { } else {
Toast.makeText(this, "系统下载服务不可用", Toast.LENGTH_SHORT).show(); Toast.makeText(this, "系统下载服务不可用", Toast.LENGTH_SHORT).show();
@ -290,7 +292,7 @@ public class FirstActivity extends BaseActivity {
* 监控下载进度每秒回调一次到前端 * 监控下载进度每秒回调一次到前端
*/ */
private void startDownloadProgressMonitor(DownloadManager dm, long downloadId) { private void startDownloadProgressMonitor(DownloadManager dm, long downloadId) {
new Thread(() -> {
DownloadManager.Query query = new DownloadManager.Query(); DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(downloadId); query.setFilterById(downloadId);
boolean downloading = true; boolean downloading = true;
@ -299,25 +301,31 @@ public class FirstActivity extends BaseActivity {
try { try {
Thread.sleep(1000); Thread.sleep(1000);
android.database.Cursor cursor = dm.query(query); android.database.Cursor cursor = dm.query(query);
Toast.makeText(this, "文件已开始下载,请等待" + cursor, Toast.LENGTH_SHORT).show();
if (cursor != null && cursor.moveToFirst()) { if (cursor != null && cursor.moveToFirst()) {
int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)); int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
long total = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)); long total = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
long downloaded = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)); long downloaded = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
Toast.makeText(this, "文件已开始下载,请等待" + status + "," + total + "," + downloaded, Toast.LENGTH_SHORT).show();
if (status == DownloadManager.STATUS_RUNNING && total > 0) { if (status == DownloadManager.STATUS_RUNNING && total > 0) {
int progress = (int) ((downloaded * 100L) / total); int progress = (int) ((downloaded * 100L) / total);
Toast.makeText(this, "1.文件已开始下载部分进度调用前端js" + status + "," + total + "," + downloaded, Toast.LENGTH_SHORT).show();
handler.post(() -> sendProgressToJs(progress)); handler.post(() -> sendProgressToJs(progress));
} else if (status == DownloadManager.STATUS_SUCCESSFUL) { } else if (status == DownloadManager.STATUS_SUCCESSFUL) {
Toast.makeText(this, "2.文件已开始下载进度成功调用前端js" + status + "," + total + "," + downloaded, Toast.LENGTH_SHORT).show();
downloading = false; downloading = false;
handler.post(() -> sendProgressToJs(100)); handler.post(() -> sendProgressToJs(100));
} else if (status == DownloadManager.STATUS_FAILED) { } else if (status == DownloadManager.STATUS_FAILED) {
Toast.makeText(this, "3.文件已开始下载进度失败调用前端js" + status + "," + total + "," + downloaded, Toast.LENGTH_SHORT).show();
downloading = false; downloading = false;
handler.post(() -> sendProgressToJs(-1)); handler.post(() -> sendProgressToJs(-1));
} }
cursor.close(); cursor.close();
} }
} catch (Exception ignored) {} } catch (Exception ignored) {
Toast.makeText(this, "文件已开始下载,请等待报错", Toast.LENGTH_SHORT).show();
}
} }
}).start();
} }
private void sendProgressToJs(int progress) { private void sendProgressToJs(int progress) {
@ -326,7 +334,9 @@ public class FirstActivity extends BaseActivity {
String js = String.format("if(window.onDownloadProgress){window.onDownloadProgress(%d);}", progress); String js = String.format("if(window.onDownloadProgress){window.onDownloadProgress(%d);}", progress);
webView.evaluateJavascript(js, null); webView.evaluateJavascript(js, null);
} }
} catch (Exception ignored) {} } catch (Exception ignored) {
Toast.makeText(this, "文件已开始下载请等待报错sendProgressToJs", Toast.LENGTH_SHORT).show();
}
} }
public static class JsBridge { public static class JsBridge {