java - 如何清理JAVA应用程序的缓存

标签 java http

我使用 java.net 库制作了一个应用程序来捕获网页打开所花费的时间..示例代码

 HttpURLConnection conn = (HttpURLConnection)url.openConnection();
 for(int i=0; i<20; i++ )
  {
   conn.disconnect();
   conn.connect();
   long starTime = System.currentTimeMillis();
   JEditorPane editorPane = new JEditorPane();
   editorPane.setPage(new URL());
   long elasedTime = System.currentTimeMillis() - starTime;
   System.out.println(elasedTime);
  }

但是我遇到了一个问题,缓存!! 如果我反复打开网页,那么在某些情况下它会显示 time=0 mills...这当然是不可能的,请任何人帮助我!

最佳答案

setUseCaches(),继承自URLConnection,应该提供所需的内容:

public void setUseCaches(boolean usecaches)

Sets the value of the useCaches field of this URLConnection to
the specified value. 

Some protocols do caching of documents. Occasionally, it is important to be
able to "tunnel through" and ignore the caches (e.g., the "reload" button in
a browser). If the UseCaches flag on a connection is true, the connection is
allowed to use whatever caches it can. If false, caches are to be ignored.
The default value comes from DefaultUseCaches, which defaults to true. 

它必须在 connect() 之前调用。

关于java - 如何清理JAVA应用程序的缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10613648/

相关文章:

java - 如何在java中将一张图片添加到另一张图片上

java - 如何在java中的http post中设置用户名?

java - 如何解析原始cookie

Git代理绕过

java - C++ web 框架,如 spring for Java

java - 如何在智能 gwt 中隐藏 LinkItem 标题

Java:如何使用 Weka 生成的模型组装/创建用于分类的单个实例?

java - Android:使用带有端口号的 Uri.Builder().build() 创建 URL

javascript - 链接多个Node http请求

http - 检测到循环重定向路径并显示错误的 Open Graph 数据