java跨服务器查询文件是否存在
方法一:(通过ResponseHeader是否有"HTTP/1.1 404 Not Found" 来判断是否存在对应的文件)
URL serverUrl = new URL("http://localhost:8090/Demo/clean.sql");
HttpURLConnection urlcon = (HttpURLConnection) serverUrl.openConnection();
String message = urlcon.getHeaderField(0);
if (message != null && message.startsWith("HTTP/1.1 404")) {
System.out.println("不存在");
}else{
System.out.println("存在"+message);
}
方法二:(通过ResponseHeader中Content-Length的size来判断文件是否存在)
URL url = new URL("http://localhost:8090/Demo/clean.sql");
HttpURLConnection urlcon2 = (HttpURLConnection) url.openConnection();
Long TotalSize=Long.parseLong(urlcon2.getHeaderField("Content-Length"));
if (TotalSize>0){
System.out.println("存在");
}else{
System.out.println("不存在");
}