java - URLConnection getContentLength() 返回负值

标签 java android httpurlconnection

这是我的代码:

url = paths[0];
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
int length = connection.getContentLength(); // i get negetive length
InputStream is = (InputStream) url.getContent();
byte[] imageData = new byte[length]; 
int buffersize = (int) Math.ceil(length / (double) 100);
int downloaded = 0;
int read;
while (downloaded < length) {
    if (length < buffersize) {
        read = is.read(imageData, downloaded, length);
    } else if ((length - downloaded) <= buffersize) {
        read = is.read(imageData, downloaded, length - downloaded);
    } else {
        read = is.read(imageData, downloaded, buffersize);
    }
    downloaded += read;
    publishProgress((downloaded * 100) / length);
}
Bitmap bitmap = BitmapFactory.decodeByteArray(imageData, 0,
        length);
if (bitmap != null) {
    Log.i(TAG, "Bitmap created");
} else {
    Log.i(TAG, "Bitmap not created");
}
is.close();
return bitmap;

我在 Java 文档中查看了这个,长度为负数,原因如下:

"the number of bytes of the content, or a negative number if unknown. If the content >length is known but exceeds Long.MAX_VALUE, a negative number is returned."

这可能是什么原因?我正在尝试下载图像。我想指出这是我尝试下载图像的第四种方法。提到其他三个here .

编辑:

根据要求,这是我正在使用的完整方法。

protected Bitmap getImage(String imgurl) {

    try {
        URL url = new URL(imgurl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        int length = connection.getContentLength();
        InputStream is = (InputStream) url.getContent();
        byte[] imageData = new byte[length];
        int buffersize = (int) Math.ceil(length / (double) 100);
        int downloaded = 0;
        int read;
        while (downloaded < length) {
            if (length < buffersize) {
                read = is.read(imageData, downloaded, length);
            } else if ((length - downloaded) <= buffersize) {
                read = is.read(imageData, downloaded, length
                        - downloaded);
            } else {
                read = is.read(imageData, downloaded, buffersize);
            }
            downloaded += read;
        //  publishProgress((downloaded * 100) / length);
        }
        Bitmap bitmap = BitmapFactory.decodeByteArray(imageData, 0,length);
        if (bitmap != null) {
             System.out.println("Bitmap created");
        } else {
            System.out.println("Bitmap not created");
        }
        is.close();
        return bitmap;
    } catch (MalformedURLException e) {
        System.out.println(e);
    } catch (IOException e) {
        System.out.println(e);
    } catch (Exception e) {
        System.out.println(e);
    }
    return null;
}

最佳答案

默认情况下,此 HttpURLConnection 实现请求服务器使用 gzip 压缩。
由于 getContentLength() 返回传输的字节数,因此您不能使用该方法来预测可以从 getInputStream() 读取多少字节。
相反,读取该流直到它耗尽:当 read() 返回 -1 时。
可以通过在请求 header 中设置可接受的编码来禁用 Gzip 压缩:

 urlConnection.setRequestProperty("Accept-Encoding", "identity");

那么试试这个:

HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Accept-Encoding", "identity"); // <--- Add this line
int length = connection.getContentLength(); // i get negetive length

来源(表演段落):http://developer.android.com/reference/java/net/HttpURLConnection.html

关于java - URLConnection getContentLength() 返回负值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5428639/

相关文章:

Java静态错误

java - 如何在 GET 请求中获取 rest Controller 中的查询参数?

java - 生成的 javadoc 页面不必要地用注释包装方法参数

java - 将 HttpEntity 解码为 android 字符串 - 编码问题

Android 如何从 Geocoder 返回的地址中获取街道名称

android - 使用 HttpURLConnection 从 Android 发送 HTTP POST

java - 如何解决警告: This AsyncTask class should be static or leaks might occur

android - 来自条形码值的详细信息

java - PHP 接收来自 Java 的 POST 请求

java - HttpUrlConnection "PUT"请求还发送 "GET"请求