Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 3

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:http://hostname/my.jar!/"); // Create a URL that refers to a jar file in the file system url = new URL("jar:file:/c:/almanac/my.jar!/"); // Get the jar file JarURLConnection conn = (JarURLConnection)url.openConnection(); JarFile jarfile = conn.getJarFile(); // When no entry is specified on the URL, the entry name is null String entryName = conn.getEntryName(); // null

// Create a URL that refers to an entry in the jar file url = new URL("jar:file:/c:/almanac/my.jar!/com/mycompany/MyClass.class"); // Get the jar file conn = (JarURLConnection)url.openConnection(); jarfile = conn.getJarFile(); // Get the entry name; it should be the same as specified on URL entryName = conn.getEntryName(); // Get the jar entry JarEntry jarEntry = conn.getJarEntry(); } 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/ image.gi f " ); // Get the image java.awt.Image java.awt.Toolkit.getDefaultToolkit().getDefaultToolkit().createImage(url); } catch (MalformedURLException e) { } catch (IOException e) { } image =

Accessing a Password-Protected URL


COPY

// Install the custom authenticator Authenticator.setDefault(new MyAuthenticator());

// Access the page try { // Create a URL for the desired page URL url = new URL("ht tp : / / hos tname:80/ i ndex.html" ); // Read all the text returned by the server BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); String str; while ((str = in.readLine()) != null) { // str is one line of text; readLine() strips the newline character(s) } in.close(); } 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( "http://hostname:80/index.html#_top_"); String protocol = url.getProtocol(); // http String host = url.getHost(); // hostname int port = url.getPort(); // 80 String file = url.getFile(); // index.html String ref = url.getRef(); // _top_ } catch (MalformedURLException e) { }

Getting Text from a URL


COPY

try { / / Create a URL for the desi red page URL ur l = new URL( "http://hostname:80/index.html"); // Read all the text returned by the server BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); String str; while ((str = in.readLine()) != null) { // str is one line of text; readLine() strips the newline character(s) } in.close(); } 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("file://D:/almanac1.4/Ex1.java"); } catch (URISyntaxException e) { } // Convert an absolute URI to a URL try { url = uri.toURL(); } catch (IllegalArgumentException e) { // URI was not absolute } catch (MalformedURLException e) { } // Convert a URL to a URI try { uri = new URI(url.toString()); } catch (URISyntaxException e) { }

You might also like