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(String spec) throws MalformedURLException 

Source Link

Document

Creates a URL object from the String representation.

Usage

From source file:Main.java

public static URL getConsoleUrl() {
    try {// w w  w  .j a  va 2  s. c  om
        URL url = new URL("http://localhost:8080/jbpm-console/");
        return url;
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static boolean delete(String murl) throws Exception {
    URL url = new URL(murl);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("DELETE");
    conn.setConnectTimeout(5000);/* ww  w .j  ava 2  s  .  c  o m*/
    if (conn.getResponseCode() == 204) {
        return true;
    }
    return false;
}

From source file:Main.java

public static final URL safeCreateUrl(final String url) {
    try {//from ww w.  java2 s. c o m
        return new URL(url);
    } catch (MalformedURLException ex) {
        return null;
    }
}

From source file:Main.java

public static String getPathFromUrl(String url) {
    URL iurl;/*w  w  w  .j a  va 2  s .co m*/
    try {
        iurl = new URL(url);
        return iurl.getPath();

    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return "";
    }
}

From source file:Main.java

public static InputStream getRequest(String path) throws Exception {
    URL url = new URL(path);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("GET");
    conn.setConnectTimeout(5000);//  w  ww.j  a  v  a  2 s . c om
    if (conn.getResponseCode() == 200) {
        return conn.getInputStream();
    }
    return null;
}

From source file:Main.java

public static String getURLContent(String urlStr) throws Exception {
    BufferedReader br = null;//from w ww.j a  va  2s.  c  om
    URL url = new URL(urlStr);
    InputStream ins = url.openStream();
    br = new BufferedReader(new InputStreamReader(ins));

    StringBuilder sb = new StringBuilder();
    String msg = null;
    while ((msg = br.readLine()) != null) {
        sb.append(msg);
        sb.append("\n"); // Append a new line
    }
    br.close();
    return sb.toString();
}

From source file:Main.java

public static String getHost(String url) throws MalformedURLException {
    return new URL(url).getHost();
}

From source file:Main.java

public static DataInputStream getStream(String filePath) throws Exception {
    URL url1 = new URL(filePath);
    HttpURLConnection conn = (HttpURLConnection) url1.openConnection();
    DataInputStream in = new DataInputStream(conn.getInputStream());
    return in;//w w  w  .j  a v  a  2s  .c  o m
}

From source file:Main.java

public static String getNoQueryUrl(String source) {
    String dest = null;/*from   ww w  .  j  a va  2s .c  o m*/
    try {
        URL sUrl = new URL(source);
        URL dUrl = new URL(sUrl.getProtocol(), sUrl.getHost(), sUrl.getPort(), sUrl.getPath());
        dest = dUrl.toString();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    return dest;
}

From source file:Main.java

public static String getQueryValue(String url, String name) {
    try {//from   w w  w .  j a v  a 2s .c o m
        URL dUrl = new URL("http://" + url);
        Map<String, String> query = getQueryMap(dUrl.getQuery());
        return query.get(name);

    } catch (Exception e) {
    }

    return null;
}