沃梦达 / 编程问答 / php问题 / 正文

如何使用Retrofit 2下载pdf文件

How to download a pdf file with Retrofit 2(如何使用Retrofit 2下载pdf文件)

本文介绍了如何使用Retrofit 2下载pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下载经过改造的pdf文件时遇到困难。我的代码生成了一个文件,但它的大小错误,当pdf打开时它是空白的。

这是我的php webservice返回pdf文件的方式。

**
 * @param SlimSlim $app
 * @param String $pdf
 */
function respondWithPDF($app, $pdf) {
    $res = $app->response();
    $res['Content-Description'] = 'File Transfer';
    $res['Content-Type'] = 'application/octet-stream';
    $res['Content-Disposition'] ='attachment; filename=' . basename($pdf);
    $res['Content-Transfer-Encoding'] = 'binary';
    $res['Expires'] = '0';
    $res['Cache-Control'] = 'must-revalidate';
    $res['Pragma'] = 'public';
    $res['Content-Length'] = filesize($pdf);
    readfile($pdf);
}

如果我通过浏览器导航到API终结点,则会立即下载一个pdf文件。

这是我在Android中用于下载pdf文件的代码。

private boolean writeResponseBodyToDisk(ResponseBody body, String fileName) {

    try {

        File futureStudioIconFile = new File(mContext.getExternalCacheDir() + File.separator + fileName);

        InputStream inputStream = null;
        OutputStream outputStream = null;

        try {
            byte[] fileReader = new byte[4096];

            long fileSize = body.contentLength();
            long fileSizeDownloaded = 0;

            inputStream = body.byteStream();
            outputStream = new FileOutputStream(futureStudioIconFile);

            while (true) {
                int read = inputStream.read(fileReader);

                if (read == -1) {
                    break;
                }

                outputStream.write(fileReader, 0, read);

                fileSizeDownloaded += read;

                Log.d(Constants.LOG_TAG, "file download: " + fileSizeDownloaded + " of " + fileSize);
            }

            outputStream.flush();

            return true;
        } catch (IOException e) {
            return false;
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }

            if (outputStream != null) {
                outputStream.close();
            }
        }
    } catch (IOException e) {
        return false;
    }
}

我一直在记录http响应。这是我从pdf中得到的答复。

