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("不存在");
}
该博客介绍了两种Java方法来检查远程服务器上文件是否存在。方法一是通过响应头的HTTP状态码判断,如果返回404,则表示文件不存在。方法二是通过Content-Length字段的大小,如果长度大于0,则表示文件存在。这两种技术对于服务器间通信和文件操作具有实用价值。
1967

被折叠的 条评论
为什么被折叠?



