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?

图片转代码服务由CSDN问答提供 功能建议

我有一个php URL,当我在浏览器中运行时,它返回给我一个PDF文件。 在我的应用程序中,我想下载此PDF文件。 我正在尝试这样做:

  HttpURLConnection urlConnection = urlConnection =(HttpURLConnection)url_download_relatorio_funcionario.openConnection(); 
urlConnection.setRequestMethod(“GET”); 
urlConnection.setDoOutput  (true); 
urlConnection.connect(); 
 
int totalSizePDF = urlConnection.getContentLength(); 
   
 
 

但整数值“totalSizePDF”仅返回 me -1值,而不是我在浏览器中运行此URL时打开的PDF文件的实际大小。 如何从php URL下载此PDF文件,产生与浏览器相同的效果?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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.

    打赏 评论

相关推荐 更多相似问题