/** * Enumerates the resouces in a give package name. This works even if the resources are loaded * from a jar file! * * <p>Adapted from code by mikewse on the java.sun.com message boards. * http://forum.java.sun.com/thread.jsp?forum=22&thread=30984 * * @param packageName The package to enumerate * @return A Set of Strings for each resouce in the package. */ public static Set getResoucesInPackage(String packageName) throws IOException { String localPackageName; if (packageName.endsWith("/")) { localPackageName = packageName; } else { localPackageName = packageName + '/'; } Enumeration dirEnum = ClassLoader.getSystemResources(localPackageName); Set names = new HashSet(); // Loop CLASSPATH directories while (dirEnum.hasMoreElements()) { URL resUrl = (URL) dirEnum.nextElement(); // Pointing to filesystem directory if (resUrl.getProtocol().equals("file")) { File dir = new File(resUrl.getFile()); File[] files = dir.listFiles(); if (files != null) { for (int i = 0; i < files.length; i++) { File file = files[i]; if (file.isDirectory()) continue; names.add(localPackageName + file.getName()); } } // Pointing to Jar file } else if (resUrl.getProtocol().equals("jar")) { JarURLConnection jconn = (JarURLConnection) resUrl.openConnection(); JarFile jfile = jconn.getJarFile(); Enumeration entryEnum = jfile.entries(); while (entryEnum.hasMoreElements()) { JarEntry entry = (JarEntry) entryEnum.nextElement(); String entryName = entry.getName(); // Exclude our own directory if (entryName.equals(localPackageName)) continue; String parentDirName = entryName.substring(0, entryName.lastIndexOf('/') + 1); if (!parentDirName.equals(localPackageName)) continue; names.add(entryName); } } else { // Invalid classpath entry } } return names; }
/** * ** Gets the 'protocol' specification in the URI ** @return The 'protocol' specification in the * URL, or null if unable to determine protocol */ public String getProtocol() { try { URL url = new URL(this.getURI()); return url.getProtocol(); } catch (MalformedURLException mue) { return null; } }
void showContent() { URL address = null; try { address = new URL(addressTextField.getText()); } catch (MalformedURLException mue) { showWarning(mainFrame, "地址输入错误"); } System.out.println("getProtocol() " + address.getProtocol()); System.out.println("getHost() " + address.getHost()); System.out.println("getPort() " + address.getPort()); System.out.println("getPath() " + address.getPath()); System.out.println("getFile() " + address.getFile()); System.out.println("getQuery() " + address.getQuery()); }
/** ** Sets the 'port' */ public boolean setPort(int _port) { String uri = this.getURI(); if ((_port > 0) && URIArg.isAbsoluteURL(uri)) { try { URL oldURI = new URL(uri); String proto = oldURI.getProtocol(); String host = oldURI.getHost(); int port = _port; String file = oldURI.getFile(); URL newURI = new URL(proto, host, port, file); this._setURI(newURI.toString()); return true; } catch (MalformedURLException mue) { // error } } return false; }
private void downloadImage(final MercatorTextureTile tile, String mimeType) throws Exception { // System.out.println(tile.getPath()); final URL resourceURL = tile.getResourceURL(mimeType); Retriever retriever; String protocol = resourceURL.getProtocol(); if ("http".equalsIgnoreCase(protocol)) { retriever = new HTTPRetriever(resourceURL, new HttpRetrievalPostProcessor(tile)); } else { String message = Logging.getMessage("layers.TextureLayer.UnknownRetrievalProtocol", resourceURL); throw new RuntimeException(message); } retriever.setConnectTimeout(10000); retriever.setReadTimeout(20000); retriever.call(); }