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

如何清理JAVA应用程序的缓存,java,http,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 editor

我使用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);
  }
HttpURLConnection conn=(HttpURLConnection)url.openConnection();

对于(int i=0;i您可以在
URLConnection
(或
HttpURLConnection
)中禁用缓存


您可以在
URLConnection
(或
HttpURLConnection
)中禁用缓存


您可以通过设置
conn.setUseCaches(false)
选择不使用任何现有缓存。这可能会提高计算精度。

您可以通过设置
conn.setUseCaches(false)
选择不使用任何现有缓存。这可能会提高计算精度。

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. 公共void setUseCaches(布尔usecaches) 将此URLConnection的useCaches字段的值设置为 指定的值。 有些协议对文档进行缓存。有时候,重要的是 能够“穿越”并忽略缓存(例如,中的“重新加载”按钮 浏览器)。如果连接上的UseCaches标志为true,则连接为 允许使用它可以使用的任何缓存。如果为false,缓存将被忽略。 默认值来自DefaultUseCaches,默认为true。 必须先调用它,然后从
URLConnection
继承的
connect()

setUseCaches()
才能提供所需的内容:

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. 公共void setUseCaches(布尔usecaches) 将此URLConnection的useCaches字段的值设置为 指定的值。 有些协议对文档进行缓存。有时候,重要的是 能够“穿越”并忽略缓存(例如,中的“重新加载”按钮 浏览器)。如果连接上的UseCaches标志为true,则连接为 允许使用它可以使用的任何缓存。如果为false,缓存将被忽略。 默认值来自DefaultUseCaches,默认为true。
必须在调用
connect()
之前调用它。rahul,您的程序中有一个巨大的错误。您的程序没有给出预期的结果,因为您没有正确编写它,具体来说,您的程序根本没有测量网页打开所需的时间

请尝试此代码-->

HttpURLConnection conn=(HttpURLConnection)url.openConnection();

对于(int i=0;irahul),您的程序中有一个巨大的错误。您的程序没有给出预期的结果,因为您没有正确编写它,具体来说,您的程序根本没有测量网页打开所需的时间

请尝试此代码-->

HttpURLConnection conn=(HttpURLConnection)url.openConnection();

对于(int i=0;iTry
conn.setUseCaches(false);
我认为您应该通过读取服务器发送的数据(从inputstream)来比较加载页面所花费的时间。这将帮助您获得更准确的页面加载持续时间。@RaviJain:请使用conn.setUseCaches(false)进行详细解释;对我没有帮助…仍然获得o作为值当前您的程序只测量连接服务器所用的时间,您没有测量读取从服务器发送的数据所用的时间(图像、html标记)。从服务器读取响应实际上需要几毫秒,因此捕获该持续时间可以帮助您获得更准确的结果try
conn.setUseCaches(false);
我认为您应该通过读取服务器发送的数据(从inputstream)来比较加载页面所需的时间。这将帮助您获得更准确的页面加载持续时间。@RaviJain:请进一步解释…使用conn.setUseCaches(false);对我没有帮助…仍然获得o作为值当前您的程序只测量连接服务器所用的时间,您没有测量读取从服务器发送的数据所用的时间(图像、html标记)。从服务器读取响应实际上需要几毫秒,因此捕获持续时间可以帮助您获得更准确的结果。我这样做是因为我只需要获取打开整个网页所需的时间…这就是我保持conn.connect超出时间间隔。插入这两行代码->InputStream stream=conn.getInputStream();stream.read(新字节[stream.available()]);after long starTime=System.currentTimeMillis();。还有一件事……我还需要捕获响应时间,即请求的最后一位到接收到的第一位所用的时间……对于响应时间,我只使用了conn.connect()但这也给我带来了问题有时它给我时间=0 mill我这样做是因为我只需要获得打开整个网页所需的时间…这就是我保持conn.connect超出时间间隔。插入这两行代码->InputStream=conn.getInputStream();stream.read(新字节[stream.available());long starTime=System.currentTimeMillis();。还有一件事……我还需要捕获响应时间,即从请求的最后一位到接收到的第一位所花费的时间……对于响应时间,我只使用了conn.connect(),但这有时也会给我带来问题,它给我的时间=0