无法将PDF文件作为二进制数据获取

时间:2011-03-10 06:13:46

标签: java android http-headers httprequest httpurlconnection

我正在尝试从以下位置获取PDF文件:

网址:https://domain_name/xyz/_id/download/

其中它不指向直接pdf文件,并且每个唯一文件都被下载 解释特定的< _id>字段。

我将此链接放在浏览器的地址栏中,并立即下载Pdf文件, 当我尝试通过HTTPsURLConnection获取它时,它的Content-Type是'text / html'形式, 虽然它应该在'application / pdf'。

我还尝试在连接之前'setRequestProperty'为'application / pdf',但文件总是以'text / html'形式下载。

我正在使用的方法是'GET'

1)我是否需要使用HttpClient而不是HttpsURLConnection?

2)这些类型的链接是否用于提高安全性?

3)请指出我的错误。

4)如何知道服务器上的文件名?

我正在粘贴我已实施的主要代码:

    URL url = new URL(sb.toString());

    //created new connection
    HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();

    //have set the request method and property
    urlConnection.setRequestMethod("GET");
    urlConnection.setDoOutput(true);
    urlConnection.setRequestProperty("Content-Type", "application/pdf");

    Log.e("Content Type--->", urlConnection.getContentType()+"   "+ urlConnection.getResponseCode()+"  "+ urlConnection.getResponseMessage()+"              "+urlConnection.getHeaderField("Content-Type"));

    //and connecting!
    urlConnection.connect();

    //setting the path where we want to save the file
    //in this case, going to save it on the root directory of the
    //sd card.
    File SDCardRoot = Environment.getExternalStorageDirectory();

    //created a new file, specifying the path, and the filename

    File file = new File(SDCardRoot,"example.pdf");

    if((Environment.getExternalStorageState()).equals(Environment.MEDIA_MOUNTED_READ_ONLY))

    //writing the downloaded data into the file we created
    FileOutputStream fileOutput = new FileOutputStream(file);

    //this will be used in reading the data from the internet
    InputStream inputStream = urlConnection.getInputStream();

    //this is the total size of the file
    int totalSize = urlConnection.getContentLength();

    //variable to store total downloaded bytes
    Log.e("Total File Size ---->", ""+totalSize);
    int downloadedSize = 0;

    //create a buffer...
    byte[] buffer = new byte[1024];
    int bufferLength = 0; //used to store a temporary size of the buffer

    //Reading through the input buffer and write the contents to the file
    while ( (bufferLength = inputStream.read(buffer)) > 0 ) {

        //add the data in the buffer to the file in the file output stream (the file on the sd card
        fileOutput.write(buffer, 0, bufferLength);


        //adding up the size
        downloadedSize += bufferLength;

        //reporting the progress:
        Log.e("This much downloaded---->",""+ downloadedSize);

    }
    //closed the output stream
    fileOutput.close();

我搜索了很多但无法得到结果。 如果可能的话,请在我实施时尽量详细说明我的错误 这是第一次。

**尝试获取直接的pdf链接,例如:http://labs.google.com/papers/bigtable-osdi06.pdf   并且他们很容易下载,而且他们的'Content-Type'也是'application / pdf'**

感谢。

2 个答案:

答案 0 :(得分:1)

理论1:服务器响应中的内容类型不正确。如果您编写并部署了服务器代码,请检查。

理论2:网址返回一个html页面,其中包含一些javascript,将页面重定向到实际pdf文件的URL。

答案 1 :(得分:1)

这个帖子引导我解决我的问题! 当您尝试从WebView下载流式PDF并使用HttpURLConnection时,您还需要从Webview中传递cookie。

String cookie = CookieManager.getInstance().getCookie(url.toString());
if (cookie != null) connection.setRequestProperty("cookie", cookie);