Example usage for java.net URL URL

List of usage examples for java.net URL URL

Introduction

In this page you can find the example usage for java.net URL URL.

Prototype

public URL(URL context, String spec, URLStreamHandler handler) throws MalformedURLException 

Source Link

Document

Creates a URL by parsing the given spec with the specified handler within a specified context.

Usage

From source file:Main.java

public static void main(String args[]) throws Exception {
    URL webURL = new URL("http", "www.macfaq.com", "/vendor.html");
    System.out.println(webURL);/*from  w w  w  .ja v  a 2  s.  co m*/
    URL ftpURL = new URL("ftp", "ftp.macfaq.com", "/pub");
    System.out.println(ftpURL);

}

From source file:MainClass.java

public static void main(String[] args) {

    String host = "www.java2s.com";
    String file = "/index.html";

    String[] schemes = { "http", "https", "ftp", "mailto", "telnet", "file", "ldap", "gopher", "jdbc", "rmi",
            "jndi", "jar", "doc", "netdoc", "nfs", "verbatim", "finger", "daytime", "systemresource" };

    for (int i = 0; i < schemes.length; i++) {
        try {//from  ww w  .j a  v a2  s.com
            URL u = new URL(schemes[i], host, file);
            System.out.println(schemes[i] + " is supported\r\n");
        } catch (Exception ex) {
            System.out.println(schemes[i] + " is not supported\r\n");
        }
    }

}

From source file:SifterJava.java

public static void main(String[] args) throws Exception {
    /** Enter your sifter account and access key in the 
    ** string fields below *//*from ww  w.  j ava2  s  .co m*/

    // create URL object to SifterAPI
    String yourAccount = "your-account"; // enter your account name
    String yourDomain = yourAccount + ".sifterapp.com";
    URL sifter = new URL("https", yourDomain, "/api/projects");

    // open connectino to SifterAPI
    URLConnection sifterConnection = sifter.openConnection();

    // add Access Key to header request
    String yourAccessKey = "your-32char-sifterapi-access-key"; // enter your access key
    sifterConnection.setRequestProperty("X-Sifter-Token", yourAccessKey);

    // add Accept: application/json to header - also not necessary
    sifterConnection.addRequestProperty("Accept", "application/json");

    // URLconnection.connect() not necessary
    // getInputStream will connect

    // don't make a content handler, org.json reads a stream!

    // create buffer and open input stream
    BufferedReader in = new BufferedReader(new InputStreamReader(sifterConnection.getInputStream()));
    // don't readline, create stringbuilder, append, create string

    // construct json tokener from input stream or buffered reader
    JSONTokener x = new JSONTokener(in);

    // initialize "projects" JSONObject from string
    JSONObject projects = new JSONObject(x);

    // prettyprint "projects"
    System.out.println("************ projects ************");
    System.out.println(projects.toString(2));

    // array of projects
    JSONArray array = projects.getJSONArray("projects");
    int arrayLength = array.length();
    JSONObject[] p = new JSONObject[arrayLength];

    // projects
    for (int i = 0; i < arrayLength; i++) {
        p[i] = array.getJSONObject(i);
        System.out.println("************ project: " + (i + 1) + " ************");
        System.out.println(p[i].toString(2));
    }

    // check field names
    String[] checkNames = { "api_url", "archived", "api_issues_url", "milestones_url", "api_milestones_url",
            "api_categories_url", "issues_url", "name", "url", "api_people_url", "primary_company_name" };

    // project field names
    String[] fieldNames = JSONObject.getNames(p[0]);
    int numKeys = p[0].length();
    SifterProj[] proj = new SifterProj[arrayLength];

    for (int j = 0; j < arrayLength; j++) {
        proj[j] = new SifterProj(p[j].get("api_url").toString(), p[j].get("archived").toString(),
                p[j].get("api_issues_url").toString(), p[j].get("milestones_url").toString(),
                p[j].get("api_milestones_url").toString(), p[j].get("api_categories_url").toString(),
                p[j].get("issues_url").toString(), p[j].get("name").toString(), p[j].get("url").toString(),
                p[j].get("api_people_url").toString(), p[j].get("primary_company_name").toString());
        System.out.println("************ project: " + (j + 1) + " ************");
        for (int i = 0; i < numKeys; i++) {
            System.out.print(fieldNames[i]);
            System.out.print(" : ");
            System.out.println(p[j].get(fieldNames[i]));
        }
    }
}

From source file:dk.qabi.imapfs.IMAPMount.java

public static void main(String[] args) throws MessagingException, MalformedURLException {

    if (args.length < 2) {
        System.out.println("[Error]: Must specify a mounting point");
        System.out.println();/*from   w  w w  .  j  a  va2 s  .c  om*/
        System.out.println("[Usage]: imapfsmnt <mounting point>");
        System.exit(-1);
    }

    final String urlSpec = args[0];
    final URL url = new URL(null, urlSpec, new IMAPStreamHandler());
    final String mountpoint = args[1];

    String[] fs_args = new String[4];
    fs_args[0] = "-f";
    fs_args[1] = "-s";
    fs_args[2] = mountpoint;
    fs_args[3] = "-ovolname=" + url.getHost() + ",fssubtype=7";

    Filesystem imapfs = new LoggingFilesystem(new IMAPFileSystem(url), LogFactory.getLog("dk.qabi.imapfs"));

    File m = new File(mountpoint);
    if (!m.exists())
        m.mkdirs();

    try {
        FuseMount.mount(fs_args, imapfs);
    } catch (Exception e) {
        e.printStackTrace();
    }

}