How can I parse dynamic string values in order to create URL instances? I need to replace spaces with %20, accents, non-ASCII characters...?

I tried to use URLEncoder but it also encodes / character and if I give a string encoded with URLEncoder to the URL constructor I get a MalformedURLException (no protocol).

解决方案

URLEncoder has a very misleading name. It is according to the Javadocs used encode form parameters using MIME type application/x-www-form-urlencoded.

With this said it can be used to encode e.g., query parameters. For instance if a parameter looks like &/?# its encoded equivalent can be used as:

String url = "http://host.com/?key=" + URLEncoder.encode("&/?#");

Unless you have those special needs the URL javadocs suggests using new URI(..).toURL which performs URI encoding according to RFC2396.

The recommended way to manage the encoding and decoding of URLs is to use URI

The following sample

new URI("http", "host.com", "/path/", "key=| ?/#ä", "fragment").toURL();

produces the result http://host.com/path/?key=%7C%20?/%23ä#fragment. Note how characters such as ?&/ are not encoded.

EDIT

Since your input is a string URL, using one of the parameterized constructor of URI will not help you. Neither can you use new URI(strUrl) directly since it doesn't quote URL parameters.

So at this stage we must use a trick to get what you want:

public URL parseUrl(String s) throws Exception {

URL u = new URL(s);

return new URI(

u.getProtocol(),

u.getAuthority(),

u.getPath(),

u.getQuery(),

u.getRef()).

toURL();

}

Before you can use this routine you have to sanitize your string to ensure it represents an absolute URL. I see two approaches to this:

Guessing. Prepend http:// to the string unless it's already present.

Construct the URI from a context using new URL(URL context, String spec)

java解析url字符串,将字符串解析为URL相关推荐

  1. jsp java json解析,jsp中获取json字符串,并解析

    JqueryDemo1 function showData() { var str='{ "name": "John" }';//json标准格式 var ob ...

  2. java代码中fastjson生成字符串和解析字符串的方法和javascript文件中字符串和json数组之间的转换方法...

    1.java代码中fastjson生成字符串和解析字符串的方法 List<TemplateFull> templateFulls = new ArrayList<TemplateFu ...

  3. 【Android NDK 开发】JNI 方法解析 ( 字符串数组参数传递 | 字符串遍历 | 类型强转 | Java 字符串与 C 字符串转换 | 字符串释放 )

    文章目录 I . C/C++ 中的 Java 字符串数组类型 II . 获取字符串数组长度 III . 获取字符串数组元素 IV . 类型强转 ( jobject -> jstring ) V ...

  4. java乱码base64,解决 JAVA WebSocket 解析 base64 后中文字符串乱码

    解决 JAVA WebSocket 解析 base64 后中文字符串乱码 解决 JAVA WebSocket 解析 base64 后中文字符串乱码 春风如贵客,一到便繁华.各位看官先赞再看,养成好习惯 ...

  5. java解析多层嵌套json字符串

    java解析多层嵌套json字符串 java分别解析下面两个json字符串 package jansonDemo;import com.alibaba.fastjson.JSON; import co ...

  6. java字符串根据分隔符取值_java中解析含分隔符的字符串

    java中解析含分隔符的字符串 (2010-03-24 16:43:58) 标签: 杂谈 字符串的分隔对于一个以某种分隔符标识字段的字符串,如果想得到串中每个字段项,常用的分隔方法能3种. 设args ...

  7. java,url长链接生成短链接,短链接生成器,自定义字符串,对字符串md5混合KEY加密,根据短链接获得key值,不重复的随机数,不重复的随机字符串...

    java,url长链接生成短链接,短链接生成器,自定义字符串,对字符串md5混合KEY加密,根据短链接获得key值,不重复的随机数,不重复的随机字符串 1 package com.zdz.test; ...

  8. c#解析json字符串数组_C#解析JSON字符串总结

    JSON文件读取到内存中就是字符串,.NET操作JSON就是生成与解析JSON字符串. 操作JSON通常有以下几种方式: 1. 原始方式:按照JSON字符串自己来解析. 2. 通用方式[★★★★★]: ...

  9. Jackson解析与生成Json字符串

    Jackson API:http://tool.oschina.net/apidocs/apidoc?api=jackson-1.9.9 流式API读取和写入JSON内容离散事件. JsonParse ...

最新文章

  1. Burpsuite+SQLMAP绕过Token保护(Burpsuite Macros应用)
  2. 搞懂正则表达式之基础篇
  3. 浅谈 UNIX、Linux、ios、android 他们之间的关系
  4. linux kset subsystem 3.10内核,Kobject、Kset 和 Subsystem
  5. 计算机小知识分享,分享几小个计算机操作技巧
  6. Mybatis Plus 的BaseMapper 和 Model有什么用
  7. Python 调度算法 死锁 静动态链接 分页分段(七)
  8. 学习笔记:GoogLeNet
  9. 电脑计算机配置应用程序兼容性,电脑怎么打开兼容模式怎么办
  10. 基于粒子群算法的PID控制器优化设计
  11. 选择一个网络托管业务域名代备案提供全面的解决方案
  12. 笔记本插入耳机没反应 必须重启前插入再启动才行 启动后拔下再插入依旧外放
  13. 谷歌翻译退出,idea谷歌翻译无法使用(解决)
  14. 超级爆笑:2008高考各省零分作文大全
  15. oracle lms进程 内存,lms进程耗用大量内存
  16. 【每日一题】P1551 亲戚
  17. macM1芯片通过第三方安装php
  18. matlab 非线性常微分方程,Matlab数值求解非线性常微分方程 - 仿真模拟 - 小木虫 - 学术 科研 互动社区...
  19. 学习如何使用php创建一张空白的图片
  20. smartGit系列之长期使用批处理脚本

热门文章

  1. 【Python】正则表达式使用、常用匹配表达式
  2. 【C# 委托 Lambda表达式】一个简单的例子
  3. vb 实现小超市饮料补货提醒程序 public全局变量的声明与初始化
  4. NumPy之:数据类型
  5. 语言题库体型判断问题_1000道Python题库系列分享20(43道填空与判断题)
  6. Leecode05. 最长回文子串——Leecode大厂热题100道系列
  7. 【简洁易懂】为什么判断素数时只需要循环到该数的平方根
  8. 数据结构学习笔记——顺序表的基本操作(超详细最终版+++)建议反复看看ヾ(≧▽≦*)o
  9. 浙江省职业技能考试大纲计算机,浙江省高校计算机等级考试大纲
  10. python虚拟机优化_KVM虚拟机管理及性能优化