Java URLEncoder改变特殊字符?
我想使用java的 URLEncoder 类 对我的 url 字符串进行编码。但是,默认情况下它将空格转换为“+”(加号)。我希望它使用“-”代替(类似于该网站对其网址进行编码的方式)。
我想知道做到这一点的最佳方法是什么?我应该只使用其内置功能,然后进行字符串替换(从“+”到“-”)吗?谢谢
I want to use java's URLEncoder class to encode my url strings. However, by default it converts spaces to '+' (plus sign). I'd like it to use '-' instead (similar to how this site encodes its urls).
I'm wondering what the best way to do this is? Should I just use its built in functionality and then do a string replace (from '+' to '-')? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
URLEncoder
根据特定合同对 URL 进行编码。您可以而且不应该不更改它。事实上,只需在之后进行字符串替换,或者在之前进行替换可能更好。即连字符-
已经是 URL 中的有效字符。The
URLEncoder
encodes the URL's according a specific contract. You can and should not change it. Indeed just do a string replace afterwards, or maybe better, beforehand. The hyphen-
is namely already a valid character in URL.同意@BalusC。
我还想指出,您想要做的并不是“URL 编码”一词正常意义上的真正“编码”。编码意味着有一个反向解码步骤,可以返回原始 URL。除非您有一些排除“-”字符的业务规则(例如,如果您从“文本”创建 URL 名称,则使用连字符),否则您的转换将是不可逆的。
您似乎正在做的是根据某些可能不可逆的规则将 URL 字符串转换为其他 URL 字符串。 URLEncoder 类(实现特定的标准化可逆编码)没有实现您的(特定于应用程序的)转换,这一点也不奇怪。
正如 @BalusC 指出的,正确的方法是在对 URL 字符串进行编码之前(根据应用程序的需求)对其进行转换。事实上,在组装完整的 URL 之前,在 URL 字符串的组件上实现转换可能会更好(例如更安全)。
(我理所当然地认为 URL 正在实际需要由 URLEncoder 实现的编码的上下文中使用。)
Agreed with @BalusC.
I would also like to point out that what you are trying to do is not really "encoding" in the normal sense of the phrase "URL encoding". Encoding implies that that there is a reverse decoding step that will give you the original URL back. Unless you have some business rule that precludes "-" characters (e.g. hyphens if you are creating URL names from "text"), your transformation will not be reversible.
What you appear to be doing is transforming URL strings into other URL strings according to some rule that probably not reversible. It is not at all surprising that the URLEncoder class (which implements a particular standardised reversible encoding) is not implementing your (application specific) transformation.
As @BalusC points out, the correct approach is to transform the URL strings (according to your application's needs) before you encode them. Indeed, it may be better (e.g. safer) to implement the transformation on the URL string's components ... before you assemble the complete URL.
(And I am taking for granted that the URLs are being used in a context that actually require the encoding implemented by URLEncoder.)
该网站不会用连字符“对其 URL 进行编码”,它首先以这种方式定义它们,显然这样就不需要编码。这取决于他们如何生成 URL。您可以执行相同的操作,但这不是 URLEncoding,而只是生成 URL。使用 URLEncoder 来完成此任务(它不是为此而设计的),并抱怨它完全按照它应该做的方式而不是您想要的方式进行操作,这是毫无意义的。正如 BalusC 所说,URLEncoder 是您的浏览器和世界各地的 Web 服务器之间合同的一部分。你无法改变这一点。
This site doesn't 'encode its URLs' with hyphens, it defines them that way in the first place, apparently so that no encoding is necessary. That's up to how they generate their URLs. You can do the same, but that's not URLEncoding, that's just generating URLs. Using URLEncoder for this task, which it is not designed for, and complaining that it does exactly what it is supposed to do and not what you want is pretty pointless. As BalusC said, URLEncoder is part of a contract between your browser and the Web servers of the world. You can't change that.