public void download(Long fileId, String downFilePath, Long tenantId, Boolean…
- User
public void download(Long fileId, String downFilePath, Long tenantId, Boolean replaceAddress) { Instant start = Instant.now(); log.info("Starting file download: fileId={}, path={}, tenantId={}", fileId, downFilePath, tenantId); String downloadTmpFileUrl = platformClient.getDownloadTmpFileUrl(fileId, tenantId, replaceAddress); log.info("Download URL acquired: {}", downloadTmpFileUrl); try { ResponseEntity<byte[]> response = restTemplate.getForEntity(downloadTmpFileUrl, byte[].class); if (!response.getStatusCode().is2xxSuccessful()) { log.error("下载文件:{},状态码:{}", fileId, response.getStatusCode().value()); throw new ClientException("File download failed. Status: " + response.getStatusCodeValue()); }
codeFile file = new File(downFilePath); Files.createDirectories(file.getParentFile().toPath()); try (FileOutputStream fos = new FileOutputStream(file)) { StreamUtils.copy(response.getBody(), fos); } Instant end = Instant.now(); double durationSeconds = Duration.between(start, end).toMillis() / 1000.0; log.info("File: {} downloaded successfully, Time taken: {:.3f} seconds", fileId, durationSeconds); } catch (IOException e) { log.error("Error downloading file: {}", fileId, e); throw new ClientException("File download failed"); } catch (Exception e) { log.error("Unexpected error during file download: {}", fileId, e); throw new ClientException("Unexpected error during file download"); } } 优化代码 Assistant