文件下载提交

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