Описание проблемы
Когда я получаю данные журнала из службы и сохраняю их как файл .gz, полученный файл по адресу C:\sgtw\downInternal\system.log-2024-07-21-1721500200.gz содержит внутри другой файл GZIP с тем же именем. Этот внутренний файл GZIP при извлечении содержит фактические данные журнала.
Ожидаемое поведение
Я хочу, чтобы данные журнала сохранялись непосредственно в GZIP. файл без какой-либо вложенной структуры. Окончательный результат должен представлять собой один файл .gz, содержащий непосредственно данные журнала.
Фрагмент кода
Вот соответствующая часть моего код контроллера:
Код: Выделить всё
@Controller
public class MyController {
@PostMapping("/download-logs")
public String downloadLogs(@RequestParam List entryCheckbox,
@RequestParam String token,
@RequestParam Map allRequestParams,
Model model) {
System.out.println("Inside downloadLogs method of controller");
System.out.println("Token value is: " + token);
List successfullyDownloadedFiles = new ArrayList();
// Check if any log files are selected
if (entryCheckbox.isEmpty()) {
model.addAttribute("errorMessage", "No log files selected for download.");
return "downloaded_sgtw"; // Redirect to a result page with an error
}
try {
String ids = String.join(",", entryCheckbox);
byte[] logData = sgtwService.fetchLogsFromMockService(ids, token.replace("Bearer ", ""));
// Check if the fetched log data is valid
if (logData == null || logData.length == 0) {
model.addAttribute("errorMessage", "No log data retrieved. Please check the selected log files.");
return "downloaded_sgtw"; // Redirect to a result page with an error
}
// Retrieve the log name from the request parameters which should include the .gz extension
String logName = allRequestParams.get("logName_" + entryCheckbox.get(0));
if (logName == null || !logName.endsWith(".gz")) {
model.addAttribute("errorMessage", "Invalid log name. Ensure it includes the .gz extension.");
return "downloaded_sgtw"; // Redirect to a result page with an error
}
// Specify the directory where you want to save the file
String destinationDir = "C:\\sgtw\\downInternal";
String destinationPath = Paths.get(destinationDir, logName).toString(); // Use the log name directly
System.out.println("Destination Path: " + destinationPath);
System.out.println("Log data byte array length: " + logData.length);
// Write the log data directly to the file
try (FileOutputStream fileOutputStream = new FileOutputStream(destinationPath)) {
fileOutputStream.write(logData); // Write the byte array directly
} // Automatically closes the stream here
successfullyDownloadedFiles.add(logName);
// Processing for success messages
model.addAttribute("successMessage", "Logs saved successfully at: " + destinationPath);
} catch (IOException e) {
e.printStackTrace();
model.addAttribute("errorMessage", "Error occurred while downloading logs: " + e.getMessage());
}
return "downloaded_sgtw"; // Redirect to the result page
}
}
- Я запускаю приложение в среде Windows.
- Ожидаемый результат — один файл .gz, содержащий данные журнала, но вместо этого я получаю вложенную файловую структуру .gz.
Как я могу изменить свою реализацию чтобы гарантировать, что данные журнала сохраняются непосредственно в файле GZIP без каких-либо вложенных файлов GZIP? Любая помощь будет принята с благодарностью!
Подробнее здесь: https://stackoverflow.com/questions/793 ... in-windows