文件上传的逻辑

This commit is contained in:
frank wang 2025-10-30 16:58:16 +08:00
parent f586a6d0e3
commit f8adef2a9e
1 changed files with 46 additions and 52 deletions

View File

@ -262,50 +262,52 @@ public class FirstActivity extends BaseActivity {
private long currentDownloadId = -1; // 保存当前下载任务ID
private void downloadFile(String url) {
try {
if (TextUtils.isEmpty(url)) {
Toast.makeText(this, "下载地址为空", Toast.LENGTH_SHORT).show();
return;
new Thread(() -> {
try {
if (TextUtils.isEmpty(url)) {
Toast.makeText(this, "下载地址为空", Toast.LENGTH_SHORT).show();
return;
}
if (url.startsWith("blob:") || url.startsWith("data:")) {
Toast.makeText(this, "无法直接下载 Blob 文件,请检查前端下载逻辑", Toast.LENGTH_SHORT).show();
return;
}
Uri uri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(uri);
String fileName = URLUtil.guessFileName(url, null, null);
request.setTitle(fileName);
request.setDescription("正在下载文件...");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
request.allowScanningByMediaScanner();
request.addRequestHeader("User-Agent", System.getProperty("http.agent"));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
request.setRequiresCharging(false);
request.setAllowedOverMetered(true);
request.setAllowedOverRoaming(true);
}
String mimeType = getMimeTypeForUrl(url);
request.setMimeType(!TextUtils.isEmpty(mimeType) ? mimeType : "application/octet-stream");
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
if (dm != null) {
Toast.makeText(this, "文件已开始下载,请等待", Toast.LENGTH_SHORT).show();
currentDownloadId = dm.enqueue(request);
startDownloadProgressMonitor(dm, currentDownloadId);
} else {
Toast.makeText(this, "系统下载服务不可用", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "下载失败: " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
if (url.startsWith("blob:") || url.startsWith("data:")) {
Toast.makeText(this, "无法直接下载 Blob 文件,请检查前端下载逻辑", Toast.LENGTH_SHORT).show();
return;
}
Uri uri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(uri);
String fileName = URLUtil.guessFileName(url, null, null);
request.setTitle(fileName);
request.setDescription("正在下载文件...");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
request.allowScanningByMediaScanner();
request.addRequestHeader("User-Agent", System.getProperty("http.agent"));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
request.setRequiresCharging(false);
request.setAllowedOverMetered(true);
request.setAllowedOverRoaming(true);
}
String mimeType = getMimeTypeForUrl(url);
request.setMimeType(!TextUtils.isEmpty(mimeType) ? mimeType : "application/octet-stream");
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
if (dm != null) {
Toast.makeText(this, "文件已开始下载,请等待", Toast.LENGTH_SHORT).show();
currentDownloadId = dm.enqueue(request);
startDownloadProgressMonitor(dm, currentDownloadId);
} else {
Toast.makeText(this, "系统下载服务不可用", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "下载失败: " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
}).start();
}
/**
@ -553,15 +555,6 @@ public class FirstActivity extends BaseActivity {
}
}
// 供前端调用的文件选择方法
@JavascriptInterface
public void previewFile(String url) {
FirstActivity activity = activityRef.get();
if (activity != null && !TextUtils.isEmpty(url)) {
activity.runOnUiThread(() -> activity.previewFile(url));
}
}
// 供前端调用的文件选择方法
@JavascriptInterface
public void setToken(String token) {
@ -570,6 +563,7 @@ public class FirstActivity extends BaseActivity {
activity.runOnUiThread(() -> activity.setToken(token));
}
}
@JavascriptInterface
public void showLog(String message) {
FirstActivity activity = activityRef.get();