43 lines
1.9 KiB
Java
43 lines
1.9 KiB
Java
public boolean downloadTradeBill(String time, String filePath) {
|
|
// time = "2023-11-16"; 事例
|
|
// 获取交易账单
|
|
RSAAutoCertificateConfig config = this.configManager.getConfig("payOrder");
|
|
BillDownloadServiceExtension service = new BillDownloadServiceExtension.Builder().config(config).build();
|
|
GetTradeBillRequest request = new GetTradeBillRequest();
|
|
request.setBillDate(time);
|
|
request.setBillType(BillType.ALL);
|
|
request.setTarType(TarType.GZIP);
|
|
DigestBillEntity bill = service.getTradeBill(request);
|
|
|
|
try (InputStream inputStream = bill.getInputStream()) {
|
|
// 使用有缓存的 BufferedOutputStream
|
|
try (BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(filePath))) {
|
|
// 处理大文件时,常用的缓冲区大小为 8192 或 16384。
|
|
// 不过,最佳缓冲区大小可能取决于具体的硬件和系统配置。
|
|
byte[] buffer = new byte[16384];
|
|
int bytesRead;
|
|
while ((bytesRead = inputStream.read(buffer)) != -1) {
|
|
outputStream.write(buffer, 0, bytesRead);
|
|
}
|
|
}
|
|
} catch (IOException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
|
|
// 验证数据流中已读取数据的摘要
|
|
if (bill.verifyHash()) {
|
|
// 账单是完整准确,可以开启后续操作,例如完成每日对账。
|
|
log.info("微信账单下载验证成功!");
|
|
return true;
|
|
} else {
|
|
// 账单不完整或者被篡改,应清理之前保存的文件
|
|
try {
|
|
Path path = Paths.get(filePath);
|
|
Files.delete(path);
|
|
} catch (IOException e) {
|
|
log.error(e.toString());
|
|
}
|
|
return false;
|
|
}
|
|
}
|