ورود

View Full Version : وجود یا عدم وجود فایل



Ebrahimkh
شنبه 04 مرداد 1393, 18:13 عصر
سلام دوستان

------------------------------

می خواستم بدونم چه جوری می تونیم بفهمیم که یک فایل
در یک مکانی از وب وجود داره یا نداره
مثلا چطوری می تونیم بفهمیم که این فایل در این مسیر وجود داره یا نه
http://www.ietf.org/rfc/rfc4288.txt

من هر راهی رو تست کردم ولی false برگردوند:گریه:


---------------------------


سپاسگذارم بخاطر راهنماییهاتون

Ebrahimkh
شنبه 04 مرداد 1393, 19:02 عصر
یه روش برا این کار پیدا کردم

بنظرتون از اینم که هست ساده تر میشه




public class Net {
public static String URL = "http://www.ietf.org/rfc";
public static void main(String a[]){

try {
System.out.println(getResponseCode(URL + "rfc4288.txt"));
System.out.println(getResponseCode(URL + "/rfc42108.txt"));
System.out.println(getResponseCode("http://www.example.com"));
System.out.println(getResponseCode("http://www.example.com/junk"));
} catch (IOException ex) {
Logger.getLogger(Net.class.getName()).log(Level.SE VERE, null, ex);
}

}
public static int getResponseCode(String urlString) throws MalformedURLException, IOException {
URL u = new URL(urlString);
HttpURLConnection huc = (HttpURLConnection) u.openConnection();
huc.setRequestMethod("GET");
huc.connect();
return huc.getResponseCode();
}
}