{"id":679,"date":"2023-02-26T14:37:33","date_gmt":"2023-02-26T06:37:33","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=679"},"modified":"2023-04-29T18:22:34","modified_gmt":"2023-04-29T10:22:34","slug":"hutool-a-practical-java-tool-library","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/26\/hutool-a-practical-java-tool-library\/","title":{"rendered":"Hutool\uff0c\u4e00\u4e2a\u5b9e\u7528\u7684Java\u5de5\u5177\u7c7b\u5e93"},"content":{"rendered":"<h2>\u7b80\u4ecb<\/h2>\n<p><code>Hutool<\/code>\u8c10\u97f3 \u201c\u7cca\u6d82\u201d\uff0c\u5bd3\u610f\u8ffd\u6c42 \u201c\u4e07\u4e8b\u90fd\u4f5c\u7cca\u6d82\u89c2\uff0c\u65e0\u6240\u8c13\u5931\uff0c\u65e0\u6240\u8c13\u5f97\u201d \u7684\u5883\u754c\u3002<br \/>\n<code>Hutool<\/code>\u662f\u4e00\u4e2a Java \u5de5\u5177\u5305\uff0c\u4e5f\u53ea\u662f\u4e00\u4e2a\u5de5\u5177\u5305\uff0c\u5b83\u5e2e\u52a9\u6211\u4eec\u7b80\u5316\u6bcf\u4e00\u884c\u4ee3\u7801\uff0c\u51cf\u5c11\u6bcf\u4e00\u4e2a\u65b9\u6cd5\uff0c\u8ba9 Java \u8bed\u8a00\u4e5f\u53ef\u4ee5 \u201c\u751c\u751c\u7684\u201d\u3002<code>Hutool<\/code>\u6700\u521d\u662f\u6211\u9879\u76ee\u4e2d \u201cutil\u201d \u5305\u7684\u4e00\u4e2a\u6574\u7406\uff0c\u540e\u6765\u6162\u6162\u79ef\u7d2f\u5e76\u52a0\u5165\u66f4\u591a\u975e\u4e1a\u52a1\u76f8\u5173\u529f\u80fd\uff0c\u5e76\u5e7f\u6cdb\u5b66\u4e60\u5176\u5b83\u5f00\u6e90\u9879\u76ee\u7cbe\u9ad3\uff0c\u7ecf\u8fc7\u81ea\u5df1\u6574\u7406\u4fee\u6539\uff0c\u6700\u7ec8\u5f62\u6210\u4e30\u5bcc\u7684\u5f00\u6e90\u5de5\u5177\u96c6\u3002\uff08\u6284\u81ea\u4f5c\u8005\u7b80\u4ecb\uff09<\/p>\n<p><!-- more --><\/p>\n<p>\u5b98\u7f51\u5730\u5740\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"http:\/\/www.hutool.cn\/\">http:\/\/www.hutool.cn\/<\/a><br \/>\n\u5177\u4f53\u4f7f\u7528\u65b9\u6cd5\u8bf7\u770b\u6587\u6863\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"http:\/\/hutool.mydoc.io\/?t=255673\">http:\/\/hutool.mydoc.io\/?t=255673<\/a><\/p>\n<h2>\u529f\u80fd<\/h2>\n<p>\u4e00\u4e2a Java \u57fa\u7840\u5de5\u5177\u7c7b\uff0c\u5bf9\u6587\u4ef6\u3001\u6d41\u3001\u52a0\u5bc6\u89e3\u5bc6\u3001\u8f6c\u7801\u3001\u6b63\u5219\u3001\u7ebf\u7a0b\u3001XML \u7b49 JDK \u65b9\u6cd5\u8fdb\u884c\u5c01\u88c5\uff0c\u7ec4\u6210\u5404\u79cd Util \u5de5\u5177\u7c7b\uff0c\u540c\u65f6\u63d0\u4f9b\u4ee5\u4e0b\u7ec4\u4ef6\uff1a<\/p>\n<ul>\n<li><code>hutool-aop<\/code> <code>JDK<\/code>\u52a8\u6001\u4ee3\u7406\u5c01\u88c5\uff0c\u63d0\u4f9b\u975e<code>IOC<\/code>\u4e0b\u7684\u5207\u9762\u652f\u6301<\/li>\n<li><code>hutool-bloomFilter<\/code> \u5e03\u9686\u8fc7\u6ee4\uff0c\u63d0\u4f9b\u4e00\u4e9b<code>Hash<\/code>\u7b97\u6cd5\u7684\u5e03\u9686\u8fc7\u6ee4<\/li>\n<li><code>hutool-cache<\/code> \u7f13\u5b58<\/li>\n<li><code>hutool-core<\/code> \u6838\u5fc3\uff0c\u5305\u62ec<code>Bean<\/code>\u64cd\u4f5c\u3001\u65e5\u671f\u3001\u5404\u79cd<code>Util<\/code>\u7b49<\/li>\n<li><code>hutool-cron<\/code> \u5b9a\u65f6\u4efb\u52a1\u6a21\u5757\uff0c\u63d0\u4f9b\u7c7b<code>Crontab<\/code>\u8868\u8fbe\u5f0f\u7684\u5b9a\u65f6\u4efb\u52a1<\/li>\n<li><code>hutool-crypto<\/code> \u52a0\u5bc6\u89e3\u5bc6\u6a21\u5757<\/li>\n<li><code>hutool-db<\/code> <code>JDBC<\/code>\u5c01\u88c5\u540e\u7684\u6570\u636e\u64cd\u4f5c\uff0c\u57fa\u4e8e<code>ActiveRecord<\/code>\u601d\u60f3<\/li>\n<li><code>hutool-dfa<\/code> \u57fa\u4e8e<code>DFA<\/code>\u6a21\u578b\u7684\u591a\u5173\u952e\u5b57\u67e5\u627e<\/li>\n<li><code>hutool-extra<\/code> \u6269\u5c55\u6a21\u5757\uff0c\u5bf9\u7b2c\u4e09\u65b9\u5c01\u88c5\uff08\u6a21\u677f\u5f15\u64ce\u3001\u90ae\u4ef6\u7b49\uff09<\/li>\n<li><code>hutool-http<\/code> \u57fa\u4e8e<code>HttpUrlConnection<\/code>\u7684 Http \u5ba2\u6237\u7aef\u5c01\u88c5<\/li>\n<li><code>hutool-log<\/code> \u81ea\u52a8\u8bc6\u522b\u65e5\u5fd7\u5b9e\u73b0\u7684\u65e5\u5fd7\u95e8\u9762<\/li>\n<li><code>hutool-script<\/code> \u811a\u672c\u6267\u884c\u5c01\u88c5\uff0c\u4f8b\u5982<code>Javascript<\/code><\/li>\n<li><code>hutool-setting<\/code> \u529f\u80fd\u66f4\u5f3a\u5927\u7684<code>Setting<\/code>\u914d\u7f6e\u6587\u4ef6\u548c<code>Properties<\/code>\u5c01\u88c5<\/li>\n<li><code>hutool-system<\/code> \u7cfb\u7edf\u53c2\u6570\u8c03\u7528\u5c01\u88c5\uff08JVM \u4fe1\u606f\u7b49\uff09<\/li>\n<li><code>hutool-json<\/code> JSON \u5b9e\u73b0<\/li>\n<li><code>hutool-captcha<\/code> \u56fe\u7247\u9a8c\u8bc1\u7801\u5b9e\u73b0<\/li>\n<\/ul>\n<h2>\u7b80\u5355\u6d4b\u8bd5<\/h2>\n<h3>SecureUtil\uff08\u52a0\u5bc6\u89e3\u5bc6\u5de5\u5177\uff09<\/h3>\n<p>\u4e3b\u8981\u662f\u5728\u767b\u5f55\u7684\u65f6\u5019\u8fd8\u6709\u4fee\u6539\u5bc6\u7801\u7684\u65f6\u5019\u7528\u5230\u7684\uff0c\u56e0\u4e3a\u6570\u636e\u5e93\u91cc\u9762\u7684\u5bc6\u7801\u662f<code>md5<\/code>\u52a0\u5bc6\u5904\u7406\u7684\uff0c\u6240\u4ee5\u767b\u5f55\u7684\u65f6\u5019\u9700\u8981\u5148\u52a0\u5bc6\u4e4b\u540e\u518d\u5230\u6570\u636e\u5e93\u8fdb\u884c\u67e5\u8be2\uff0c\u4f7f\u7528<code>Hutool<\/code>\u7684\u8bdd\uff0c\u53ea\u9700\u8981\u8c03\u7528<code>SecureUtil<\/code>\u4e2d\u7684<code>md5<\/code>\u65b9\u6cd5\u5373\u53ef\uff1a<\/p>\n<pre><code class=\"language-java\">user = userService.userLoginByName(loginName, SecureUtil.md5(loginPwd));<\/code><\/pre>\n<h3>HtmlUtil\uff08HTML \u5de5\u5177\u7c7b\uff09<\/h3>\n<p>\u4f8b\u5982<code>HtmlUtil.encode<\/code>\uff0c\u53ef\u4ee5\u5c06\u4e00\u4e9b\u5b57\u7b26\u8f6c\u5316\u4e3a\u5b89\u5168\u5b57\u7b26\uff0c\u9632\u6b62<code>xss<\/code>\u6ce8\u5165\u548c<code>SQL<\/code>\u6ce8\u5165\uff0c\u6bd4\u5982\u4e0b\u9762\u7684\u8bc4\u8bba\u63d0\u4ea4\u3002<\/p>\n<pre><code class=\"language-java\">comment.setCommentAuthor(HtmlUtil.encode(comment.getCommentAuthor()));<\/code><\/pre>\n<p>\u53e6\u5916\uff0cHtmlUtil \u8fd8\u63d0\u4f9b\u4e86\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li><code>HtmlUtil.restoreEscaped<\/code> \u8fd8\u539f\u88ab\u8f6c\u4e49\u7684 HTML \u7279\u6b8a\u5b57\u7b26<\/li>\n<li><code>HtmlUtil.encode<\/code> \u8f6c\u4e49\u6587\u672c\u4e2d\u7684 HTML \u5b57\u7b26\u4e3a\u5b89\u5168\u7684\u5b57\u7b26<\/li>\n<li><code>HtmlUtil.cleanHtmlTag<\/code> \u6e05\u9664\u6240\u6709 HTML \u6807\u7b7e<\/li>\n<li><code>HtmlUtil.removeHtmlTag<\/code> \u6e05\u9664\u6307\u5b9a HTML \u6807\u7b7e\u548c\u88ab\u6807\u7b7e\u5305\u56f4\u7684\u5185\u5bb9<\/li>\n<li><code>HtmlUtil.unwrapHtmlTag<\/code> \u6e05\u9664\u6307\u5b9a HTML \u6807\u7b7e\uff0c\u4e0d\u5305\u62ec\u5185\u5bb9<\/li>\n<li><code>HtmlUtil.removeHtmlAttr<\/code> \u53bb\u9664 HTML \u6807\u7b7e\u4e2d\u7684\u5c5e\u6027<\/li>\n<li><code>HtmlUtil.removeAllHtmlAttr<\/code> \u53bb\u9664\u6307\u5b9a\u6807\u7b7e\u7684\u6240\u6709\u5c5e\u6027<\/li>\n<li><code>HtmlUtil.filter<\/code> \u8fc7\u6ee4 HTML \u6587\u672c\uff0c\u9632\u6b62 XSS \u653b\u51fb<\/li>\n<\/ul>\n<h3>CronUtil\uff08\u5b9a\u65f6\u4efb\u52a1\uff09<\/h3>\n<p>\u8fd9\u4e2a\u5de5\u5177\u5b8c\u5168\u4e0d\u9700\u8981\u7c7b\u4f3c<code>quartz<\/code>\u8fd9\u6837\u7684\u6846\u67b6\u6765\u505a\u5b9a\u65f6\u4efb\u52a1\uff0c\u800c\u4e14<code>CronUtil<\/code>\u4e5f\u4e0d\u9700\u8981\u4efb\u4f55\u5176\u4ed6\u4f9d\u8d56\uff0c\u53ea\u9700\u8981\u5728<code>resources<\/code>\u4e0b\u5efa\u4e00\u4e2a\u914d\u7f6e\u6587\u4ef6\uff0c\u7136\u540e\u5728\u7a0b\u5e8f\u542f\u52a8\u7684\u65f6\u5019\u5c06\u5b9a\u65f6\u4efb\u52a1\u5f00\u542f\u5373\u53ef\uff0c\u5982\u5b9a\u65f6\u5907\u4efd\u529f\u80fd\uff08\u6bcf\u5929\u51cc\u6668 1 \u70b9\u5907\u4efd\u4e00\u6b21\uff09\u3002<\/p>\n<p><code>cron.setting<\/code>:<\/p>\n<pre><code>cn.appblog.test.web.controller.admin.BackupController.backupResources = 0 0 1 * * ?\ncn.appblog.test.web.controller.admin.BackupController.backupDatabase = 0 0 1 * * ?\ncn.appblog.test.web.controller.admin.BackupController.backupPosts = 0 0 1 * * ?<\/code><\/pre>\n<pre><code class=\"language-java\">@Override\npublic void onApplicationEvent(ContextRefreshedEvent event){\n    this.loadActiveTheme();\n    this.loadOptions();\n    this.loadFiles();\n    this.loadThemes();\n    \/\/\u542f\u52a8\u5b9a\u65f6\u4efb\u52a1\n    CronUtil.start();\n    log.info(&quot;\u5b9a\u65f6\u4efb\u52a1\u542f\u52a8\u6210\u529f\uff01&quot;);\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7b80\u4ecb Hutool\u8c10\u97f3 \u201c\u7cca\u6d82\u201d\uff0c\u5bd3\u610f\u8ffd\u6c42 \u201c\u4e07\u4e8b\u90fd\u4f5c\u7cca\u6d82\u89c2\uff0c\u65e0\u6240\u8c13\u5931\uff0c\u65e0\u6240\u8c13\u5f97\u201d \u7684\u5883\u754c\u3002 Hutool\u662f\u4e00 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[],"class_list":["post-679","post","type-post","status-publish","format-standard","hentry","category-java-basic"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/679","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=679"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/679\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=679"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=679"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=679"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}