how should the filename be saved as?
@GetMapping("{trace-id}")
@ResponseStatus(HttpStatus.OK)
// @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "Successful", content = @Content(mediaType = "text/csv"))})
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Successful", content = @Content(mediaType = "application/octet-stream"))
})
public void downloadCSVFindByUUID(@TraceIdNonExistent @PathVariable("trace-id") final String traceId, final HttpServletResponse servletResponse) {
csvReadWriteService.writeCsvFileToResponse(traceId, servletResponse);
}
// ----------------------------------
private void writeCsvResponse(final String client, final String uuid, final List<CsvEntryResp> csvRespList, final OutputStream out) {
final String[] memberFieldsToBindTo = FieldExtractorUtil.getFieldNames(CsvEntryResp.class);
final ColumnPositionMappingStrategy<CsvEntryResp> strategy = new ColumnPositionMappingStrategy<>();
strategy.setType(CsvEntryResp.class);
strategy.setColumnMapping(memberFieldsToBindTo);
log.info("PrintWriter ClientId: {}. List-CsvEntryResp-Count: {}", client, csvRespList.size());
try (final GZIPOutputStream gzip = new GZIPOutputStream(out);
final OutputStreamWriter osw = new OutputStreamWriter(gzip, StandardCharsets.UTF_8);
final PrintWriter writer = new PrintWriter(osw)) {
final List<String> list = new ArrayList<>();
Collections.addAll(list, memberFieldsToBindTo);
final var header = String.format("%s%s", list.toString()
.replaceAll("\[", "")
.replaceAll("\]", "")
.replaceAll(" ", ""), "n");
final StatefulBeanToCsv<CsvEntryResp> beanToCsv = new StatefulBeanToCsvBuilder<CsvEntryResp>(writer)
.withMappingStrategy(strategy)
.withQuotechar(NO_QUOTE_CHARACTER)
.build();
writer.write(header);
beanToCsv.write(csvRespList.parallelStream());
writer.flush();
log.info("Completed writing CSV Response File-Length: {} with clientId: {}", gzip.toString().getBytes(StandardCharsets.UTF_8).length, client);
} catch (Exception e) {
log.error("CSV writer error. {}", e.getMessage(), e);
throw new CsvFileProcessingException(uuid, null, String.format("CSV writer error. %s", e.getMessage()));
}
}
I tried renaming the gzip filename. it won’t save with the correct name.