doubeng9567 2015-05-21 09:44
浏览 45
已采纳

如何从Android中的php URL下载PDF文件?

I have a php URL, which when I run in a browser, it returns to me a PDF file. In my application I want to download this PDF file. I'm trying to do this:

HttpURLConnection urlConnection = urlConnection = (HttpURLConnection) url_download_relatorio_funcionario.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.connect();

int totalSizePDF = urlConnection.getContentLength();

But the integer value "totalSizePDF" only returns to me -1 value, instead of the real size of PDF file which is opened if I run this URL in a browser. How can I download this PDF file from the php URL, producing the same effect as a browser?

  • 写回答

1条回答 默认 最新

  • duanboniao5903 2015-05-21 09:47
    关注

    getContentLength() uses the Content-Length header which, according to Google Chrome's Dev Tools, your server isn't returning.

    Try setting the length header from your PHP script first.

    Downloading the file should be simply reading the returned bytes and saving them to file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部