utf 8 Java URL编码器保留原始消息的大小写,但使UTF8代码小写
我正在使用URLEncoder。编码(信息“UTF-8”);对字符串进行编码强>
问题在于,我需要最后一个字符串中的UTF-8代码为小写,同时保留原始消息的大小写强>
例如:
消息:{消息
所需输出:%1消息
URL编码器。编码(“{Message”,“UTF-8”);->;%5b消息
URL编码器。编码(信息“UTF-8”)。toLowerCase()->;%5B信息
有没有办法改变URLEncoder的行为强>
或
对于所有UTF-8字符和任意字符串长度,是否有一种简单的方法可以在事后将所有UTF-8代码转换为小写强>
# 1 楼答案
我认为您的意思是希望URL转义符中的十六进制数字用小写字母表示(这些不是“UTF-8代码”)。无论如何,这是一个糟糕的问题,因为URL编码规范(也称为“百分比编码”)明确规定百分比代码中的十六进制数字不区分大小写。只有在这些十六进制数字的情况下才有差异的2%编码URL是等效的,因此,处理它们的代码可能是一个持续的问题,只要它在使用中
The docs of ^{} 非常简短。不需要太多的检查就可以看出,不,没有调节其行为这一方面的机制。您可以编写自己的实现(这并不难),或者您可以找到第三方编码器,但标准库的编码器将无法满足您的需求
这取决于你所说的“容易”是什么意思。原则上可以执行这样的转换,但是当您解析和更新编码的URL时,您所花费的精力至少是以您最初想要的方式执行编码所花费的两倍
但如果你真的想这么做,那么你可以使用这样的方法:
# 2 楼答案
我的解决方案是将URLEncoder源代码复制到一个新类中,并将hextr常量从“0123456789ABCDEF”更改为“0123456789ABCDEF”
由于可能存在许可问题,不打算在此处发布代码:
源版本:
# 3 楼答案
这已经硬编码到URLEncoder中(我正在查看Oracle的JDK btw)。转换基本上是转换字符,并获取每个字符的十六进制值,然后减去
'a' -'A'
(字符值的大小写差异)之间的差异,以强制使用大写值我认为你能绕过这个问题的唯一方法是使用反射并将
URLEncoder#caseDiff
修改为0,因为这个变量是静态的final:通过这样做(考虑一下这个psudeo代码,你会想再看一遍):
我真的很好奇你这么做的意图,我想知道这是否真的是你想做的