首页 > 代码库 > Java下的URL生成类

Java下的URL生成类

  该类用于生成一个可用的URL,方便URL的生成和管理。

/**
 * 构造的URL
 * @author Alvin
 */
final public class UrlBuilder {

	// 协议名称
	private String scheme;

	// 服务器名称
	private String serverName;

	// 端口号
	private int serverPort;

	// 上下文名称
	private String[] paths;

	// 参数集合
	private Map<String, Object> parameters;

	// 地址缓冲
	private String urlBuffer = null;

	// 路径缓冲
	private String pathBuffer = null;
	
	// 参数缓冲
	private String parametersBuffer = null;
	
	/**
	 * 构造器
	 */
	public UrlBuilder(String scheme, String serverName, int serverPort, String[] paths) {
		if (paths != null) {
			for (int i = 0; i < paths.length; i++) {
				paths[i] = trimSeparator(paths[i]);
			}
		}
		this.scheme = scheme;
		this.serverName = serverName;
		this.serverPort = serverPort;
		this.paths = paths;
	}

	/**
	 * 构造器
	 */
	public UrlBuilder(String serverName, int serverPort, String[] paths) {
		this("http", serverName, serverPort, paths);
	}

	/**
	 * 构造器
	 */
	public UrlBuilder(String serverName, int serverPort) {
		this(serverName, serverPort, null);
	}

	/**
	 * 构造器
	 */
	public UrlBuilder(String serverName) {
		this(serverName, 80, null);
	}

	/**
	 * 去除字符串前后的 /
	 */
	private static String trimSeparator(String path) {
		boolean b1 = path.startsWith("/"), b2 = path.endsWith("/");
		if (b1 && b2) {
			path = path.substring(1, path.length() - 1);
		} else if (b1) {
			path = path.substring(1);
		} else if (b2) {
			path = path.substring(0, path.length() - 1);
		}
		return path;
	}

	/**
	 * 添加一个目录
	 */
	public void addPath(String path) {
		path = trimSeparator(path);
		if (path.isEmpty() == false) {
			pathBuffer = null;
			urlBuffer = null;
			if (paths == null) {
				paths = new String[] { path };
			} else {
				paths = Arrays.copyOf(paths, paths.length + 1);
				paths[paths.length - 1] = path;
			}
		}
	}

	/**
	 * 添加若干目录
	 */
	public void addPath(String...paths) {
		List<String> pathList = new ArrayList<>();
		for (String path : paths) {
			path = trimSeparator(path);
			if (path.isEmpty() == false) {
				pathList.add(path);
			}
		}
		if (pathList.isEmpty() == false) {
			pathBuffer = null;
			urlBuffer = null;
			if (this.paths == null) {
				this.paths = pathList.toArray(new String[pathList.size()]);
			} else {
				int len = this.paths.length;
				this.paths = Arrays.copyOf(this.paths, len + pathList.size());
				for (int i = 0; i < pathList.size(); i++) {
					this.paths[len + i] = pathList.get(i);
				}
			}
		}
	}

	/**
	 * 删除目录
	 */
	public String removePath(int index) {
		String value = http://www.mamicode.com/paths[index];>
  测试代码如下:

@Test
public void test() {
	UrlBuilder builder = new UrlBuilder("localhost", 8080);
	builder.addPath("xxx");
	System.out.println(builder.toString());
	
	builder.addPath("aaa", "bbb");
	System.out.println(builder.toString());
	
	builder.removePath(1);
	System.out.println(builder.toString());
	
	builder.addParameter("id", 100);
	System.out.println(builder.toString());
	
	builder.addParameter("name", "小李");
	System.out.println(builder.toString());
	
	builder.removeParameter("id");
	System.out.println(builder.toString());
	
	builder = UrlBuilder.parse("http://www.baidu.com:80/test/a.jsp?id=1");
	System.out.println(builder);
}