0

I have some error of open file.xlsx, genereted by librart Apache POI. File is save fine and open fine on my local machine. But after sending stream from java to vue it doesn't work. This is java:

public void sendFileToResponse(HttpServletResponse response, SearchParam searchParam) throws `IOException {`

        String type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        String fileNameAttr = "filename=MyExcel.xlsx";
        response.setHeader("Content-Disposition", "attachment;" + fileNameAttr);
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setCharacterEncoding("utf-8");
        response.setContentType(type + ";charset=UTF-8");

        Workbook workbook;
        FileOutputStream fileOutputStream = null;
        ServletOutputStream outputStream = null;
        try {
            workbook = getFile(searchParam);
            fileOutputStream = new FileOutputStream("MyExcel2.xlsx");
            workbook.write(fileOutputStream);

            outputStream = response.getOutputStream();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            workbook.write(baos);
            byte[] xlsx = baos.toByteArray();
            outputStream.write(xlsx);

            workbook.close();
            fileOutputStream.flush();
            fileOutputStream.close();
        } catch (Exception e) {
            logger.error("Error: {}", e);
        }

here i get it on frontend:

saveStatistic () {
      client.get('/statistics/statisticUseOfData/download', {responseType: 'blob'})
        .then(response => {
          console.log(response)
          this.downloadFileName = 'test.xlsx'

          var aHref = document.createElement('a')
          var binaryData = [];
          binaryData.push(response.data);
          var urlBlob = window.URL.createObjectURL(new Blob(binaryData, {type: "application/octet-stream"}))
          aHref.href = urlBlob
          aHref.download = this.downloadFileName
          document.body.appendChild(aHref)
          aHref.click()
          window.URL.revokeObjectURL(urlBlob)
        })
       .catch(error => {
          console.log(error)
        })

and that part of response, which i have in console.log(response.data):

PKw1Q[Content_Types].xml�S�n�0����*6�PU�C���\{�X�%����]8�R�
q�cfgfW�d�q�ZCB|��|�*�*h㻆},^�{Va�^K<4�6�N�XQ�dž�9�!P��$��҆�d�c�D�j);��ѝP�g��E�M'O�ʕ����H7L�h���R���G��^�'�{��zސʮB��3�˙��h.�h�W�жF�j娄CQՠ똈���}ιL�U:D�����%އ����,�B���[�   �� ;˱�  �{N��~��X�p�ykOL��kN�V��ܿBZ~����q�� �ar��{O�PKz��q;PKw1Q_rels/.rels���j�0�_���8�`�Q��2�m��4[ILb��ږ���.[K
�($}��v?�I�Q.���uӂ�h���x>=��@��p�H"�~�}�    �n����*"�H�׺؁�����8�Z�^'�#��7m{��O�3���G�u�ܓ�'��y|a�����D�  ��l_EYȾ����vql3�ML�eh���*���\3�Y0���oJ׏�    :��^��}PK��z��IPKw1QdocProps/app.xmlM��
Montego
  • 3
  • 1
  • 4
  • Close the `FileOutputStream` and the `Workbook` in your `try`, maybe even `flush()` the output stream before closing it. – deHaar Sep 21 '20 at 11:27
  • @deHaar , i added close and flush in try and delete my finaly. The same condition - local machine save it normaly, but browser response show me symbols in response.data – Montego Sep 21 '20 at 11:46
  • Sorry then, no more ideas... – deHaar Sep 21 '20 at 11:56
  • @deHaar, okay:)Thank you in any case – Montego Sep 21 '20 at 12:00
  • You're welcome... maybe [this question](https://stackoverflow.com/questions/45586113/corrupt-xlsx-file-after-downloading) is of use for you. – deHaar Sep 21 '20 at 12:01

2 Answers2

0

You don't need to write the workbook into a file and then into memory, you can write directly into the response:

    try {
        workbook = getFile(searchParam);
        workbook.write(response.getOutputStream());
    } catch (Exception e) {
        logger.error("Error: {}", e);
    }
Guillaume
  • 14,306
  • 3
  • 43
  • 40
  • Yeah, i just tryed to find, where is a problem. Right now i found, that workbook.write(baos); already have info like this "PKw1Q[Content_Types].xml�S�n�0����*..." – Montego Sep 21 '20 at 12:49
  • Are you trying to print the workbook in the console? Xlsx files are essentially zip files, they look like gibberish when looking at the raw content. – Guillaume Sep 21 '20 at 13:30
  • realy? thanks for new info:) so it means problem somewhere else. The problem is - i can't open file after downloading it from fronted. I have a message "file damaged and cannot be opened" and then window with such staff : "PKw1Q[Content_Types].xml�S�n..." – Montego Sep 21 '20 at 13:54
  • Try to download the file by directly typing the url in a browser, so you can isolate if the problem is in your backend or your frontend – Guillaume Sep 21 '20 at 14:44
  • Thank you:)well, it's work ,if i try to download the file by directly and it means that problem somewhere in response – Montego Sep 22 '20 at 09:13
0

I found the problem. It was in one custom layer before axios, which override http requests. And someone didn't add any params for this get, just url Thank's you

Montego
  • 3
  • 1
  • 4