Getting a Jar File Using a URL
COPY
try { // Create a URL that refers to a jar file on the net URL url = new URL("jar:[Link] // Create a URL that refers to a jar file in the file system url = new URL("jar:file:/c:/almanac/[Link]!/"); // Get the jar file JarURLConnection conn = (JarURLConnection)[Link](); JarFile jarfile = [Link](); // When no entry is specified on the URL, the entry name is null String entryName = [Link](); // null
// Create a URL that refers to an entry in the jar file url = new URL("jar:file:/c:/almanac/[Link]!/com/mycompany/[Link]"); // Get the jar file conn = (JarURLConnection)[Link](); jarfile = [Link](); // Get the entry name; it should be the same as specified on URL entryName = [Link](); // Get the jar entry JarEntry jarEntry = [Link](); } catch (MalformedURLException e) { } catch (IOException e) { }
Getting an Image from a URL
COPY
try { // Create a URL for the image's location URL url = new URL("ht tp : / / hos tname:80/ [Link] f " ); // Get the image [Link] [Link]().getDefaultToolkit().createImage(url); } catch (MalformedURLException e) { } catch (IOException e) { } image =
Accessing a Password-Protected URL
COPY
// Install the custom authenticator [Link](new MyAuthenticator());
// Access the page try { // Create a URL for the desired page URL url = new URL("ht tp : / / hos tname:80/ i [Link]" ); // Read all the text returned by the server BufferedReader in = new BufferedReader(new InputStreamReader([Link]())); String str; while ((str = [Link]()) != null) { // str is one line of text; readLine() strips the newline character(s) } [Link](); } catch (MalformedURLException e) { } catch (IOException e) { } public class MyAuthenticator extends Authenticator { // This method is called when a password-protected URL is accessed protected PasswordAuthentication getPasswordAuthentication() { // Get information about the request String promptString = getRequestingPrompt(); String hostname = getRequestingHost(); InetAddress ipaddr = getRequestingSite(); int port = getRequestingPort(); // Get the username from the user... String username = "myusername" ; // Get the password from the user... String password = "mypassword" ; / / Return the in fo rmat ion return new PasswordAuthent i ca t i on (username, password. toCharArray( ) ) ; } }
Parsing a URL
COPY
try { URL ur l = new URL( "[Link] String protocol = [Link](); // http String host = [Link](); // hostname int port = [Link](); // 80 String file = [Link](); // [Link] String ref = [Link](); // _top_ } catch (MalformedURLException e) { }
Getting Text from a URL
COPY
try { / / Create a URL for the desi red page URL ur l = new URL( "[Link] // Read all the text returned by the server BufferedReader in = new BufferedReader(new InputStreamReader([Link]())); String str; while ((str = [Link]()) != null) { // str is one line of text; readLine() strips the newline character(s) } [Link](); } catch (MalformedURLException e) { } catch (IOException e) { }
Converting Between a URL and a URI
COPY
URI uri = null; URL url = null; // Create a URI try { uri = new URI("[Link] } catch (URISyntaxException e) { } // Convert an absolute URI to a URL try { url = [Link](); } catch (IllegalArgumentException e) { // URI was not absolute } catch (MalformedURLException e) { } // Convert a URL to a URI try { uri = new URI([Link]()); } catch (URISyntaxException e) { }