gpt4 book ai didi

java - URLConnection getContentLength() 返回负值

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:16:16 25 4
gpt4 key购买 nike

这是我的代码:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com