方法一:
URL serverUrl = new URL("http://localhost:8090/Demo/clean.sql");
HttpURLConnection urlcon = (HttpURLConnection) serverUrl.openConnection();
String message = urlcon.getHeaderField(0);
if (StringUtils.hasText(message) && message.startsWith("HTTP/1.1 404")) {
System.out.println("不存在");
}else{
System.out.println("存在"+message);
}
方法二:
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("不存在");
}
String filepath ="http://22.58.123.146:8002/bidpu/upload/E3502000072/20180426/1984fa0b9f6/招告/20180426招标公告.pdf"
try {
URL pathUrl = new URL(filepath);
HttpURLConnection urlcon = (HttpURLConnection) pathUrl.openConnection();
if(urlcon.getResponseCode()>=400){
System.out.println("文件不存在");
}else{
System.out.println("文件存在");
}
}catch (Exception e) {
System.out.print("请求失败");
}
最后
以上就是负责哑铃最近收集整理的关于java 根据url链接判断服务器上的文件是否存在有效的全部内容,更多相关java内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复