Here you can find the source of getURL(String path, URL context)
Parameter | Description |
---|---|
path | a parameter |
context | a parameter |
public static URL getURL(String path, URL context)
//package com.java2s; //License from project: Open Source License import java.io.*; import java.net.*; public class Main { /**/*from w ww . j av a2s. co m*/ * Given an address and the context (referrer URL/current directory), try to * create a URL object. * @param path * @param context * @return */ public static URL getURL(String path, URL context) { URL result; // If there's no context, first try to parse as a local file. if (context == null) { result = resolveFile(new File(path)); if (result != null) return result; } // Try to use standard URL handling to obtain the new URL. try { result = new URL(context, path); } catch (MalformedURLException ex) { return null; } // If we've found a file, try to resolve it in case it's a directory. if (result.getProtocol().equals("file")) { try { result = resolveFile(new File(result.toURI().getPath())); } catch (Exception e) { result = null; } } return result; } private static URL resolveFile(File file) { try { if (file.isDirectory()) file = getIndexFile(file); else if (!file.exists()) file = null; if (file != null) { return file.toURI().toURL(); } else return null; } catch (Exception e) { return null; } } private static File getIndexFile(File directory) { // Use FilenameFilter to select index.htm and index.html files. String[] indexFiles = directory.list(new FilenameFilter() { public boolean accept(File dir, String name) { return name.equalsIgnoreCase("index.html") || name.equalsIgnoreCase("index.htm"); } }); // Return the first one we find, else null. if (indexFiles.length > 0) return new File(indexFiles[0]); return null; } }