无法下载具有特定ID的CSV文件

时间:2019-10-07 10:53:40

标签: java spring csv download

我使用的是Spring Boot 1.5,我想使用我在url中指定的特定ID来下放警报的csv文件(类Alert),但是它总是给我第三个具有查询参数中第一个ID的警报:例如,如果我要下载ID为32和65的警报,我刚得到一个ID为32的具有警报的csv文件,我不知道代码有什么问题,请帮忙。

服务:

@Override
public InputStream exportAlerts(Long id) throws IOException {
    List<ExportSuggestVO> exportSuggestVOS = new ArrayList<>();

    final String[] header = new String[]{"id", "alertStatus","added","addedByEmail", "addedBy",
            "uo" , "assigneeMatricule", "closed", "object", "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()).getUo())
                .setAssigneeMatricule(alert.getAssignee() == null ? " "
                        : alert.getAssignee())
                .setClosed(alert.getClosed() == null ? " "
                        : alert.getClosed().toString().replace("T", " "))
                .setObject(alert.getObject() == null ? " "
                        : alert.getObject())
                .setContent(alert.getDescription() == null ? " "
                        : alert.getDescription())

                .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", "Date de fermeture" ,"Objet" , "Contenu");

        for (ExportSuggestVO exportSuggestVO : exportSuggestVOS){
            beanWriter.write(exportSuggestVO, header,processors);
        }
    } finally {
        if (beanWriter != null){
            beanWriter.close();
        }
    }
    return new ByteArrayInputStream(outputStream.toByteArray()); }

控制器:

 @RequestMapping(value = "/massiveDownload{ids}", method = RequestMethod.GET)
public void massiveExport(@RequestParam List<Long> ids, HttpServletResponse response)
        throws IOException
{
    for (Long id : ids) {
        InputStream in = alertService.exportAlerts(id);
        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();
        }
    }}

ps:调试时,控制器会识别所有ID。

0 个答案:

没有答案