首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Apache Web Server Get -文件大小

Apache Web Server Get -文件大小
EN

Stack Overflow用户
提问于 2015-04-18 02:23:05
回答 1查看 1.4K关注 0票数 0

我想访问本地Apache及其文件,比方说,http://foo.com/cats.jpg

我不希望我的客户看到图像,相反,接收数据,显示其文件大小和数据传输的持续时间,并刷新文件。我如何使用代码来实现它呢?

代码语言:javascript
复制
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://foo.com/cats.jpg");
HttpResponse response = client.execute(request);

// Get the response but don't show the content, just file size and duration of data transfer

谢谢你的帮助!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-18 02:55:58

执行html get操作时,它将返回响应。该响应包含包含重要信息的头以及内容(在本例中是.jpg文件)。标题可以告诉您文件的长度、mime类型等。如果需要显示文件长度,可以使用

代码语言:javascript
复制
response.getLastHeader("Content-Length").getValue()

要确定数据传输所用的时间,只需在要计时的操作之前和之后调用System.currentTimeMillis(),并减去它们以知道操作花费了多少毫秒。例如:

代码语言:javascript
复制
long start = System.currentTimeMillis();
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://foo.com/cats.jpg");
HttpResponse response = client.execute(request);
String size = response.getLastHeader("Content-Length").getValue();
long end = System.currentTimeMillis();
System.out.println("It took "+(end-start)+" milliseconds and the file is "+
    size+" bytes long");
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29712203

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档