URLBuilder简单快速构建URL链接
来源:互联网 发布:淘宝网男士短袖 编辑:程序博客网 时间:2024/06/14 06:35
URLBuilder快速构建一个URL结构,使用了Builder建造者模式,大大增加了代码的可读性,并且可以支持参数URL编码。
package com.kaishustory.quick.commons.text;import java.nio.ByteBuffer;import java.nio.charset.Charset;public class URLBuilder {private static boolean[] eucIgnore = new boolean[256];protected StringBuilder builder = new StringBuilder();protected boolean firstParam = true;protected boolean hasPath = false;private static String percentify(String s) {StringBuilder sb = new StringBuilder();ByteBuffer bb = Charset.forName("utf-8").encode(s);int size = bb.limit();for (int i = 0; i < size; i++) {sb.append(String.format("%%%02x", new Object[] { Integer.valueOf(bb.get() & 0xFF) }));}return sb.toString();}public static String encodeURIComponent(String s) {if (s == null) {return null;}if ("".equals(s)) {return "";}StringBuilder sb = new StringBuilder();int _i = 0;int c = Character.codePointAt(s, _i);boolean ignore = (c < 256) && (eucIgnore[c]!=false);for (int i = 0; i < s.length(); i++) {c = Character.codePointAt(s, i);if (ignore != ((c < 256) && (eucIgnore[c] != false))) {if (ignore)sb.append(s.substring(_i, i));elsesb.append(percentify(s.substring(_i, i)));ignore = !ignore;_i = i;}}if (ignore)sb.append(s.substring(_i, s.length()));else {sb.append(percentify(s.substring(_i, s.length())));}return sb.toString();}protected void appendParamPrefix() {if (this.firstParam) {this.firstParam = false;if (this.hasPath)this.builder.append('?');} else {this.builder.append('&');}}public URLBuilder appendPath(String path) {if (path == null) {return this;}if ((this.hasPath) || (!this.firstParam)) {throw new IllegalStateException("Missed the trick to set path.");}this.hasPath = true;this.builder.append(path);return this;}public URLBuilder appendParam(String key, String value) {if ((key != null) && (value != null)) {appendParamPrefix();this.builder.append(key).append('=');this.builder.append(value);}return this;}public URLBuilder appendParamEncode(String key, String value) {if ((key != null) && (value != null)) {appendParamPrefix();this.builder.append(key).append('=');this.builder.append(encodeURIComponent(value));}return this;}public URLBuilder appendParamEncode(String key, String value, String charset) {appendParamEncode(key, value);return this;}public URLBuilder appendLabel(String label) {this.builder.append('#').append(label);return this;}public URLBuilder append(String str) {this.builder.append(str);return this;}public String toString() {return this.builder.toString();}static {String ignore = "1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM-_.!~*'()";for (int i = 0; i < ignore.length(); i++)eucIgnore[Character.codePointAt(ignore, i)] = true;}public static void main(String[] args){URLBuilder urlBuilder = new URLBuilder();urlBuilder.appendPath("https://weixin.kaishustory.com").appendParam("key1", "value1").appendParam("key2", "value2").appendParamEncode("ekey3", "{$#你好}").appendLabel("label");System.err.println(urlBuilder.toString());
}}
//https://weixin.kaishustory.com?key1=value1&key2=value2&ekey3=%7b%24%23%e4%bd%a0%e5%a5%bd%7d#label
阅读全文
0 0
- URLBuilder简单快速构建URL链接
- Maven简单、快速构建spring mvc项目
- url链接
- URL链接
- 【URL】获得URL链接
- Flex3 快速入门:构建简单的用户界面 使用控件
- Flex3 快速入门:构建简单的用户界面 使用控件
- Flex3 快速入门:构建简单的用户界面 使用容器
- Flex 3快速入门: 构建简单的用户界面 添加效果
- Flex 快速入门:构建简单的用户界面 创建状态
- Flex3 快速入门:构建简单的用户界面 使用容器
- OpWeb -- 快速构建一个简单的Ajax聊天程序
- .net构建url
- 构建URL 编码设置
- 编译、链接和构建:
- 软链接构建
- 关于构建的链接
- url链接DOM4J分析
- centos7.2如何升级到centos7.3最新版本
- tomcat优化
- Java单例模式及延迟加载
- Session会话管理实例
- UnpooledDirectByteBuf源码分析
- URLBuilder简单快速构建URL链接
- Letter by Letter 字典树+dp
- 解决问题的通用思路
- 动态代理笔记
- 垃圾收集器和内存分配
- Winsock(微软套接字接口)的使用(1)
- 使用phpword导出word 中文乱码问题[IDE是PhpStrom]
- android 属性动画总结
- 简明 VIM 练级攻略