Когда я загружаю Excel из папки с java, я получаю эту ошибку «формат и расширение файла .xls не совпадают. Файл может быть поврежден» - PullRequest
0 голосов
/ 15 января 2020

Как гласит заголовок, когда я загружаю лист Excel из папки, я получаю это сообщение

Это мой код

RequestMapping(value = "/descargar-datos-entrada/{idSimulacion}", method = RequestMethod.GET)
    public void descargarDatosEntrada(@PathVariable("idSimulacion") String idSimulacion, HttpServletResponse response)
            throws IOException {
        Properties properties = new Properties();
        properties = Util.getProperties("mongo");
        FileInputStream inputStream = null;
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment; filename=Hoja-Resultados-Descarga.xls");

        try {

            Simulacion simulacion = simulacionService.finById(idSimulacion);
            inputStream = new FileInputStream(
                    properties.getProperty("ruta.copia.resultados.excel") + idSimulacion + ".xls");

            int c;
            while ((c = inputStream.read()) != -1) {
                response.getWriter().write(c);
            }

        } catch (SimulacionException | IOException e) {
            Util.autoLogError(e);
        } finally {
            if (inputStream != null)
                inputStream.close();
            response.getWriter().close();
        }

    }

, и файл .xls результата будет выглядеть следующим образом :

enter image description here

Буду признателен за помощь или совет. Заранее спасибо, это мой первый вопрос, так что извините, если я не задал его правильно .

1 Ответ

2 голосов
/ 15 января 2020

Двоичный вывод с getOutputStream, а не с выводом текста getWriter:

        Path path = Paths.get(properties.getProperty("ruta.copia.resultados.excel")
                    + idSimulacion + ".xls");
        Files.copy(path, response.getOutputStream());
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...