D: ╔════════════════════════════════════════════════════════════════════════════════════════
D: ║ Thread: OkHttp https://192.168.1.15/mamobile/index.php/report/activity?startDate=3916-08-15%2000:00:00.000&endDate=3916-02-01%2000:00:00.000
D: ╟────────────────────────────────────────────────────────────────────────────────────────
D: ║ Call$ApplicationInterceptorChain.proceed  (Call.java:232)
D: ║    ApiClient$LoggingInterceptor.intercept  (ApiClient.java:247)
D: ╟────────────────────────────────────────────────────────────────────────────────────────
D: ║ response
D: ║ Received response for https://192.168.1.15/mamobile/index.php/report/activity?startDate=3916-08-15%2000:00:00.000&endDate=3916-02-01%2000:00:00.000 in 5190.8ms
D: ║ Cache-Control: must-revalidate
D: ║ Pragma: public
D: ║ Content-Length: 60942
D: ║ Content-Type: application/octet-stream
D: ║ Expires: 0
D: ║ Server: Microsoft-IIS/10.0
D: ║ X-Powered-By: PHP/5.4.24
D: ║ Content-Description: File Transfer
D: ║ Content-Disposition: attachment; filename=Activity Report.pdf
D: ║ Content-Transfer-Encoding: binary
D: ║ X-Powered-By: ASP.NET
D: ║ Date: Wed, 20 Jul 2016 16:52:17 GMT
D: ║ OkHttp-Sent-Millis: 1469033539269
D: ║ OkHttp-Received-Millis: 1469033544169
D: ║ 
D: ║ %PDF-1.3 
D: ║ %���� 
D: ║ 1 0 obj 
D: ║ << 
D: ║ /Type /Catalog 
D: ║ /Pages 2 0 R 
D: ║ /PageMode /UseNone 
D: ║ /ViewerPreferences << 
D: ║ /FitWindow true 
D: ║ /PageLayout /SinglePage 
D: ║ /NonFullScreenPageMode /UseNone 
D: ║ >> 
D: ║ >> 
D: ║ endobj 
D: ║ 5 0 obj 
D: ║ << 
D: ║ /Length 1394 
D: ║ /Filter [ /FlateDecode ] 
D: ║ >> 
D: ║ stream
D: ║ v�X����:`��)��05~�z��gղZ?U+�IQ�A����+@�l!��l1�9��~V����X�]���I�0��=�ěg���o'@���)[P�2l��z��jwL�}����Bk<�W���J�K"Hp*K;�E@����óL����� �`U
D: ║ ���XM�����d~���eO�40q��?��Y;N�K�����������⭒htgi��y�~{?��z;z���G?ٖKP�8�!��
D: ║ endobj 
D: ║ 16 0 obj 
D: ║ << 
D: ║ /Length 308 
D: ║ /Filter [ /FlateDecode ] 
D: ║ >> 
D: ║ stream
D: ║ x�u��j�0��~
D: ║ ndstream 
D: ║ endobj 
D: ║ 19 0 obj 
D: ║ << 
D: ║ /Length 8454 
D: ║ /Length1 11968 
D: ║ /Filter [ /FlateDecode ] 
D: ║ >> 
D: ║ stream
D: ║ 1%��D?A=
D: ║ �Љq���
D: ║ "rvG<���6��h����m�ŏ�H�pe���0�X�����06�5Ɨ@3��ߡī��C�z���4�a*�;~Ѩ�����)΃APca<4��5<���dI9d�'�|�O��s�OA�֋ٱ
D: ║ s�(�����zx^@@?N�Mb��[�&�0�d��V�z�օ���3�Q�s��JxJ|/�I�
D: ║ �"�L�,��8��bl�{q>��K�fg�,�Mb��v';���e�2�+�
D: ║ �B�0K�NX.�)�;A/>)nw���R��]��ټ���V����]�������5�5d�G�&O�w��A8BVy�����q���#zЋ>z3p(��D�o�[h�G�q܅��_���(��o�{�    ���_a?�,��Y��������N�����6�W�O�`Gٻ�s��U>���Q��J^�;�~THR�ړ�������>AT�d1K*։w��C�]�Oʑ��n�n��I�� {�R�V�.���;�G  *[��4�L��6�˰�_���Љ�/�V6^�����CR*�f|
D: ║ ����B'����2�G�J��V�el!�
D: ║ ��~cc����Q��b�1�V1��+`<;,�A<4{-�bW���U�Mx`{�&�D@��*
D: ║ }��=P
D: ║ �#7�js5Ҥ!He|揂���H��R��<Z�EQ[uS�z��7�G�l5�u|��~�]�i�H��`��s�w0�?GG��h^��r5-$�8�,�������!2�7P�ȥ�d�@�Xջ{u��
D: ║ ���g������>o���y�
D: ║ ��RѰ�Q(D"�C����Q�"%
D: ║ ?�D��c���I$���ω��$��̯�]4�
D: ║ �����Н��^�e��δ��2��[�Kp��NwpG��
D: ║ �R
D: ║ X����6�_��NA{����o[v�t�v;g�����I~G�c���ա9�����zg���ɝl��wrv�#7�z�Ij��h'��7 ����s�݌骁��L�QpG3X��I�Y~�W�3#���<��M--�6��c�^痔�]�`���ot�'�x���M�'�y�c���I��ܡ���+�ߣ{���߉����)v����^_��Ȏ�c��K|Nz���_�m|��{�?+����Ǚ����M�����W�|��*y-#J��Z�ֶ�#�
D: ║ u�3�d� �%|�Rf�j�HFȱ�0�¶6�S�(Ё)h[�������
D: ║ +=v����=$�T-�8<���"�t�L
D: ║ 9��"]mWY�V��>�R�.�&ȺOf�|�N�ҋ
D: ║ }���S��f�r��
D: ║ �i�tz�>G='<�F�G�4yPQ��a_A
D: ║ ;��4�#mgO7�+�g8�`̵2�܈.�}a�f*�Z�h�
D: ║ 2�"�X��x���"+@�c9��l{׼��E�Kmm3��
D: ║ df�u�<�H��l0���f܄�t=�,��G�J��������M�H�7՛��9��`p$��%�Dp]�-,bw?�wىO/-��:�/�6zL�o�ǖY��wĞ���޶����{y5��]���>���^+)k�<,4|O�o]R����1�n�:��>����6��R��~�/1-%ٟ�E�C]���m���Cۧ���I�{��UA��k�}�s��&�Ƥ�w���=n���'�W�P�W��?p|!�� �S��sf�i�7��.���_����k�o��4�z��:ݠT�8�G�QJ�%˶#���ٹ��S��+8IAPTES�*�#G(J�B    e��H��������}��uu�nխ��݂�T�hnҾ���F8����UH���R-��p0��݃��v2�q8|���X�L�f��*S�&�:�g���������V.�
D: ║ e���G5�%���2��Ip%2��ۭ�WU㟄y����e�$gb��WP�A�+l�b0���J��X�a;������<D�l�(�Q��j��7vѺO4�s�c+��ְu����|6�Z���C��5�4�J�����B�����#KB5u��3�㵝_{��'x|Ք+n���0e�x!g���9���v�}��Z}�5_/X��O!|���jڧ��� �ыǥt&���ki���KXH�M
D: ║ endobj 
D: ║ 21 0 obj 
D: ║ << 
D: ║ /Length 507 
D: ║ /Filter [ /FlateDecode ] 
D: ║ >> 
D: ║ stream
D: ║ A-�1l���N�Zx�4f��
D: ║ ��ŕGn)�S����G�g���l��R�ҹ���=�?�{]V4�����Vendstream 
D: ║ endobj 
D: ║ 24 0 obj 
D: ║ << 
D: ║ /Length 16310 
D: ║ /Length1 22572 
D: ║ /Filter [ /FlateDecode ] 
D: ║ >> 
D: ║ stream
D: ║ ���������]u�r}�a> ǁ�
D: ║ 54����q��DY"v�2���|t-�/@JN@��n���]R��ME�����OD;�J�J,B��q�w��
D: ║ �&��f؇������lԃ��v���`?�s[�}�A�E�~0p�n�k�迀4�`�л�����q������7�[H�9�����=>"5����]=��O���=qz���`F���S|��w�
D: ║ �
D: ║ r�ڔ�e�6{ѢcL�<��N{%h������N^ ��%9d#(cLF��V�!Mٍ0y���L"��rS/VƿM�wR��I��!�����GqoU:��N>E;!F�1�
D: ║ *��I�t�J�g!=p7�b�Gc�7�]�����1��N���"�TĖ���EUY�e�J�&���8�K�����l�v����`
D: ║ �&-@�-@�"S���EP���E�L�"��
D: ║ @)���(���Y�h�f�h�f����(�e��h����h�)��(��A�h����h�)$���B
D: ║ I���B
D: ║    ($�B
D: ║    ($��(
D: ║ ��(
D: ║ e�B�(�B�(�)
D: ║ ��(
D: ║ e
D: ║ Px���������G�����(<2�"P�@!�"P�
D: ║ Q���Q��E(�2E(�@�):��(:��C������������C�V��J��.���KC�č
D: ║ 8kIΑ�ZtA�k�Y߁��v�[Ʒ��d���x
D: ║ �ړ�2�VػL_eP0`�N��j$���)�����D�z�^�)�N
D: ║ ��y�/����}ۍY,����/���_�� y�X@����I�>��8T�rǪT�>�R8J�!��M�_C[8��Q� �}�]
D: ║ }�Va�0B&���!��¨:�F�R(��UR���R��#2��"��!+�E�F�Sԧ%XA�51u�n�홅�fz��O�wax{���Pݤ�!��.!1!Z��
D: ║ *Ac��X�#[�-jc���[fcl��Y��,�t��@U�P��   �R��    ،�-����f���f�j>m�3s�,�=�B3kv8Wv$�r�h(�a�D��9�C��rrU�K�s���U���b=<t���o0[�3e6��
D: ║ /ʼ�I2k]Tg���k�DW��O�r��b$A��]�&{�tC�WJ!�;�^`�!V�hUyT�C���X�P#�.�dVg(r��ڈ[�SC���`����5��07��W�1.��N���l��6ݶ��Q��j`e�^���b�8���P2u�n����a����:g,/hZ:����X�/�L&�Qf��H�+<)sv&i
D: ║ �A�(mb���fl��Y���iֹ�<�
D: ║ *�p�V�o�g̹�s.Ҕ���%#�W�����
D: ║ 5��p�����C�*�2�
D: ║ #�<�n宻][]g
D: ║ ���$�G��;}9I�Ob{��%�N��D�t��^��j����f���o=��΂���2vf�z�B���
D: ║ �`����'=��W-��M���5��X����Em�S���6��+Kk�6����7�4G���a
D: ║ ��/An��K)Ĺ�G�"l��S�'��b5��jЙ�Hԙ����
D: ║ ���`�d��
D: ║ ��������6��Q2�F8�:��ˤ�i�5>��K#σ�`+O]�c]'b0��m1c98�m�$`t��>C>b?��d��ێ��G�G��
D: ║ ��|�b��6+V`A�|
D: ║ �ѵ����1�x02�ua�xaL�)�y���y�%3��kA_��Y��mc?c{Y���jI��ic>cz��/��հPG�����U��Hӥ&X�Pe����

本文标题为:如何使用Retrofit 2下载pdf文件

基础教程推荐