download a file from rest api is giving me some garbage value

Questions:

I am trying to download a file from rest API, I am writing code in Java and react. but when i call that rest api it is not downloading that file instead gives me some garbage

      @POST
            @Path("/{loginId}")
            @Produces(MULTIPART_FORM_DATA)
            @Consumes(APPLICATION_JSON)
            public Response downloadExportedFile(@PathParam("loginId") String loginId, ExportFileDTO fileDetails) {
        File exportFolder = new File("C://directory");

                        File[] listOfFiles = exportFolder.listFiles();
for (File listOfFile : listOfFiles) {
           if (listOfFile.getName().equals(fileDetails.getFileName())) {
                                            InputStream is = new FileInputStream(listOfFile.getAbsolutePath());
                                            byte[] buffer = IOUtils.toByteArray(is);
                                            return Response.ok(listOfFile)
                                                    .header("content-disposition", "attachment; filename=" + new File(listOfFile.getName()).getName())
                                                .type(MediaType.APPLICATION_OCTET_STREAM_TYPE).build();
                                    }
    }

It should download the file instead it is giving me output as
PK!b�h^�[Content_Types].xml �(����N�0E�H�C�-Jܲ@5��Q>�ēƪc[�ii����B�j7���{2��h�nm���ƻR����U^7/���%��rZY�@1__�f��q��R4D�AJ�h>����V�ƹ�Z�9����NV�8ʩ����ji){^��-I�”{�v^�P!XS)bR�r��K�s(�3�`c�0��������7M4�����ZƐk+�||z�(���P��6h_-[�@�!���Pk���2n�}�?�L��� ��%���d����dN”m,�ǞDO97�~��ɸ8�O�c|n���E������B��!$}�����;{���[����2���PK!�U0#�L_rels/.rels �(���MO�0��H�����ݐBKwAH�!T~�I����$ݿ’T�G�~����<���!��4��;#�w����qu*&r�Fq���v�����GJy(v��*����K��#F��D��.W ��=��Z�MY�b���BS�����7��ϛז��
?�9L�ҙ�sbgٮ|�l!��USh9i�b�r:”y_dl��D���|-N��R”4�2�G�%��Z�4�˝y�7 ë��ɂ�����PK!

Answers:



Questions:

I am trying to download a file from rest API, I am writing code in Java and react. but when i call that rest api it is not downloading that file instead gives me some garbage

      @POST
            @Path("/{loginId}")
            @Produces(MULTIPART_FORM_DATA)
            @Consumes(APPLICATION_JSON)
            public Response downloadExportedFile(@PathParam("loginId") String loginId, ExportFileDTO fileDetails) {
        File exportFolder = new File("C://directory");

                        File[] listOfFiles = exportFolder.listFiles();
for (File listOfFile : listOfFiles) {
           if (listOfFile.getName().equals(fileDetails.getFileName())) {
                                            InputStream is = new FileInputStream(listOfFile.getAbsolutePath());
                                            byte[] buffer = IOUtils.toByteArray(is);
                                            return Response.ok(listOfFile)
                                                    .header("content-disposition", "attachment; filename=" + new File(listOfFile.getName()).getName())
                                                .type(MediaType.APPLICATION_OCTET_STREAM_TYPE).build();
                                    }
    }

It should download the file instead it is giving me output as
PK!b�h^�[Content_Types].xml �(����N�0E�H�C�-Jܲ@5��Q>�ēƪc[�ii����B�j7���{2��h�nm���ƻR����U^7/���%��rZY�@1__�f��q��R4D�AJ�h>����V�ƹ�Z�9����NV�8ʩ����ji){^��-I�”{�v^�P!XS)bR�r��K�s(�3�`c�0��������7M4�����ZƐk+�||z�(���P��6h_-[�@�!���Pk���2n�}�?�L��� ��%���d����dN”m,�ǞDO97�~��ɸ8�O�c|n���E������B��!$}�����;{���[����2���PK!�U0#�L_rels/.rels �(���MO�0��H�����ݐBKwAH�!T~�I����$ݿ’T�G�~����<���!��4��;#�w����qu*&r�Fq���v�����GJy(v��*����K��#F��D��.W ��=��Z�MY�b���BS�����7��ϛז��
?�9L�ҙ�sbgٮ|�l!��USh9i�b�r:”y_dl��D���|-N��R”4�2�G�%��Z�4�˝y�7 ë��ɂ�����PK!

Answers:



Need Help? Chat with us