Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.IOException;
import java.net.URL;
import java.net.URLEncoder;

public class Main {
    public static String realUrl(String target) {
        try {
            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;
        }
    }
}