用Java编码URL的最佳方法 [英] Best way to encode URL in Java
问题描述
我有一个URL,我想将其嵌入到mailto
的正文中.到目前为止,我尝试了两种方法对URL进行编码,但两种方法都没有给我带来很好的效果:
I have a URL and I want to embed it in the body of a mailto
. Till now, I've tried 2 methods to encode the URL and both of them did not give me good results:
URLEncoder
-这使我在电子邮件中加上了加号,因为URLEncoder显然仅适用于查询参数.
URLEncoder
- this gave me plus signs in the e-mail message since apparently URLEncoder is appropriate for query parameters only.
org.apache.commons.httpclient.URI
-这没有给我完整的URL.它给我的效果与我之前在此处的帖子中所解释的相同: Escape& MailTo中的符号
org.apache.commons.httpclient.URI
- this doesn't give me the complete URL. It gives me the same results as I had explained in a post earlier here: Escape & symbol in MailTo
我该怎么办?
谢谢:) Krt_Malta
Thanks :) Krt_Malta
推荐答案
如何使用httpclient
中的URIUtil
?
URIUtil.encodeQuery(strUrl, "UTF-8");
编码以下地址mailto:jo han.sjoberg@m.com
给出mailto:jo%20han.sj%C3%83%C2%B6berg@m.com
尽管从技术上讲,+
和%20
都是空白的可接受编码.
Technically though, both +
and %20
are acceptable encoding for a whitespace.
这篇关于用Java编码URL的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!