{"id":10021208,"date":"2026-01-25T00:08:55","date_gmt":"2026-01-24T16:08:55","guid":{"rendered":"https:\/\/itmax.cn\/?p=10021208"},"modified":"2026-01-25T00:08:55","modified_gmt":"2026-01-24T16:08:55","slug":"java-%e5%ae%9e%e6%88%98-%e5%ad%97%e7%ac%a6%e7%bc%96%e7%a0%81%e9%97%ae%e9%a2%98%e8%a7%a3%e5%86%b3%e6%96%b9%e6%a1%88","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10021208","title":{"rendered":"Java \u5b9e\u6218 &#8211; \u5b57\u7b26\u7f16\u7801\u95ee\u9898\u89e3\u51b3\u65b9\u6848"},"content":{"rendered":"<h2>\u524d\u8a00<\/h2>\n<p>\u6700\u8fd1\u5728\u505a\u4e00\u4e2a Java \u9879\u76ee\u7684\u65f6\u5019\uff0c\u9047\u5230\u4e86\u4e00\u4e2a\u8ba9\u4eba\u5934\u75bc\u7684\u95ee\u9898\uff1a\u5728 Windows \u4e0a\u5f00\u53d1\u7684\u65f6\u5019\uff0c\u4e2d\u6587\u5b57\u7b26\u663e\u793a\u6b63\u5e38\uff0c\u4f46\u90e8\u7f72\u5230 Linux \u670d\u52a1\u5668\u4e0a\u5c31\u53d8\u6210\u4e71\u7801\u4e86\u3002\u521a\u5f00\u59cb\u4ee5\u4e3a\u662f\u6570\u636e\u5e93\u7684\u95ee\u9898\uff0c\u68c0\u67e5\u4e86\u6570\u636e\u5e93\u5b57\u7b26\u96c6\u4e5f\u6ca1\u95ee\u9898\u3002\u540e\u6765\u624d\u53d1\u73b0\uff0c\u539f\u6765\u662f\u4e0d\u540c\u7cfb\u7edf\u7684\u9ed8\u8ba4\u7f16\u7801\u4e0d\u4e00\u81f4\u5bfc\u81f4\u7684\u3002<\/p>\n<p>\u76f8\u4fe1\u5f88\u591a\u5f00\u53d1\u8005\u90fd\u9047\u5230\u8fc7\u7c7b\u4f3c\u7684\u95ee\u9898\uff1a\u4ee3\u7801\u91cc\u5199\u7684\u4e2d\u6587\uff0c\u5728\u4e0d\u540c\u73af\u5883\u4e0b\u663e\u793a\u4e0d\u4e00\u6837\uff1b\u4ece\u6570\u636e\u5e93\u8bfb\u53d6\u7684\u6570\u636e\uff0c\u6709\u65f6\u5019\u662f\u4e71\u7801\uff1b\u65e5\u5fd7\u6587\u4ef6\u91cc\u7684\u4e2d\u6587\u663e\u793a\u4e0d\u6b63\u5e38\u3002\u8fd9\u4e9b\u95ee\u9898\u867d\u7136\u770b\u8d77\u6765\u7b80\u5355\uff0c\u4f46\u5982\u679c\u4e0d\u4e86\u89e3\u5b57\u7b26\u7f16\u7801\u7684\u539f\u7406\uff0c\u53ef\u80fd\u4f1a\u6298\u817e\u5f88\u4e45\u3002\u4eca\u5929\u6211\u4eec\u5c31\u6765\u804a\u804a\u5b57\u7b26\u7f16\u7801\u95ee\u9898\u7684\u539f\u56e0\u548c\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<h2>\u95ee\u9898\u80cc\u666f<\/h2>\n<p>\u5b57\u7b26\u7f16\u7801\u95ee\u9898\u662f\u4e00\u4e2a\u5f88\u5e38\u89c1\u4f46\u53c8\u5bb9\u6613\u88ab\u5ffd\u89c6\u7684\u95ee\u9898\u3002\u4e0d\u540c\u7684\u7cfb\u7edf\u3001\u4e0d\u540c\u7684\u73af\u5883\uff0c\u9ed8\u8ba4\u7684\u5b57\u7b26\u7f16\u7801\u53ef\u80fd\u4e0d\u4e00\u6837\uff0c\u8fd9\u5c31\u5bfc\u81f4\u4e86\u5404\u79cd\u4e71\u7801\u95ee\u9898\u3002<\/p>\n<h3>\u4e3a\u4ec0\u4e48\u4f1a\u51fa\u73b0\u7f16\u7801\u95ee\u9898<\/h3>\n<p>\u73b0\u5728\u7684\u8ba1\u7b97\u673a\u7cfb\u7edf\uff0c\u5e95\u5c42\u90fd\u662f\u4f7f\u7528\u4e8c\u8fdb\u5236\u6765\u5b58\u50a8\u6570\u636e\u7684\u3002\u4f46\u6211\u4eec\u5728\u5c4f\u5e55\u4e0a\u770b\u5230\u7684\u6587\u5b57\uff0c\u6bd4\u5982\u4e2d\u6587\u3001\u82f1\u6587\u3001\u65e5\u6587\u7b49\uff0c\u90fd\u662f\u5b57\u7b26\u3002\u8981\u628a\u5b57\u7b26\u8f6c\u6362\u6210\u4e8c\u8fdb\u5236\u5b58\u50a8\uff0c\u5c31\u9700\u8981\u7528\u5230\u5b57\u7b26\u7f16\u7801\u3002<\/p>\n<p><strong>\u5e38\u89c1\u7684\u5b57\u7b26\u7f16\u7801\uff1a<\/strong><\/p>\n<ul>\n<li><strong>ASCII<\/strong>\uff1a\u6700\u65e9\u7684\u5b57\u7b26\u7f16\u7801\uff0c\u53ea\u80fd\u8868\u793a 128 \u4e2a\u5b57\u7b26\uff0c\u4e3b\u8981\u662f\u82f1\u6587\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u4e00\u4e9b\u7b26\u53f7<\/li>\n<li><strong>GBK\/GB2312<\/strong>\uff1a\u4e2d\u6587\u7f16\u7801\uff0cWindows \u7cfb\u7edf\u9ed8\u8ba4\u4f7f\u7528<\/li>\n<li><strong>UTF-8<\/strong>\uff1a\u73b0\u5728\u6700\u6d41\u884c\u7684\u7f16\u7801\uff0c\u53ef\u4ee5\u8868\u793a\u4e16\u754c\u4e0a\u6240\u6709\u7684\u5b57\u7b26\uff0c\u8de8\u5e73\u53f0\u517c\u5bb9\u6027\u597d<\/li>\n<\/ul>\n<p><strong>\u95ee\u9898\u6839\u6e90\uff1a<\/strong><\/p>\n<p>\u4e0d\u540c\u7cfb\u7edf\u9ed8\u8ba4\u7684\u5b57\u7b26\u7f16\u7801\u4e0d\u4e00\u6837\uff1a<\/p>\n<ul>\n<li>Windows \u7cfb\u7edf\u9ed8\u8ba4\u4f7f\u7528 GBK \u6216 GB2312<\/li>\n<li>Linux \u7cfb\u7edf\u9ed8\u8ba4\u4f7f\u7528 UTF-8<\/li>\n<li>Mac \u7cfb\u7edf\u9ed8\u8ba4\u4f7f\u7528 UTF-8<\/li>\n<\/ul>\n<p>\u5982\u679c\u4f60\u7684\u4ee3\u7801\u5728 Windows \u4e0a\u5f00\u53d1\uff08\u9ed8\u8ba4 GBK\uff09\uff0c\u4f46\u90e8\u7f72\u5230 Linux \u670d\u52a1\u5668\u4e0a\uff08\u9ed8\u8ba4 UTF-8\uff09\uff0c\u5c31\u53ef\u80fd\u51fa\u73b0\u4e71\u7801\u95ee\u9898\u3002<\/p>\n<h3>\u5e38\u89c1\u7684\u4e71\u7801\u573a\u666f<\/h3>\n<p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u8fd9\u4e9b\u4e71\u7801\u573a\u666f\uff1a<\/p>\n<p><strong>\u573a\u666f\u4e00\uff1a\u6587\u4ef6\u8bfb\u53d6\u4e71\u7801<\/strong><\/p>\n<p>\u4ece\u6587\u4ef6\u8bfb\u53d6\u6570\u636e\u65f6\uff0c\u5982\u679c\u6587\u4ef6\u7684\u7f16\u7801\u548c\u8bfb\u53d6\u65f6\u4f7f\u7528\u7684\u7f16\u7801\u4e0d\u4e00\u81f4\uff0c\u5c31\u4f1a\u51fa\u73b0\u4e71\u7801\u3002\u6bd4\u5982\u6587\u4ef6\u662f UTF-8 \u7f16\u7801\u7684\uff0c\u4f46\u7528 GBK \u7f16\u7801\u53bb\u8bfb\u53d6\uff0c\u4e2d\u6587\u5c31\u4f1a\u53d8\u6210\u4e71\u7801\u3002<\/p>\n<p><strong>\u573a\u666f\u4e8c\uff1a\u6570\u636e\u5e93\u5b58\u50a8\u4e71\u7801<\/strong><\/p>\n<p>\u6570\u636e\u5e93\u7684\u5b57\u7b26\u96c6\u8bbe\u7f6e\u4e0d\u5bf9\uff0c\u6216\u8005\u8fde\u63a5\u6570\u636e\u5e93\u65f6\u6ca1\u6709\u6307\u5b9a\u6b63\u786e\u7684\u5b57\u7b26\u96c6\uff0c\u5b58\u50a8\u548c\u8bfb\u53d6\u7684\u6570\u636e\u5c31\u53ef\u80fd\u51fa\u73b0\u4e71\u7801\u3002<\/p>\n<p><strong>\u573a\u666f\u4e09\uff1a\u65e5\u5fd7\u8f93\u51fa\u4e71\u7801<\/strong><\/p>\n<p>\u7a0b\u5e8f\u8f93\u51fa\u7684\u65e5\u5fd7\uff0c\u5982\u679c\u63a7\u5236\u53f0\u7684\u7f16\u7801\u8bbe\u7f6e\u4e0d\u5bf9\uff0c\u6216\u8005\u65e5\u5fd7\u6587\u4ef6\u7684\u7f16\u7801\u4e0d\u5bf9\uff0c\u4e2d\u6587\u65e5\u5fd7\u5c31\u4f1a\u663e\u793a\u6210\u4e71\u7801\u3002<\/p>\n<p><strong>\u573a\u666f\u56db\uff1a\u7f51\u7edc\u4f20\u8f93\u4e71\u7801<\/strong><\/p>\n<p>\u4e0d\u540c\u7cfb\u7edf\u4e4b\u95f4\u901a\u8fc7\u7f51\u7edc\u4f20\u8f93\u6570\u636e\u65f6\uff0c\u5982\u679c\u7f16\u7801\u4e0d\u4e00\u81f4\uff0c\u63a5\u6536\u5230\u7684\u6570\u636e\u53ef\u80fd\u5c31\u662f\u4e71\u7801\u3002<\/p>\n<h2>\u89e3\u51b3\u65b9\u6848\u4e00\uff1a\u7edf\u4e00\u4f7f\u7528 UTF-8<\/h2>\n<p>\u89e3\u51b3\u5b57\u7b26\u7f16\u7801\u95ee\u9898\u6700\u6839\u672c\u7684\u65b9\u6cd5\uff0c\u5c31\u662f\u7edf\u4e00\u4f7f\u7528 UTF-8 \u7f16\u7801\u3002UTF-8 \u662f\u76ee\u524d\u6700\u6d41\u884c\u7684\u5b57\u7b26\u7f16\u7801\uff0c\u51e0\u4e4e\u6240\u6709\u7684\u73b0\u4ee3\u7cfb\u7edf\u90fd\u652f\u6301\uff0c\u800c\u4e14\u53ef\u4ee5\u8868\u793a\u4e16\u754c\u4e0a\u6240\u6709\u7684\u5b57\u7b26\u3002<\/p>\n<h3>\u4e3a\u4ec0\u4e48\u9009\u62e9 UTF-8<\/h3>\n<p>UTF-8 \u6709\u5f88\u591a\u4f18\u70b9\uff1a<\/p>\n<ol>\n<li><strong>\u517c\u5bb9\u6027\u597d<\/strong>\uff1a\u51e0\u4e4e\u6240\u6709\u7684\u7cfb\u7edf\u3001\u6d4f\u89c8\u5668\u3001\u6570\u636e\u5e93\u90fd\u652f\u6301 UTF-8<\/li>\n<li><strong>\u56fd\u9645\u5316\u652f\u6301<\/strong>\uff1a\u53ef\u4ee5\u8868\u793a\u4e16\u754c\u4e0a\u6240\u6709\u7684\u5b57\u7b26\uff0c\u5305\u62ec\u4e2d\u6587\u3001\u65e5\u6587\u3001\u97e9\u6587\u3001\u963f\u62c9\u4f2f\u6587\u7b49<\/li>\n<li><strong>\u5411\u540e\u517c\u5bb9<\/strong>\uff1a\u5bf9\u4e8e ASCII \u5b57\u7b26\uff0cUTF-8 \u7f16\u7801\u548c ASCII \u7f16\u7801\u5b8c\u5168\u4e00\u6837<\/li>\n<li><strong>\u53d8\u957f\u7f16\u7801<\/strong>\uff1a\u82f1\u6587\u5b57\u7b26\u53ea\u5360 1 \u4e2a\u5b57\u8282\uff0c\u4e2d\u6587\u5b57\u7b26\u5360 3 \u4e2a\u5b57\u8282\uff0c\u6bd4\u8f83\u8282\u7701\u7a7a\u95f4<\/li>\n<\/ol>\n<h3>\u5982\u4f55\u5728\u9879\u76ee\u4e2d\u7edf\u4e00\u4f7f\u7528 UTF-8<\/h3>\n<p><strong>\u4ee3\u7801\u6587\u4ef6\u7f16\u7801\uff1a<\/strong><\/p>\n<p>\u786e\u4fdd\u6240\u6709\u7684\u6e90\u4ee3\u7801\u6587\u4ef6\u90fd\u4f7f\u7528 UTF-8 \u7f16\u7801\u4fdd\u5b58\u3002\u5728 IDE \u4e2d\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u9ed8\u8ba4\u7684\u6587\u4ef6\u7f16\u7801\uff1a<\/p>\n<ul>\n<li><strong>IntelliJ IDEA<\/strong>\uff1aFile \u2192 Settings \u2192 Editor \u2192 File Encodings\uff0c\u5c06 Project Encoding \u548c Default encoding for properties files \u90fd\u8bbe\u7f6e\u4e3a UTF-8<\/li>\n<li><strong>Eclipse<\/strong>\uff1aWindow \u2192 Preferences \u2192 General \u2192 Workspace\uff0c\u5c06 Text file encoding \u8bbe\u7f6e\u4e3a UTF-8<\/li>\n<li><strong>VS Code<\/strong>\uff1a\u5728\u8bbe\u7f6e\u4e2d\u641c\u7d22 &#8220;files.encoding&#8221;\uff0c\u8bbe\u7f6e\u4e3a UTF-8<\/li>\n<\/ul>\n<p><strong>\u914d\u7f6e\u6587\u4ef6\u7f16\u7801\uff1a<\/strong><\/p>\n<p>\u914d\u7f6e\u6587\u4ef6\uff08\u5982 properties\u3001xml\u3001json \u7b49\uff09\u4e5f\u8981\u4f7f\u7528 UTF-8 \u7f16\u7801\u3002\u7279\u522b\u662f properties \u6587\u4ef6\uff0c\u5982\u679c\u5305\u542b\u4e2d\u6587\uff0c\u5fc5\u987b\u4f7f\u7528 UTF-8 \u7f16\u7801\uff0c\u5426\u5219\u4f1a\u51fa\u73b0\u4e71\u7801\u3002<\/p>\n<p><strong>\u8d44\u6e90\u6587\u4ef6\u7f16\u7801\uff1a<\/strong><\/p>\n<p>\u8d44\u6e90\u6587\u4ef6\uff08\u5982\u56fd\u9645\u5316\u6587\u4ef6\u3001\u6a21\u677f\u6587\u4ef6\u7b49\uff09\u4e5f\u8981\u4f7f\u7528 UTF-8 \u7f16\u7801\u3002<\/p>\n<h2>\u89e3\u51b3\u65b9\u6848\u4e8c\uff1a\u8bbe\u7f6e JVM \u53c2\u6570<\/h2>\n<p>\u5728 Java \u9879\u76ee\u4e2d\uff0cJVM \u7684\u9ed8\u8ba4\u5b57\u7b26\u7f16\u7801\u53ef\u80fd\u4f1a\u5f71\u54cd\u7a0b\u5e8f\u7684\u8fd0\u884c\u3002\u5982\u679c JVM \u7684\u9ed8\u8ba4\u7f16\u7801\u4e0d\u662f UTF-8\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6587\u4ef6\u8bfb\u5199\u3001\u7f51\u7edc\u4f20\u8f93\u7b49\u64cd\u4f5c\u51fa\u73b0\u4e71\u7801\u3002<\/p>\n<h3>\u8bbe\u7f6e JVM \u53c2\u6570<\/h3>\n<p>\u5728\u542f\u52a8 Java \u7a0b\u5e8f\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7 JVM \u53c2\u6570\u6765\u8bbe\u7f6e\u5b57\u7b26\u7f16\u7801\uff1a<\/p>\n<pre><code class=\"bash\">java -Dfile.encoding=UTF-8 -jar your-app.jar<\/code><\/pre>\n<p>\u8fd9\u4e2a\u53c2\u6570\u4f1a\u544a\u8bc9 JVM\uff0c\u6587\u4ef6\u7cfb\u7edf\u7684\u9ed8\u8ba4\u7f16\u7801\u662f UTF-8\u3002<\/p>\n<h3>\u5728 IDE \u4e2d\u8bbe\u7f6e<\/h3>\n<p>\u5982\u679c\u4f60\u5728 IDE \u4e2d\u8fd0\u884c\u7a0b\u5e8f\uff0c\u4e5f\u9700\u8981\u8bbe\u7f6e JVM \u53c2\u6570\uff1a<\/p>\n<p><strong>IntelliJ IDEA\uff1a<\/strong><\/p>\n<ol>\n<li>\u70b9\u51fb Run \u2192 Edit Configurations<\/li>\n<li>\u9009\u62e9\u4f60\u7684\u8fd0\u884c\u914d\u7f6e<\/li>\n<li>\u5728 VM options \u4e2d\u8f93\u5165\uff1a<code>-Dfile.encoding=UTF-8<\/code><\/li>\n<li>\u70b9\u51fb Apply \u548c OK<\/li>\n<\/ol>\n<p><strong>Eclipse\uff1a<\/strong><\/p>\n<ol>\n<li>\u53f3\u952e\u9879\u76ee \u2192 Run As \u2192 Run Configurations<\/li>\n<li>\u9009\u62e9\u4f60\u7684\u8fd0\u884c\u914d\u7f6e<\/li>\n<li>\u5728 Arguments \u6807\u7b7e\u9875\u7684 VM arguments \u4e2d\u8f93\u5165\uff1a<code>-Dfile.encoding=UTF-8<\/code><\/li>\n<li>\u70b9\u51fb Apply \u548c Run<\/li>\n<\/ol>\n<h3>\u5728\u4ee3\u7801\u4e2d\u8bbe\u7f6e<\/h3>\n<p>\u9664\u4e86 JVM \u53c2\u6570\uff0c\u4e5f\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u8bbe\u7f6e\u7cfb\u7edf\u5c5e\u6027\uff1a<\/p>\n<pre><code class=\"java\">System.setProperty(&quot;file.encoding&quot;, &quot;UTF-8&quot;);<\/code><\/pre>\n<p>\u4f46\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u63a8\u8350\uff0c\u56e0\u4e3a\u6709\u4e9b\u4ee3\u7801\u53ef\u80fd\u5728\u8bbe\u7f6e\u4e4b\u524d\u5c31\u5df2\u7ecf\u8bfb\u53d6\u4e86\u7cfb\u7edf\u5c5e\u6027\u3002<\/p>\n<h3>\u9a8c\u8bc1\u7f16\u7801\u8bbe\u7f6e<\/h3>\n<p>\u53ef\u4ee5\u901a\u8fc7\u4ee3\u7801\u6765\u9a8c\u8bc1\u5f53\u524d\u7684\u7f16\u7801\u8bbe\u7f6e\uff1a<\/p>\n<pre><code class=\"java\">System.out.println(&quot;\u9ed8\u8ba4\u5b57\u7b26\u7f16\u7801: &quot; + System.getProperty(&quot;file.encoding&quot;));\nSystem.out.println(&quot;\u63a7\u5236\u53f0\u7f16\u7801: &quot; + System.getProperty(&quot;console.encoding&quot;));<\/code><\/pre>\n<p>\u5982\u679c\u8f93\u51fa\u4e0d\u662f UTF-8\uff0c\u8bf4\u660e\u8bbe\u7f6e\u6ca1\u6709\u751f\u6548\u3002<\/p>\n<h2>\u89e3\u51b3\u65b9\u6848\u4e09\uff1a\u6570\u636e\u5e93\u5b57\u7b26\u96c6\u8bbe\u7f6e<\/h2>\n<p>\u6570\u636e\u5e93\u7684\u5b57\u7b26\u96c6\u8bbe\u7f6e\u4e5f\u5f88\u91cd\u8981\uff0c\u5982\u679c\u6570\u636e\u5e93\u7684\u5b57\u7b26\u96c6\u4e0d\u5bf9\uff0c\u5b58\u50a8\u548c\u8bfb\u53d6\u7684\u6570\u636e\u5c31\u53ef\u80fd\u51fa\u73b0\u4e71\u7801\u3002<\/p>\n<h3>MySQL \u5b57\u7b26\u96c6\u8bbe\u7f6e<\/h3>\n<p><strong>\u521b\u5efa\u6570\u636e\u5e93\u65f6\u8bbe\u7f6e\u5b57\u7b26\u96c6\uff1a<\/strong><\/p>\n<pre><code class=\"sql\">CREATE DATABASE mydb \nDEFAULT CHARACTER SET utf8mb4 \nDEFAULT COLLATE utf8mb4_unicode_ci;<\/code><\/pre>\n<p><code>utf8mb4<\/code> \u662f MySQL \u4e2d\u771f\u6b63\u7684 UTF-8 \u7f16\u7801\uff0c\u53ef\u4ee5\u5b58\u50a8\u6240\u6709\u7684 Unicode \u5b57\u7b26\uff0c\u5305\u62ec emoji \u8868\u60c5\u3002\u800c MySQL \u7684 <code>utf8<\/code> \u5b9e\u9645\u4e0a\u53ea\u652f\u6301\u6700\u591a 3 \u5b57\u8282\u7684\u5b57\u7b26\uff0c\u4e0d\u652f\u6301 emoji\u3002<\/p>\n<p><strong>\u521b\u5efa\u8868\u65f6\u8bbe\u7f6e\u5b57\u7b26\u96c6\uff1a<\/strong><\/p>\n<pre><code class=\"sql\">CREATE TABLE users (\n    id INT PRIMARY KEY,\n    name VARCHAR(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci\n) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;<\/code><\/pre>\n<p><strong>\u4fee\u6539\u73b0\u6709\u6570\u636e\u5e93\u5b57\u7b26\u96c6\uff1a<\/strong><\/p>\n<p>\u5982\u679c\u6570\u636e\u5e93\u5df2\u7ecf\u521b\u5efa\uff0c\u53ef\u4ee5\u4fee\u6539\u5b57\u7b26\u96c6\uff1a<\/p>\n<pre><code class=\"sql\">ALTER DATABASE mydb CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;\nALTER TABLE users CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;<\/code><\/pre>\n<h3>\u8fde\u63a5\u6570\u636e\u5e93\u65f6\u8bbe\u7f6e\u5b57\u7b26\u96c6<\/h3>\n<p>\u5728\u8fde\u63a5\u6570\u636e\u5e93\u65f6\uff0c\u4e5f\u8981\u6307\u5b9a\u5b57\u7b26\u96c6\uff1a<\/p>\n<p><strong>JDBC \u8fde\u63a5\u5b57\u7b26\u4e32\uff1a<\/strong><\/p>\n<pre><code class=\"java\">String url = &quot;jdbc:mysql:\/\/localhost:3306\/mydb?useUnicode=true&amp;characterEncoding=utf8&amp;useSSL=false&quot;;<\/code><\/pre>\n<p>\u6216\u8005\u4f7f\u7528\u66f4\u5b8c\u6574\u7684\u53c2\u6570\uff1a<\/p>\n<pre><code class=\"java\">String url = &quot;jdbc:mysql:\/\/localhost:3306\/mydb?useUnicode=true&amp;characterEncoding=utf8mb4&amp;useSSL=false&amp;serverTimezone=UTC&quot;;<\/code><\/pre>\n<p><strong>Spring Boot \u914d\u7f6e\uff1a<\/strong><\/p>\n<p>\u5728 <code>application.properties<\/code> \u6216 <code>application.yml<\/code> \u4e2d\u914d\u7f6e\uff1a<\/p>\n<pre><code class=\"properties\">spring.datasource.url=jdbc:mysql:\/\/localhost:3306\/mydb?useUnicode=true&amp;characterEncoding=utf8mb4&amp;useSSL=false\nspring.datasource.username=root\nspring.datasource.password=password<\/code><\/pre>\n<p>\u6216\u8005\uff1a<\/p>\n<pre><code class=\"yaml\">spring:\n  datasource:\n    url: jdbc:mysql:\/\/localhost:3306\/mydb?useUnicode=true&amp;characterEncoding=utf8mb4&amp;useSSL=false\n    username: root\n    password: password<\/code><\/pre>\n<h3>\u5176\u4ed6\u6570\u636e\u5e93\u7684\u5b57\u7b26\u96c6\u8bbe\u7f6e<\/h3>\n<p><strong>PostgreSQL\uff1a<\/strong><\/p>\n<p>PostgreSQL \u9ed8\u8ba4\u4f7f\u7528 UTF-8 \u7f16\u7801\uff0c\u521b\u5efa\u6570\u636e\u5e93\u65f6\uff1a<\/p>\n<pre><code class=\"sql\">CREATE DATABASE mydb WITH ENCODING 'UTF8';<\/code><\/pre>\n<p><strong>Oracle\uff1a<\/strong><\/p>\n<p>Oracle \u6570\u636e\u5e93\u7684\u5b57\u7b26\u96c6\u5728\u521b\u5efa\u6570\u636e\u5e93\u65f6\u8bbe\u7f6e\uff0c\u4e4b\u540e\u5f88\u96be\u4fee\u6539\u3002\u5efa\u8bae\u5728\u521b\u5efa\u6570\u636e\u5e93\u65f6\u5c31\u9009\u62e9 UTF-8 \u76f8\u5173\u7684\u5b57\u7b26\u96c6\uff0c\u5982 <code>AL32UTF8<\/code>\u3002<\/p>\n<h2>\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/h2>\n<p>\u8ba9\u6211\u4eec\u770b\u770b\u51e0\u4e2a\u5b9e\u9645\u5e94\u7528\u573a\u666f\uff0c\u4e86\u89e3\u5982\u4f55\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u5e94\u7528\u8fd9\u4e9b\u89e3\u51b3\u65b9\u6848\uff1a<\/p>\n<h3>\u573a\u666f\u4e00\uff1aWeb \u5e94\u7528\u5f00\u53d1<\/h3>\n<p>\u5728 Web \u5e94\u7528\u4e2d\uff0c\u9700\u8981\u5904\u7406\u524d\u7aef\u7684\u8bf7\u6c42\u548c\u54cd\u5e94\uff1a<\/p>\n<p><strong>\u8bbe\u7f6e\u8bf7\u6c42\u548c\u54cd\u5e94\u7f16\u7801\uff1a<\/strong><\/p>\n<p>\u5728 Spring MVC \u4e2d\uff0c\u53ef\u4ee5\u914d\u7f6e\u5b57\u7b26\u7f16\u7801\u8fc7\u6ee4\u5668\uff1a<\/p>\n<pre><code class=\"java\">@Configuration\npublic class WebConfig implements WebMvcConfigurer {\n    @Bean\n    public CharacterEncodingFilter characterEncodingFilter() {\n        CharacterEncodingFilter filter = new CharacterEncodingFilter();\n        filter.setEncoding(&quot;UTF-8&quot;);\n        filter.setForceEncoding(true);\n        return filter;\n    }\n}<\/code><\/pre>\n<p>\u6216\u8005\u5728 <code>web.xml<\/code> \u4e2d\u914d\u7f6e\uff1a<\/p>\n<pre><code class=\"xml\">&lt;filter&gt;\n    &lt;filter-name&gt;characterEncodingFilter&lt;\/filter-name&gt;\n    &lt;filter-class&gt;org.springframework.web.filter.CharacterEncodingFilter&lt;\/filter-class&gt;\n    &lt;init-param&gt;\n        &lt;param-name&gt;encoding&lt;\/param-name&gt;\n        &lt;param-value&gt;UTF-8&lt;\/param-value&gt;\n    &lt;\/init-param&gt;\n    &lt;init-param&gt;\n        &lt;param-name&gt;forceEncoding&lt;\/param-name&gt;\n        &lt;param-value&gt;true&lt;\/param-value&gt;\n    &lt;\/init-param&gt;\n&lt;\/filter&gt;<\/code><\/pre>\n<h3>\u573a\u666f\u4e8c\uff1a\u6587\u4ef6\u8bfb\u5199<\/h3>\n<p>\u5728\u8bfb\u5199\u6587\u4ef6\u65f6\uff0c\u8981\u660e\u786e\u6307\u5b9a\u7f16\u7801\uff1a<\/p>\n<p><strong>\u8bfb\u53d6\u6587\u4ef6\uff1a<\/strong><\/p>\n<pre><code class=\"java\">\/\/ \u4f7f\u7528 UTF-8 \u7f16\u7801\u8bfb\u53d6\u6587\u4ef6\ntry (BufferedReader reader = new BufferedReader(\n        new InputStreamReader(new FileInputStream(&quot;file.txt&quot;), StandardCharsets.UTF_8))) {\n    String line;\n    while ((line = reader.readLine()) != null) {\n        System.out.println(line);\n    }\n}<\/code><\/pre>\n<p><strong>\u5199\u5165\u6587\u4ef6\uff1a<\/strong><\/p>\n<pre><code class=\"java\">\/\/ \u4f7f\u7528 UTF-8 \u7f16\u7801\u5199\u5165\u6587\u4ef6\ntry (BufferedWriter writer = new BufferedWriter(\n        new OutputStreamWriter(new FileOutputStream(&quot;file.txt&quot;), StandardCharsets.UTF_8))) {\n    writer.write(&quot;\u4e2d\u6587\u5185\u5bb9&quot;);\n}<\/code><\/pre>\n<h3>\u573a\u666f\u4e09\uff1a\u65e5\u5fd7\u8f93\u51fa<\/h3>\n<p>\u5728\u8f93\u51fa\u65e5\u5fd7\u65f6\uff0c\u8981\u786e\u4fdd\u65e5\u5fd7\u6587\u4ef6\u7684\u7f16\u7801\u6b63\u786e\uff1a<\/p>\n<p><strong>Logback \u914d\u7f6e\uff1a<\/strong><\/p>\n<pre><code class=\"xml\">&lt;configuration&gt;\n    &lt;appender name=&quot;FILE&quot; class=&quot;ch.qos.logback.core.FileAppender&quot;&gt;\n        &lt;file&gt;app.log&lt;\/file&gt;\n        &lt;encoder&gt;\n            &lt;charset&gt;UTF-8&lt;\/charset&gt;\n            &lt;pattern&gt;%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n&lt;\/pattern&gt;\n        &lt;\/encoder&gt;\n    &lt;\/appender&gt;\n&lt;\/configuration&gt;<\/code><\/pre>\n<p><strong>Log4j2 \u914d\u7f6e\uff1a<\/strong><\/p>\n<pre><code class=\"xml\">&lt;Configuration&gt;\n    &lt;Appenders&gt;\n        &lt;File name=&quot;File&quot; fileName=&quot;app.log&quot;&gt;\n            &lt;PatternLayout pattern=&quot;%d{yyyy-MM-dd HH:mm:ss} [%t] %-5level %logger{36} - %msg%n&quot; charset=&quot;UTF-8&quot;\/&gt;\n        &lt;\/File&gt;\n    &lt;\/Appenders&gt;\n&lt;\/Configuration&gt;<\/code><\/pre>\n<h2>\u6700\u4f73\u5b9e\u8df5\u5efa\u8bae<\/h2>\n<p>\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u4e3a\u4e86\u907f\u514d\u5b57\u7b26\u7f16\u7801\u95ee\u9898\uff0c\u5efa\u8bae\u9075\u5faa\u4ee5\u4e0b\u6700\u4f73\u5b9e\u8df5\uff1a<\/p>\n<h3>\u7edf\u4e00\u7f16\u7801\u89c4\u8303<\/h3>\n<ol>\n<li><strong>\u6240\u6709\u4ee3\u7801\u6587\u4ef6\u4f7f\u7528 UTF-8 \u7f16\u7801<\/strong>\uff1a\u5305\u62ec Java \u6e90\u6587\u4ef6\u3001\u914d\u7f6e\u6587\u4ef6\u3001\u8d44\u6e90\u6587\u4ef6\u7b49<\/li>\n<li><strong>\u6240\u6709\u6570\u636e\u5e93\u4f7f\u7528 UTF-8 \u5b57\u7b26\u96c6<\/strong>\uff1aMySQL \u4f7f\u7528 utf8mb4\uff0cPostgreSQL \u4f7f\u7528 UTF8<\/li>\n<li><strong>\u6240\u6709\u7f51\u7edc\u4f20\u8f93\u4f7f\u7528 UTF-8 \u7f16\u7801<\/strong>\uff1aHTTP \u8bf7\u6c42\u548c\u54cd\u5e94\u3001\u6d88\u606f\u961f\u5217\u7b49<\/li>\n<li><strong>\u6240\u6709\u65e5\u5fd7\u6587\u4ef6\u4f7f\u7528 UTF-8 \u7f16\u7801<\/strong>\uff1a\u786e\u4fdd\u65e5\u5fd7\u4e2d\u7684\u4e2d\u6587\u80fd\u6b63\u5e38\u663e\u793a<\/li>\n<\/ol>\n<h3>\u660e\u786e\u6307\u5b9a\u7f16\u7801<\/h3>\n<p>\u5728\u4ee3\u7801\u4e2d\uff0c\u51e1\u662f\u6d89\u53ca\u5b57\u7b26\u7f16\u7801\u7684\u5730\u65b9\uff0c\u90fd\u8981\u660e\u786e\u6307\u5b9a UTF-8\uff0c\u4e0d\u8981\u4f9d\u8d56\u7cfb\u7edf\u9ed8\u8ba4\u7f16\u7801\uff1a<\/p>\n<pre><code class=\"java\">\/\/ \u597d\u7684\u505a\u6cd5\uff1a\u660e\u786e\u6307\u5b9a\u7f16\u7801\nString content = new String(bytes, StandardCharsets.UTF_8);\n\n\/\/ \u4e0d\u597d\u7684\u505a\u6cd5\uff1a\u4f9d\u8d56\u9ed8\u8ba4\u7f16\u7801\nString content = new String(bytes);  \/\/ \u53ef\u80fd\u5728\u4e0d\u540c\u73af\u5883\u4e0b\u8868\u73b0\u4e0d\u4e00\u81f4<\/code><\/pre>\n<h3>\u9a8c\u8bc1\u7f16\u7801\u8bbe\u7f6e<\/h3>\n<p>\u5728\u9879\u76ee\u542f\u52a8\u65f6\uff0c\u53ef\u4ee5\u8f93\u51fa\u5f53\u524d\u7684\u7f16\u7801\u8bbe\u7f6e\uff0c\u65b9\u4fbf\u6392\u67e5\u95ee\u9898\uff1a<\/p>\n<pre><code class=\"java\">@PostConstruct\npublic void checkEncoding() {\n    System.out.println(&quot;file.encoding: &quot; + System.getProperty(&quot;file.encoding&quot;));\n    System.out.println(&quot;Default Charset: &quot; + Charset.defaultCharset());\n}<\/code><\/pre>\n<h3>\u6d4b\u8bd5\u4e0d\u540c\u73af\u5883<\/h3>\n<p>\u5728\u90e8\u7f72\u5230\u4e0d\u540c\u73af\u5883\u4e4b\u524d\uff0c\u8981\u6d4b\u8bd5\u5b57\u7b26\u7f16\u7801\u662f\u5426\u6b63\u5e38\uff1a<\/p>\n<ol>\n<li>\u5728 Windows \u5f00\u53d1\u73af\u5883\u6d4b\u8bd5<\/li>\n<li>\u5728 Linux \u6d4b\u8bd5\u73af\u5883\u6d4b\u8bd5<\/li>\n<li>\u5728 Linux \u751f\u4ea7\u73af\u5883\u6d4b\u8bd5<\/li>\n<\/ol>\n<p>\u786e\u4fdd\u5728\u6240\u6709\u73af\u5883\u4e0b\uff0c\u4e2d\u6587\u5b57\u7b26\u90fd\u80fd\u6b63\u5e38\u663e\u793a\u3002<\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u5b57\u7b26\u7f16\u7801\u95ee\u9898\u867d\u7136\u770b\u8d77\u6765\u7b80\u5355\uff0c\u4f46\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u5374\u7ecf\u5e38\u9047\u5230\u3002\u89e3\u51b3\u8fd9\u7c7b\u95ee\u9898\u7684\u5173\u952e\u662f\u7edf\u4e00\u4f7f\u7528 UTF-8 \u7f16\u7801\uff0c\u5e76\u5728\u6240\u6709\u53ef\u80fd\u6d89\u53ca\u7f16\u7801\u7684\u5730\u65b9\u660e\u786e\u6307\u5b9a\u3002<\/p>\n<p><strong>\u5173\u952e\u70b9\u603b\u7ed3\uff1a<\/strong><\/p>\n<ol>\n<li><strong>\u7edf\u4e00\u4f7f\u7528 UTF-8<\/strong>\uff1a\u6240\u6709\u6587\u4ef6\u3001\u6570\u636e\u5e93\u3001\u7f51\u7edc\u4f20\u8f93\u90fd\u4f7f\u7528 UTF-8 \u7f16\u7801<\/li>\n<li><strong>\u8bbe\u7f6e JVM \u53c2\u6570<\/strong>\uff1a\u901a\u8fc7 <code>-Dfile.encoding=UTF-8<\/code> \u8bbe\u7f6e JVM \u9ed8\u8ba4\u7f16\u7801<\/li>\n<li><strong>\u6570\u636e\u5e93\u5b57\u7b26\u96c6<\/strong>\uff1a\u6570\u636e\u5e93\u548c\u8868\u90fd\u4f7f\u7528 UTF-8 \u5b57\u7b26\u96c6\uff0c\u8fde\u63a5\u65f6\u4e5f\u8981\u6307\u5b9a\u5b57\u7b26\u96c6<\/li>\n<li><strong>\u660e\u786e\u6307\u5b9a\u7f16\u7801<\/strong>\uff1a\u5728\u4ee3\u7801\u4e2d\uff0c\u51e1\u662f\u6d89\u53ca\u7f16\u7801\u7684\u5730\u65b9\u90fd\u8981\u660e\u786e\u6307\u5b9a\uff0c\u4e0d\u8981\u4f9d\u8d56\u9ed8\u8ba4\u503c<\/li>\n<li><strong>\u9a8c\u8bc1\u548c\u6d4b\u8bd5<\/strong>\uff1a\u5728\u4e0d\u540c\u73af\u5883\u4e0b\u6d4b\u8bd5\uff0c\u786e\u4fdd\u7f16\u7801\u8bbe\u7f6e\u6b63\u786e<\/li>\n<\/ol>\n<p><strong>\u6700\u4f73\u5b9e\u8df5\uff1a<\/strong><\/p>\n<ol>\n<li>\u9879\u76ee\u5f00\u59cb\u65f6\u5c31\u7edf\u4e00\u7f16\u7801\u89c4\u8303<\/li>\n<li>\u5728 IDE \u4e2d\u8bbe\u7f6e\u9ed8\u8ba4\u7f16\u7801\u4e3a UTF-8<\/li>\n<li>\u5728\u6784\u5efa\u811a\u672c\u4e2d\u8bbe\u7f6e JVM \u53c2\u6570<\/li>\n<li>\u6570\u636e\u5e93\u521b\u5efa\u65f6\u5c31\u4f7f\u7528 UTF-8 \u5b57\u7b26\u96c6<\/li>\n<li>\u4ee3\u7801\u4e2d\u660e\u786e\u6307\u5b9a\u7f16\u7801\uff0c\u4e0d\u8981\u4f9d\u8d56\u9ed8\u8ba4\u503c<\/li>\n<\/ol>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000047570297\">https:\/\/segmentfault.com\/a\/1190000047570297<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00 \u6700\u8fd1\u5728\u505a\u4e00\u4e2a Java \u9879\u76ee\u7684\u65f6\u5019\uff0c\u9047\u5230\u4e86\u4e00\u4e2a\u8ba9\u4eba\u5934\u75bc\u7684\u95ee\u9898\uff1a\u5728 Windows \u4e0a\u5f00\u53d1\u7684\u65f6\u5019\uff0c\u4e2d\u6587\u5b57\u7b26 [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10021208","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021208","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10021208"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021208\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10021208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10021208"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10021208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}