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 String realUrl(String target) {
    try {/*from  www. j  a v  a  2  s .co m*/
        URL url = new URL(target);

        String protocol = url.getProtocol();
        String host = url.getHost();
        String path = url.getPath();
        String query = url.getQuery();

        path = URLEncoder.encode(path, "utf-8").replace("%3A", ":").replace("%2B", "+").replace("%2C", ",")
                .replace("%5E", "^").replace("%2F", "/").replace("%21", "!").replace("%24", "$")
                .replace("%25", "%").replace("%26", "&").replace("%28", "(").replace("%29", ")")
                .replace("%40", "@").replace("%60", "`");
        // .replace("", "#"); // not support.

        StringBuilder urlBuild = new StringBuilder(protocol).append("://").append(host).append(path);
        if (query != null)
            urlBuild.append("?").append(query);
        return urlBuild.toString();
    } catch (IOException e) {
        return target;
    }
}

From source file:Main.java

static String fileUrlToFilePath(String mediaPath) throws IOException {
    try {/*from   ww  w  . ja  v  a  2  s . co m*/
        return new File(new URL(mediaPath).toURI()).getAbsolutePath();
    } catch (IllegalArgumentException e) {
        throw new IOException("Unable to determine file path of file url " + mediaPath);
    } catch (Exception e) {
        throw new IOException("Unable to determine file path of file url " + mediaPath);
    }
}

From source file:Main.java

public static Object fetch(String address) throws MalformedURLException, IOException {
    URL url = new URL(address);
    Object content = url.getContent();
    return content;
}

From source file:Main.java

public static Document getDocFromURL(String url) throws Exception {
    return getDoc(new URL(url));
}

From source file:Main.java

private static HttpURLConnection getHttpURLConnection(String strURL) throws IOException {
    URL url = new URL(strURL);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setConnectTimeout(10000);/*from  w  w w . j ava2 s  .c  o m*/
    conn.setReadTimeout(15000);
    return conn;
}

From source file:Main.java

protected static InputStream getInputStream(String path) throws IOException {
    URL url = new URL(path);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    return connection.getInputStream();
}

From source file:Main.java

public static boolean isInternetWorking() {
    boolean success = false;
    try {//w  w w .j a v  a2  s. c om
        URL url = new URL("https://google.com");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setConnectTimeout(8000);
        connection.connect();
        success = connection.getResponseCode() == 200;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return success;
}

From source file:Main.java

public static String restoreAvatarUrl(String stringUrl) {
    try {/*from  w  w w.j av a  2  s. c  om*/
        StringBuilder sb = new StringBuilder();
        URL url = new URL(stringUrl);
        sb.append(url.getProtocol()).append("://").append(url.getHost()).append(url.getPath());
        return sb.toString();
    } catch (MalformedURLException e) {
        return stringUrl;
    }
}

From source file:Main.java

public static boolean isInternetWorking() {
    boolean success = false;
    try {/*  w w  w .ja va2s  .  c  o  m*/
        URL url = new URL("https://google.com");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setConnectTimeout(10000);
        connection.connect();
        success = connection.getResponseCode() == 200;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return success;
}

From source file:Main.java

public static InputStream getIS(String address) {
    InputStream is = null;//w  w  w .j a  v a 2s  .c o m
    try {
        URL url = new URL(address);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoInput(true);
        conn.connect();
        is = conn.getInputStream();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return is;
}