使用URLConnection下载图片失败

我正在使用HttpClient下载一些网上的图片,但是发现有些图片可以正确下载,有些图片则不可以。后来我尝试使用URLConnection下载,还是不能正确的下载,但是浏览器中是可以访问的。请大家帮忙看看,如下是一段测试性代码:

[code="java"]
URL server = new URL("http://203.98.189.57/images/product_images/Astrid%20Wallet_main.jpg");
HttpURLConnection connection = (HttpURLConnection)server.openConnection();
connection.setRequestMethod("GET");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.addRequestProperty("Accept","image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/x-shockwave-flash, /");
connection.addRequestProperty("Accept-Language", "en-us,zh-cn;q=0.5");
connection.addRequestProperty("Accept-Encoding", "gzip, deflate");
connection.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 2.0.50727; MS-RTC LM 8)");
connection.connect();
InputStream is = connection.getInputStream();
OutputStream os = new FileOutputStream("c:/Astrid Wallet_main.jpg");

byte[] buffer = new byte[1024];
int byteReaded = is.read(buffer);
while(byteReaded != -1)
{
    os.write(buffer,0,byteReaded);

byteReaded = is.read(buffer);
}

os.close();
[/code]
[b]问题补充:[/b]
应该就是这个问题,我晚上再尝试一下

不过os.flush()应该不需要,close之前,他会自动flush()的。

不过最好写上了,谢谢。

这个问题出在:
[quote]connection.addRequestProperty("Accept-Encoding", "gzip, deflate"); [/quote]
有些返回的图片经过了gzip压缩。而有些是没有压缩过得。
修改了下代码,你可以参考下:
[code="java"]//URL server = new URL("http://203.98.189.57/images/product_images/Astrid%20Wallet_main.jpg");
URL server = new URL("http://zi.csdn.net/studio30060.gif");
HttpURLConnection connection = (HttpURLConnection) server.openConnection();
connection.setRequestMethod("GET");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.addRequestProperty("Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/x-shockwave-flash, /");
connection.addRequestProperty("Accept-Language", "en-us,zh-cn;q=0.5");
connection.addRequestProperty("Accept-Encoding", "gzip, deflate");
connection.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 2.0.50727; MS-RTC LM 8)");
connection.connect();
InputStream is = connection.getInputStream();

        Map map = connection.getHeaderFields();
        Iterator it = map.keySet().iterator(); 

        boolean gzip = false;
        while(it.hasNext()) {
           Object type =  map.get(it.next());
           if(type.toString().indexOf("gzip") != -1){
               gzip = true;
               break;
           }
        }

        OutputStream os = new FileOutputStream("c:/Astrid Wallet_main1.gif");
        byte[] buffer = new byte[1024];

        if(!gzip){
            int byteReaded = is.read(buffer);
            while (byteReaded != -1) {
                os.write(buffer, 0, byteReaded);
                byteReaded = is.read(buffer);
            }
        }else{
            GZIPInputStream gis = new GZIPInputStream(is);
            int byteReaded = 0;          
            while ((byteReaded = gis.read(buffer)) != -1) {
               os.write(buffer, 0, byteReaded);                  
           }
        }  

        os.close();

[/code]

对了 os.write后,记得 os.flush();

[code="java"]
while (byteReaded != -1) {

os.write(buffer, 0, byteReaded);

os.flush();

byteReaded = is.read(buffer);

}
[/code]
应该是在这个循环里面flush();而不是在close之前。