标签:
1、判断远程服务器上文件
import java.net.HttpURLConnection; import java.net.URL; public boolean checkRemoteFile(String fileUrl){ boolean flag = true; try { URL serverUrl = new URL(fileUrl); HttpURLConnection urlcon = (HttpURLConnection) serverUrl.openConnection(); String message = urlcon.getHeaderField(0);//文件存在‘HTTP/1.1 200 OK’ 文件不存在 ‘HTTP/1.1 404 Not Found’ if(message != null && !"".equals(message)){ if (message.startsWith("HTTP/1.1 404")) { flag = false; } }else{ flag = false; } } catch (Exception e) { e.printStackTrace(); flag = false; } return flag; }
2、判断本地文件是否存在
import java.io.File; public boolean checkLocalFile(String filePath){ boolean flag = true; File file = new File(filePath); if(!file.exists()) { flag = false; } return flag; }
标签:
原文地址:http://www.cnblogs.com/wuxiang/p/5015427.html