From 569c02516901345de71a144e1da65e5ebe8bbb9e Mon Sep 17 00:00:00 2001 From: frank wang <425294756@qq.com> Date: Thu, 30 Oct 2025 18:00:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=B8=8A=E4=BC=A0=E7=9A=84?= =?UTF-8?q?=E9=80=BB=E8=BE=91002?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/pxkj/jwzs/FirstActivity.java | 84 ++++++++++++------- 1 file changed, 54 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/com/pxkj/jwzs/FirstActivity.java b/app/src/main/java/com/pxkj/jwzs/FirstActivity.java index f5209e1..c101e6d 100644 --- a/app/src/main/java/com/pxkj/jwzs/FirstActivity.java +++ b/app/src/main/java/com/pxkj/jwzs/FirstActivity.java @@ -246,19 +246,6 @@ public class FirstActivity extends BaseActivity { // || url.contains("/api/file/"); } - private void previewFile(String url) { - try { - if (webView != null) { - webView.loadUrl(url); - } - - Toast.makeText(this, "正在打开文件预览...", Toast.LENGTH_SHORT).show(); - } catch (Exception e) { - e.printStackTrace(); - Toast.makeText(this, "预览文件失败: " + e.getMessage(), Toast.LENGTH_LONG).show(); - } - } - private long currentDownloadId = -1; // 保存当前下载任务ID /** * ✅ 下载文件并调用系统 DownloadManager @@ -429,7 +416,7 @@ public class FirstActivity extends BaseActivity { private void uploadFile(Uri uri) { new Thread(() -> { try { - // ✅ 上传接口 + // ✅ 上传接口地址 String uploadUrl = "http://20.47.196.98:40080/fwzx/v1.0.0/getServiceInfoByServiceId/32092423SJNB12202507111046530001/320924230000-3-0800-5CDEC9969D674E1CBB7E2F86A697B0D7/api/file/upload"; String token = getToken(); @@ -438,32 +425,40 @@ public class FirstActivity extends BaseActivity { return; } - // ✅ 获取文件名 + // ✅ 获取文件名和大小 String fileName = getFileNameFromUri(uri); if (TextUtils.isEmpty(fileName)) fileName = "upload_file"; + long fileSize = getFileSizeFromUri(uri); - // ✅ 读取文件内容并计算文件大小 + // ✅ 使用流式读取方式构建 RequestBody(防止 OOM) InputStream inputStream = getContentResolver().openInputStream(uri); - byte[] fileBytes = new byte[inputStream.available()]; - inputStream.read(fileBytes); - inputStream.close(); + okhttp3.RequestBody fileBody = new okhttp3.RequestBody() { + @Nullable + @Override + public okhttp3.MediaType contentType() { + return okhttp3.MediaType.parse("application/octet-stream"); + } - long fileSize = fileBytes.length; // 文件大小(字节) + @Override + public void writeTo(okio.BufferedSink sink) throws IOException { + byte[] buffer = new byte[4096]; + int bytesRead; + while ((bytesRead = inputStream.read(buffer)) != -1) { + sink.write(buffer, 0, bytesRead); + } + inputStream.close(); + } + }; - // ✅ 构建 OkHttp 上传请求 - okhttp3.OkHttpClient client = new okhttp3.OkHttpClient(); - okhttp3.RequestBody fileBody = okhttp3.RequestBody.create( - okhttp3.MediaType.parse("application/octet-stream"), - fileBytes - ); okhttp3.MultipartBody requestBody = new okhttp3.MultipartBody.Builder() .setType(okhttp3.MultipartBody.FORM) .addFormDataPart("file", fileName, fileBody) .build(); + okhttp3.OkHttpClient client = new okhttp3.OkHttpClient(); okhttp3.Request request = new okhttp3.Request.Builder() .url(uploadUrl) - .addHeader("accesstoken", token) // ✅ 加 token + .addHeader("accesstoken", token) // ✅ 带 token .post(requestBody) .build(); @@ -476,13 +471,12 @@ public class FirstActivity extends BaseActivity { String safeResult = result.replace("'", "\\'"); String safeFileName = fileName.replace("'", "\\'"); - // ✅ JS 回调:fileName、data、fileSize + // ✅ JS 回调 fileName、data、fileSize runOnUiThread(() -> { Toast.makeText(this, "上传成功", Toast.LENGTH_SHORT).show(); - if (webView != null) { String jsCode = String.format( - "window.onFileUploadSuccess && window.onFileUploadSuccess({fileName: '%s', data: %s, fileSize: %.2f})", + "window.onFileUploadSuccess && window.onFileUploadSuccess({fileName: '%s', data: %s, fileSize: %d})", safeFileName, safeResult, fileSize ); webView.evaluateJavascript(jsCode, null); @@ -503,6 +497,36 @@ public class FirstActivity extends BaseActivity { }).start(); } + /** + * ✅ 获取文件大小(兼容 content:// 与 file://) + */ + private long getFileSizeFromUri(Uri uri) { + long fileSize = 0L; + if ("content".equals(uri.getScheme())) { + try (Cursor cursor = getContentResolver().query(uri, null, null, null, null)) { + if (cursor != null && cursor.moveToFirst()) { + int sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE); + if (sizeIndex != -1) { + fileSize = cursor.getLong(sizeIndex); + } + } + } catch (Exception e) { + e.printStackTrace(); + } + } + if (fileSize <= 0) { + // 尝试从 File 读取 + String path = uri.getPath(); + if (path != null) { + java.io.File file = new java.io.File(path); + if (file.exists()) { + fileSize = file.length(); + } + } + } + return fileSize; + } + /** * 从 Uri 获取文件名