Я использую Spring boot 1.5, и я пытаюсь экспортировать CSV-файл, используя этот код, но не знаю, как передать идентификатор, который я получаю из внешнего интерфейса в контроллере, я провел некоторые исследования, но втщетно, спасибо за помощь!
это услуга
@Override
public InputStream exportAlerts(Long id) throws IOException {
List<ExportSuggestVO> exportSuggestVOS = new ArrayList<>();
final String[] header = new String[]{"id", "alertStatus", "addedBy", "addedByEmail", "uo", "added" };
final CellProcessor[] processors = getProcessors();
Alert alert = alertRepository.findById(id);
{
OrganisationUnitVO organisationUnitVO = null;
try {
organisationUnitVO = userFromUoProvider.getSite(alert.getAddedBy());
} catch (Exception e) {
logger.error("not found", e);
}
exportSuggestVOS.add(new ExportSuggestVO.Builder()
.setId(alert.getId() == null ? null : alert.getId())
.setAlertStatus(alert.getAlertStatus() == null ? null : alert.getAlertStatus().getLabel())
.setAddedBy(String.valueOf(getEmployeeDetails(alert.getAddedBy())))
.setAddedByEmail(getEmployeeDetails(alert.getAddedBy()) == null ? " "
: getEmployeeDetails(alert.getAddedBy()).getEmail())
.setUo(organisationUnitVO == null ? " " : organisationUnitVO.getName())
.setAdded(alert.getAdded() == null ? "" : alert.getAdded().toString().replace("T"," "))
.build());}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
CsvBeanWriter beanWriter = null;
try{
beanWriter = new CsvBeanWriter(new OutputStreamWriter(outputStream, "ISO-8859-1"),
CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE);
beanWriter.writeHeader("id", "Status", "Auteur", "Auteur-email", "Site/UO", "Date création");
for (ExportSuggestVO exportSuggestVO : exportSuggestVOS){
beanWriter.write(exportSuggestVO, header,processors);
}
} finally {
if (beanWriter != null){
beanWriter.close();
}
}
return new ByteArrayInputStream(outputStream.toByteArray()); }
контроллер
@RequestMapping(value = "/downloadAlert", method = RequestMethod.POST)
public void exportAllSuggest(HttpServletResponse response)
throws IOException {
InputStream in = alertService.exportAlerts();
try {
response.setContentType("text/csv;charset=UTF-8");
response.addHeader("Content-Disposition", "attachment; filename = Alerts" + LocalDate.now() + ".csv");
OutputStream outputStream = response.getOutputStream();
IOUtils.copy(new BufferedInputStream(in), outputStream);
in.close();
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}