how to get the size of an image in java

后端 未结 4 1479
谎友^
谎友^ 2021-02-03 11:14

hi I am using Jtidy parser in java.


URL url = new URL(\"http://l1.yimg.com/t/frontpage/baba-ramdev-310511-60.jpg\");  
Image image = new ImageIcon(url).g         


        
4条回答
  •  -上瘾入骨i
    2021-02-03 11:47

    Use a HEAD request to get the content length

    URL url = new URL("http://l1.yimg.com/t/frontpage/baba-ramdev-310511-60.jpg");
    //Set the user agent
    System.setProperty("http.agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0");
    //Use Http to get the head request
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    //head request to minimal response
    urlConnection.setRequestMethod("HEAD");
    length = (urlConnection).getContentLengthLong();
    

提交回复
热议问题