httpurlConnection.getContentLength() return -1

httpurlConnection.getContentLength() return -1

我想从网上下载一张图片在这个IP: http://redsonic.skyf.ir/testdownload.jpg

但是我的 httpurlConnection.getContentLength() returns -1。

这是我的代码:

URL url = new URL(downloadPath);
HttpURLConnection httpConnection =(HttpURLConnection) url.openConnection();
httpConnection.setRequestMethod("GET");
httpConnection.setDoOutput(true);
httpConnection.connect();

File file = new File(filePath);
OutputStream outputStream = new FileOutputStream(file);

InputStream inputStream = httpConnection.getInputStream();
byte[] buffer = new byte[8*1024];
int len=0;
int downloadsize = 0;
int filesize = httpConnection.getContentLength();
while ((len=inputStream.read(buffer))>0)
{
    outputStream.write(buffer,0,len);
    downloadsize += len;
    float persent = (float) downloadsize/filesize;
    Log.i("***","downloadsize="+downloadsize + "   filesize="+filesize+"    percent = "+persent);
}
//outputStream.flush();
outputStream.close();

但是当我从互联网上下载其他图片时它有效。

怎么可能?

没有错。我试过我的电脑。 httpurlConnection.getContentLength() returns 847.如果请求过多,可能已经超过了最大连接数。

只需删除行:httpConnection.setDoOutput(true); set false

Thanks EJP