且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

获取Java中的HTTP响应大小

更新时间:2022-06-27 05:54:59

我使用 commons-io CountingInputStream ,它可以帮到你。一个完整而简单的例子:

I'd use a commons-io CountingInputStream, which would do the job for you. A full but trivial example:

public long countContent(URL feedurl) {
  CountingInputStream counter = null;
  try {
     HttpURLConnection con = (HttpURLConnection) feedurl.openConnection();
     counter = new CountingInputStream(con.getInputStream());
     String output = IOUtils.toString(counter);
     return counter.getByteCount();
  } catch (IOException ex) {
     throw new RuntimeException(ex);
  } finally {
     IOUtils.closeQuietly(counter);
  }
}