我使用 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/