JDK 是否有一个类可以进行 HTML 编码(但不能进行 URL 编码)?
我当然熟悉 java.net.URLEncoder
和 java.net.URLDecoder
类。 然而,我只需要 HTML 风格的编码。 (我不希望 ' '
替换为 '+'
等)。 我不知道有任何 JDK 内置类只能执行 HTML 编码。 有吗? 我知道其他选择(例如, Jakarta Commons Lang 'StringEscapeUtils',但我不想在需要它的项目中添加另一个外部依赖项。
我希望最近的 JDK 中已添加一些内容(又名 5 或 6)这会做我不知道的事情,否则我必须自己动手。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
没有内置的 JDK 类可以执行此操作,但它是 Jakarta commons-lang 库的一部分。
查看 JavaDoc
添加依赖项通常就像将 jar 放在某处一样简单,并且 commons-lang 有如此多有用的实用程序,因此通常值得将其加入其中。
There isn't a JDK built in class to do this, but it is part of the Jakarta commons-lang library.
Check out the JavaDoc
Adding the dependency is usually as simple as dropping the jar somewhere, and commons-lang has so many useful utilities that it is often worthwhile having it on board.
一种简单的方法似乎是这样的:
但如果您确实需要转义所有非 ASCII 符号,即您将在 7 位编码上传输编码文本,则将最后一个 else 替换为:
A simple way seem to be this one:
But if you do need to escape all non ASCII symbols i.e. you'll transmit encoded text on 7bit encoding then replace the last else with:
显然,答案是“不”。 不幸的是,在这种情况下,我必须做一些事情,并且在短期内无法为其添加新的外部依赖项。 我同意大家的看法,使用 Commons Lang 是最好的长期解决方案。 一旦我可以向项目添加新库,这就是我将要采用的方法。
遗憾的是 Java API 中没有如此常用的东西。
Apparently, the answer is, "No." This was unfortunately a case where I had to do something and couldn't add a new external dependency for it -- in the short term. I agree with everyone that using Commons Lang is the best long-term solution. This is what I will go with once I can add a new library to the project.
It's a shame that something of such common use is not in the Java API.
我发现我审查过的所有现有解决方案(库)都存在以下一个或多个问题:
'
替换单引号(错误!)除此之外,我还遇到了不的问题能够引入外部库,至少没有一定数量的繁文缛节。
所以,我自己推出了。 有罪的。
下面是它的样子,但最新版本始终可以在此要点中找到。
TODO:保留连续的空格。
I've found that all existing solutions (libraries) I've reviewed suffered from one or several of the below issues:
'
(wrong!)On top of this I also had the problem of not being able to bring in an external library, at least not without a certain amount of red tape.
So, I rolled my own. Guilty.
Below is what it looks like but the latest version can always be found in this gist.
TODO: Preserve consecutive whitespace.
请不要自己卷。 使用雅加达 Commons Lang。 它经过测试并证明有效。 除非必要,否则不要编写代码。 “不是在这里发明的”或“不是另一个依赖项”并不是决定选择/编写什么内容的很好的基础。
Please don't roll your own. Use Jakarta Commons Lang. It is tested and proven to work. Don't write code until you have to. "Not invented here" or "Not another dependency" is not a very good base for deciding what to choose / write.
不,我建议使用您提到的 StringEscapeUtils,或者例如 JTidy (http://jtidy.sourceforge.net/multiproject/jtidyservlet/apidocs/org/w3c/tidy/servlet/util/HTMLEncode.html)。
No. I would recommend using the StringEscapeUtils you mentioned, or for example JTidy (http://jtidy.sourceforge.net/multiproject/jtidyservlet/apidocs/org/w3c/tidy/servlet/util/HTMLEncode.html).
我建议使用 org.springframework.web.util.HtmlUtils.htmlEscape(String input)
可能会有所帮助。
I will suggest use org.springframework.web.util.HtmlUtils.htmlEscape(String input)
may be this will help.