Johnson_Cheng 2009-08-18 13:24
浏览 357
已采纳

使用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()的。

不过最好写上了,谢谢。

  • 写回答

3条回答 默认 最新

  • 墨染静然 2009-08-18 16:51
    关注

    这个问题出在:
    [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]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程