Я использую Spring boot 1.5, я пытаюсь загрузить csv-файл с конкретной информацией, как это показано в моем сервисе.На самом деле, когда я тестирую в рестлете, ответ содержит всю информацию, которая мне нужна, в моем CSV-файле, но проблема в том, что он не загружается.Я не знаю, что именно здесь имеет значение.Пожалуйста, помогите.
Контроллер:
@RequestMapping(value = "/downloadAlert/{id}", method = RequestMethod.POST)
public void exportAllSuggest(@PathVariable final Long id, HttpServletResponse response)
throws IOException {
InputStream in = alertService.exportAlerts(id);
try {
response.setContentType("text/csv;charset=UTF-8");
response.addHeader("Content-Disposition", "attachment; filename = Alert" + LocalDate.now() + ".csv");
OutputStream outputStream = response.getOutputStream();
IOUtils.copy(new BufferedInputStream(in), outputStream);
in.close();
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
А вот у меня сервис:
@Override
public InputStream exportAlerts(Long id) throws IOException {
List<ExportSuggestVO> exportSuggestVOS = new ArrayList<>();
final String[] header = new String[]{"id", "alertStatus","added", "addedBy",
"addedByEmail", "uo" , "assigneeMatricule", "assigneeEmail", "closed", "content"};
final CellProcessor[] processors = getProcessors();
Alert alert = alertRepository.findById(id);
{
exportSuggestVOS.add(new ExportSuggestVO.Builder()
.setId(alert.getId() == null ? null : alert.getId())
.setAlertStatus(alert.getAlertStatus() == null ? " "
: alert.getAlertStatus().getLabel())
.setAdded(alert.getAdded() == null ? " "
: alert.getAdded().toString().replace("T", " "))
.setAddedBy(alert.getAddedBy() == null ? " "
: alert.getAddedBy())
.setAddedByEmail(intelciaCoreService.getEmployeeDetails(alert.getAddedBy()) == null ? " "
: intelciaCoreService.getEmployeeDetails(alert.getAddedBy()).getEmail())
.setUo(intelciaCoreService.getEmployeeDetails(alert.getAddedBy()) == null ? " "
: intelciaCoreService.getEmployeeDetails(alert.getAddedBy()).getSite())
.setAssigneeMatricule(alert.getAssignee() == null ? " "
: alert.getAssignee())
.setAssigneeEmail(intelciaCoreService.getEmployeeDetails(alert.getAssignee()) == null ? " "
: intelciaCoreService.getEmployeeDetails(alert.getAssignee()).getEmail())
.setClosed(alert.getClosed() == null ? " "
: alert.getClosed().toString().replace("T", " "))
.setContent(alert.getObject() == null ? " "
: alert.getObject())
.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", "Date de creation", "Auteur", "Auteur-email", "Site/UO",
"Assignee", "Assignee-email", "Date de fermeture" , "Contenu");
for (ExportSuggestVO exportSuggestVO : exportSuggestVOS){
beanWriter.write(exportSuggestVO, header,processors);
}
} finally {
if (beanWriter != null){
beanWriter.close();
}
}
return new ByteArrayInputStream(outputStream.toByteArray()); }