日期:2014-05-20  浏览次数:20724 次

URL 获取资源的大小
Java code
URL url1 = new URL("http://www.google.com/imgres?                  imgurl=http://a2.att.hudong.com/85/15/01300000196604122280159748057_s.jpg&imgrefurl=http://www.hudong.com/wiki/%25E7%258E%25AB%25E7%2591%25B0%25E8%258A%25B1&h=300&w=275&sz=15&tbnid=i0RGriJWfoSQOM:&tbnh=90&tbnw=83&prev=/search%3Fq%3D%25E7%258E%25AB%25E7%2591%25B0%25E8%258A%25B1%26tbm%3Disch%26tbo%3Du&zoom=1&q=%E7%8E%AB%E7%91%B0%E8%8A%B1&docid=fWy0CgEDxlwPOM&hl=en&sa=X&ei=efGET4_sMc7qmAXmp4i6Bw&ved=0CEEQ9QEwAw&dur=747");

        HttpURLConnection urlConnection = (HttpURLConnection) url1.openConnection();
        long len = urlConnection.getContentLength();
        if(len<=0){
           System.out.println( "无法获知文件大小 ");
        }else {
            System.out.println( "文件大小为 " + len/1024/1024 + "M ");
        }

原谅我,这URL好长…它指向一个玫瑰花图片,为什么无法获得这个文件的大小呢?打印总是"无法获知文件大小 "。
是URL地址不合法的原因吗?正确的应该是怎么给出的呢?

------解决方案--------------------
你是不是单词写错了?imgres?还是images?