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条)

报告相同问题?

悬赏问题

  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员