我想访问本地Apache及其文件,比方说,http://foo.com/cats.jpg
我不希望我的客户看到图像,相反,接收数据,显示其文件大小和数据传输的持续时间,并刷新文件。我如何使用代码来实现它呢?
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谢谢你的帮助!
发布于 2015-04-18 02:55:58
执行html get操作时,它将返回响应。该响应包含包含重要信息的头以及内容(在本例中是.jpg文件)。标题可以告诉您文件的长度、mime类型等。如果需要显示文件长度,可以使用
response.getLastHeader("Content-Length").getValue()要确定数据传输所用的时间,只需在要计时的操作之前和之后调用System.currentTimeMillis(),并减去它们以知道操作花费了多少毫秒。例如:
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");https://stackoverflow.com/questions/29712203
复制相似问题