From b86d0bf82f29412574011496d2ef5ea9fb34ec8d Mon Sep 17 00:00:00 2001 From: Niko <1377382065@qq.com> Date: Tue, 5 May 2026 19:36:30 +0800 Subject: [PATCH] Use createReadStream instead of saveAs for Excel download Bypasses saveAs filesystem path which can produce 0-byte files in headless server environments. Co-Authored-By: Claude Opus 4.7 --- src/main/java/com/ets/scraper/EtsScraper.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/ets/scraper/EtsScraper.java b/src/main/java/com/ets/scraper/EtsScraper.java index a8f4bca..47c7902 100644 --- a/src/main/java/com/ets/scraper/EtsScraper.java +++ b/src/main/java/com/ets/scraper/EtsScraper.java @@ -214,12 +214,21 @@ public class EtsScraper { page.evaluate("document.querySelectorAll('button').forEach(b => { if (b.textContent.trim() === '导出') b.click(); })"); }); System.out.println("[*] Waiting for download to complete..."); - dl.saveAs(savedFile); - System.out.println("[+] Download saved to: " + savedFile); - if (java.nio.file.Files.size(savedFile) == 0) { + long[] totalBytes = {0}; + try (java.io.InputStream stream = dl.createReadStream(); + java.io.OutputStream out = Files.newOutputStream(savedFile)) { + byte[] buf = new byte[8192]; + int n; + while ((n = stream.read(buf)) > 0) { + out.write(buf, 0, n); + totalBytes[0] += n; + } + } + System.out.println("[+] Download saved to: " + savedFile + " (" + totalBytes[0] + " bytes)"); + if (totalBytes[0] == 0) { System.out.println("[-] Downloaded file is empty"); } else { - System.out.println("[+] Download size: " + java.nio.file.Files.size(savedFile) + " bytes"); + System.out.println("[+] Download size: " + totalBytes[0] + " bytes"); // Auto-import to ets-proxy autoImportBill(savedFile, proxyHost, proxyUser, proxyPass); }