dpw70180
2016-07-18 14:24
浏览 3.4k

如何使用Retrofit 2下载pdf文件

I'm having difficulty downloading a pdf file with retrofit. My code produces a file, but it is the wrong size and when the pdf opens it is blank.

This is how my php webservice returns a pdf file.

**
 * @param Slim\Slim $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);
}

If I navigate to the api endpoint via browser, a pdf file downloads immediatley.

This is the code I am using to download the pdf file in Android.

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;
    }
}

I've been logging the http responses. Here is the response that I get with the 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&lt;�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&gt;���Q��J^�;�~THR�ړ�������&gt;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{�&amp;�D@��*
D: ║ }��=P
D: ║ �#7�js5Ҥ!He|揂���H��R��&lt;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&lt;���"�t�L
D: ║ 9��"]mWY�V��&gt;�R�.�&amp;Ⱥ\Of�|�N�ҋ
D: ║ }���S��f�r��
D: ║ �i�tz�&gt;G='&lt;�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��]���&gt;���^+)k�&lt;,4|O�o]R����1�n�:��&gt;����6��R��~�/1-%ٟ�E�C]���m���Cۧ���I�{��UA��k�}�s��&amp;�Ƥ�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�&amp;�:�g���\������V.�
D: ║ e���G5�%���2��Ip%2��ۭ�WU㟄y����e�$gb��WP�A�+l�b0���J��X�a;������&lt;D�l�(�Q��j��7vѺO4�s�c+��ְu����|6�Z���C��5�4�\ZJ�����B�����#KB5u��3�㵝_{��'x|Ք+n���0e�x!g���9���v�}��Z}�5_/X��O!|���jڧ��� �ыǥt&amp;���ki���KXH�M
D: ║ endobj
D: ║ 21 0 obj
D: ║ &lt;&lt;
D: ║ /Length 507
D: ║ /Filter [ /FlateDecode ]
D: ║ &gt;&gt;
D: ║ stream
D: ║ A-�1l���N�Zx�4f��
D: ║ ��ŕGn)�S����G�g���l��R�ҹ���=�?�{]V4�����Vendstream
D: ║ endobj
D: ║ 24 0 obj
D: ║ &lt;&lt;
D: ║ /Length 16310
D: ║ /Length1 22572
D: ║ /Filter [ /FlateDecode ]
D: ║ &gt;&gt;
D: ║ stream
D: ║ ���������]u�r}�a&gt; ǁ�
D: ║ 54����q��DY"v�2���|t-�/@JN@��n���]R��ME�����OD;�J�J,B��q�w��
D: ║ �&amp;��f؇������lԃ��v���
?\�s[�}�A�E�~0p�n�k�迀4��л�����q������7�[H�9�����=&gt;"5����]=��O���=qz���F���S|��w�
D: ║ �
D: ║ r�ڔ�e�6{ѢcL�<��N{%h������N^ ��%9d#(c\LF��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: ║ �&amp;-@�-@�"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�����(&lt;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��je�^���b�8���P2u�n����a����:g,/hZ:����X�/�\L&amp;�Qf��H�+&lt;)sv&amp;i
D: ║ �A�(mb���fl��Y���iֹ�&lt;�
D: ║ *�p�V�o�g̹�s.Ҕ���%#�W�����
D: ║ 5��p�����C�*�2�
D: ║ #�&lt;�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&gt;��K#σ�
+O]�c]'b0��m1c98�m�$t��&gt;C&gt;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����/}��O��1�[�k�M_�65хBM�Ua�G��p^
D: ║ endobj
D: ║ 26 0 obj
D: ║ <<
D: ║ /Length 618
D: ║ /Filter [ /FlateDecode ]
D: ║ >>
D: ║ stream
D: ║ endobj
D: ║ 29 0 obj
D: ║ <<
D: ║ /Length 25243
D: ║ /Length1 40156
D: ║ /Filter [ /FlateDecode ]
D: ║ >>
D: ║ stream
D: ║ �C��h�-+7=��ɟ��l
�<�E?&ģ�!�#r��x��� �3�g��뿺m���m�7f2AY^AQ-���_��ڿ�-��GH��欟����eD���B9ē�44��6V�A�!�=$AM���2�Q �KDe�%�U�������4
D: ║ ��"h+�rQ�F��!;��8�G!�O1E���V/�lȍ��X�.�v��B4���Ր�t1��ė�.H[�kv��P���� ��#Ta�O�p+�By�΀�%HBzR�P��h9��p���N�6��?D%І��0�V�=�
D: ║ �Խ݆�"7�f>E����Y�k'�v.�jڈ�ų2ף&��D��uh��4d��<�y}�~�������=��L�M�w@{M���Q�~P��j�tG��-�1��Ǚՙ�0�(��#��>A���J�'��۹)��3�t�$�
D: ║ �f�=���
D: ║ ���h��8�9�=ÿ�%���XE�;��G�O�����!�B���?r��_�)��S_�6�{���؎�����oǻ��Q|
D: ║ ����ʡ2����
hf1Ql D:�<#i��3�����s�x��Fw�W
D: ║ �M맴t�h>�?-��X���0 v�#sZ�1.�Q����A����ta!Ei
D: ║ �)�q��]����b��$0}h�mk�b��h�.�=})�.����Z�F��dc��ВZ�k1-��J�no�&+*�+�K�Ye��n̈́4v��+��c3�/i��u��s;sш+�|�P�
D: ║ �I!��Jy�n��a��tZF��'+%XG)P,���JIJ��C�N��Pj��uP"Ӓ�$b�ʧ.5��� ���I��Ьn�9
D: ║ � ^fs�+�s�}\AO�>�
D: ║ 7
D: ║ �~"9Nr�W����1�A�n�)K�CO�P�-�JL��(1�
D: ║ � z2�d��=
D: ║ ���!d �P;~?��C�����>� ��|�������A���'�WzG�$�ʑ�rR���?��!<7� �J�v˶�t�>l>��?�aF�Z=���OY7٫��w �?ҝ8�u�Q���U(�㐎G��u4-GA�2����z��ڝ(
D: ║ �zס����O�}�O��¿���;�+�y�P������� �D��h�U=����
D: ║ ��F�C�[���났�Rpm;\����%�+����e�T;�y(\�6\�Ԫ��
D: ║ ���
D: ║ �*A�gUꇫ�U�
D: ║ �r7���u�J�9����I��~VNN&amp;q����M����[ �%}϶5^��G��7���D˲�kh
D: ║ B)36��B( 䆜fC��ܗ���щ�C�N�G���1�&lt;O1Lͩ�䩷O�?F5j.$4_�O6�- �F���R�5�75�ľ(��[!�е�WD�����|G��&amp;E���e�@��&gt;
D: ║ EY�&lt;3���٥Ț�^$L��nt0^+g.RZ�)T��/�U�Ȍ̀x��x���� ����⌼������44i��=�� �!
��,�l����=�Zf�a����My�t̞>�ד��=^��/@t�1
D: ║ �,���S0hGE���p�ԩS\�S_?
D: ║ (&lt;VoC&amp;&amp;��L�1�T8GyEm��[��KE ��3&amp;��T�3Eڤ"U�H�n35�w����֟Y�d�:�,�U�)�E�&amp;W�oA
��θܱ޵����� n3�l�%>"=$���yG|��[�hH#�Փ���,~�i�3~��H/뉾���7i�O�����{�6�5�4�lS�z[;�L�RQw�V�Ӕ��z�74Ĕ�'��ʔթ�͛Q���b�'ALt����������k��x�ev2fl6�~�߼��Gd;Ed��%�� �LٮMf�y��uo��v�oz�ȋ۷���mۯj&g1
D: ║ ����Ҟ��{������0���ß��5�?wC^���P��r^���B��_8�\XXe�L(�Q�ln.l3�-l)�4�5�1�w�/�]
D: ║ �c��Y��Tp�w���t�/]�覺q�2NE��Ƃ����ΥP��&�
D: ║ pȁu�S�K�S��KՉ*��
D: ║ �����e�2�2=Lմ�l� ����v=U�\۶�÷���K=��I���۰b֎ |��s�.k8z��@>�������ä���=��;��[�r���ä����Gt��3#jϔ��M�v�#T���t��v�_�\�̗����v�|�>� ���P�z��s�K�oA=T��U)�:��@��ā�̕u��<}���F]"&g�W�r�^�9�z������_޿�ۃ�o�~����q�5�t٨��r��84�b����I�=�8L*�8B�VQ�� L�E9�B.W�N'�h��P�l�H�R�O ������ŧ�5��I�d�j��v�XY<�SNg�C�����1�6��;��B?�0������pZ�f0�R�uʲ�B,kʅ�a���,=�X�Fd[*o�o��y�C��JY�W��j�v/�j�&amp;�^�R�ϻ7bW +�6��m9����c�H�t׽au��8K^�x�b�נ�1�Lq��lk�9�K7&amp;)������^}�Pb&lt;
D: ║ j*���mrg!4;,@ʼ��&lt;
D: ║ Ш����i
D: ║ ~�&lt;��J^"d�KΠl�Y8w0Hw�bD_m�V��Zy�'�����lHZ&gt;���A$E��З�X6��k^S��1�Ǡ�$To�R�4lŎ2Rf���eHh(z���G��{�&gt;����&lt;s@�_+�
D: ║ f�
D: ║ f�
D: ║
a4D!'\F-���ʍ�M�Q���.���$@a9�:>��1�Garǎ(K����y8+��q�1���3�e����r�J�v��8j�e�u��1�Sy�\���qRq� e9Q�m���7VĿ��sk�>���.��M�k����Ł;�?\���[O����۲r�1o�tƎ9�o*'��e�wAӂ�X0�a�+��޴�ɫ���J�~u�R~�
D: ║ v���e� �£ȃ�c{ˑ{��,X�KY����%�e�����d�GnY��D7p���r��?8���[o��pF����Z�MR��W���}RZ:!��D���bJ��_�c��ǪLcSV^F�- ���$E�JGI��q]��-o��������E��B�C��g++���U��.��Ǖ�]�-�Ҕ~=6��6�AϜ��ԓe���*�{�W�be�JXĘ��$�V��Ew��s�#Yz�Iy�ʧ��=XZ?xo�.�#N=uFf4�kQ!�ƓRF���!̗�W<-�8:��y\���f�h{�-�vE;��'���*��ѓ:E�Xy�n|��Tq�D��e�[E��hI��J��TYb�V�R��<{h���8x�<�d�'|�Ɗ���rwx<N���nl(���Qg�9H����;�m�*I� .�G���n�'�DƏ/-5
D: ║ �9�X��u͊�&��_|A�R��y�Q�sB�$�q</�D!�OQ�{��/��h��b�92��y�o�{���p:e�8�ӎ�]��
D: ║ ��:l|��(����ϰ1�ac�6�-��{�E��o��Fw�޶u�<�΀E݌��I��,��`b�q�
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douzhan2027 2016-07-20 18:14
    已采纳

    I was using an old version of Retrofit and Okhttp. Once I updated to a newer version it worked.

    点赞 评论
  • duannv2081 2016-07-20 16:17

    You can see the following link:

    Set appropriate headers and don't forget annotation @Streaming. It works for me. I tested it with different types of files and it worked.

    Retrofit2 - How to Download Files From Server

    点赞 评论

相关推荐 更多相似问题