用Java编码URL的最佳方法 [英] Best way to encode URL in Java

查看:1062
本文介绍了用Java编码URL的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