doubeng9567 2015-05-21 17: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 17: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.

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

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序