{"id":1419,"date":"2024-08-04T10:48:49","date_gmt":"2024-08-04T02:48:49","guid":{"rendered":"https:\/\/blog.nonot.cn\/?p=1419"},"modified":"2024-08-04T10:48:49","modified_gmt":"2024-08-04T02:48:49","slug":"java-%e4%b8%ad%e7%9a%84%e8%bf%87%e6%bb%a4%e5%99%a8%e8%a7%a3%e5%86%b3%e4%b9%b1%e7%a0%81%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/blog.nonot.cn\/index.php\/2024\/08\/04\/java-%e4%b8%ad%e7%9a%84%e8%bf%87%e6%bb%a4%e5%99%a8%e8%a7%a3%e5%86%b3%e4%b9%b1%e7%a0%81%e9%97%ae%e9%a2%98\/","title":{"rendered":"Java \u4e2d\u7684\u8fc7\u6ee4\u5668\u89e3\u51b3\u4e71\u7801\u95ee\u9898"},"content":{"rendered":"<p>Java \u4e2d\u7684\u8fc7\u6ee4\u5668\u89e3\u51b3\u4e71\u7801\u95ee\u9898<br \/>\n\u5728 Java \u7f16\u7a0b\u4e2d\uff0c\u4e71\u7801\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u95ee\u9898\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u65f6\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u5b66\u4e60\u5982\u4f55\u4f7f\u7528\u8fc7\u6ee4\u5668\u6765\u89e3\u51b3 Java \u4e2d\u7684\u4e71\u7801\u95ee\u9898\u3002<\/p>\n<p>\u4ec0\u4e48\u662f\u4e71\u7801\uff1f<br \/>\n\u4e71\u7801\u662f\u6307\u5728\u5b57\u7b26\u7f16\u7801\u8fc7\u7a0b\u4e2d\uff0c\u7531\u4e8e\u7f16\u7801\u65b9\u5f0f\u4e0d\u5339\u914d\u6216\u9519\u8bef\uff0c\u5bfc\u81f4\u663e\u793a\u7684\u5b57\u7b26\u4e0e\u539f\u59cb\u5b57\u7b26\u4e0d\u4e00\u81f4\u7684\u95ee\u9898\u3002\u5728 Java \u4e2d\uff0c\u5e38\u89c1\u7684\u4e71\u7801\u95ee\u9898\u51fa\u73b0\u5728\u8bfb\u5199\u6587\u4ef6\u3001\u7f51\u7edc\u901a\u4fe1\u3001\u6570\u636e\u5e93\u64cd\u4f5c\u7b49\u573a\u666f\u4e2d\u3002<\/p>\n<p>\u4e71\u7801\u4ea7\u751f\u7684\u539f\u56e0<br \/>\n\u4e71\u7801\u95ee\u9898\u7684\u6839\u672c\u539f\u56e0\u5728\u4e8e\u5b57\u7b26\u7f16\u7801\u65b9\u5f0f\u7684\u4e0d\u4e00\u81f4\u3002\u5728 Java \u4e2d\uff0c\u5e38\u89c1\u7684\u5b57\u7b26\u96c6\u7f16\u7801\u65b9\u5f0f\u6709 ASCII\u3001UTF-8\u3001GBK \u7b49\uff0c\u5b83\u4eec\u4f7f\u7528\u4e0d\u540c\u7684\u7f16\u7801\u65b9\u5f0f\u5c06\u5b57\u7b26\u8f6c\u6362\u4e3a\u5b57\u8282\u3002<\/p>\n<p>\u4e71\u7801\u95ee\u9898\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u79cd\u539f\u56e0\u5bfc\u81f4\uff1a<\/p>\n<p>\u7f16\u7801\u65b9\u5f0f\u4e0d\u5339\u914d\uff1a\u8bfb\u53d6\u6570\u636e\u65f6\u4f7f\u7528\u4e86\u9519\u8bef\u7684\u7f16\u7801\u65b9\u5f0f\uff0c\u5bfc\u81f4\u89e3\u7801\u9519\u8bef\uff0c\u4ea7\u751f\u4e71\u7801\u3002<br \/>\n\u7f16\u7801\u65b9\u5f0f\u7f3a\u5931\uff1a\u5728\u7f51\u7edc\u4f20\u8f93\u6216\u5b58\u50a8\u6570\u636e\u65f6\uff0c\u6ca1\u6709\u660e\u786e\u6307\u5b9a\u7f16\u7801\u65b9\u5f0f\uff0c\u5bfc\u81f4\u63a5\u6536\u65b9\u65e0\u6cd5\u6b63\u786e\u89e3\u7801\uff0c\u4ea7\u751f\u4e71\u7801\u3002<br \/>\n\u5b57\u7b26\u96c6\u8f6c\u6362\u95ee\u9898\uff1a\u5728\u4e0d\u540c\u7f16\u7801\u65b9\u5f0f\u4e4b\u95f4\u8fdb\u884c\u5b57\u7b26\u96c6\u8f6c\u6362\u65f6\uff0c\u5982\u679c\u8f6c\u6362\u5931\u8d25\u6216\u4f7f\u7528\u4e86\u9519\u8bef\u7684\u8f6c\u6362\u65b9\u5f0f\uff0c\u4f1a\u5bfc\u81f4\u4e71\u7801\u3002<br \/>\n\u4f7f\u7528\u8fc7\u6ee4\u5668\u89e3\u51b3\u4e71\u7801\u95ee\u9898<br \/>\n\u8fc7\u6ee4\u5668\u662f Java Web \u5e94\u7528\u4e2d\u5e38\u7528\u7684\u4e00\u79cd\u7ec4\u4ef6\uff0c\u5b83\u53ef\u4ee5\u5bf9\u8bf7\u6c42\u548c\u54cd\u5e94\u8fdb\u884c\u9884\u5904\u7406\u548c\u540e\u5904\u7406\u3002\u5728\u5904\u7406\u4e71\u7801\u95ee\u9898\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8fc7\u6ee4\u5668\u6765\u7edf\u4e00\u8bbe\u7f6e\u8bf7\u6c42\u548c\u54cd\u5e94\u7684\u5b57\u7b26\u7f16\u7801\u65b9\u5f0f\uff0c\u786e\u4fdd\u6570\u636e\u7684\u6b63\u786e\u4f20\u8f93\u548c\u663e\u793a\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u8fc7\u6ee4\u5668\u89e3\u51b3\u4e71\u7801\u95ee\u9898\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<p>import javax.servlet.*;<br \/>\nimport javax.servlet.annotation.WebFilter;<br \/>\nimport javax.servlet.http.HttpServletRequest;<br \/>\nimport javax.servlet.http.HttpServletResponse;<br \/>\nimport java.io.IOException;<\/p>\n<p>@WebFilter(&#8220;\/*&#8221;)<br \/>\npublic class CharsetFilter implements Filter {<\/p>\n<p>private String encoding;<\/p>\n<p>@Override<br \/>\npublic void init(FilterConfig filterConfig) throws ServletException {<br \/>\nencoding = filterConfig.getInitParameter(&#8220;encoding&#8221;);<br \/>\nif (encoding == null) {<br \/>\nencoding = &#8220;UTF-8&#8221;;<br \/>\n}<br \/>\n}<\/p>\n<p>@Override<br \/>\npublic void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {<br \/>\nHttpServletRequest req = (HttpServletRequest) request;<br \/>\nHttpServletResponse resp = (HttpServletResponse) response;<\/p>\n<p>req.setCharacterEncoding(encoding);<br \/>\nresp.setCharacterEncoding(encoding);<br \/>\nresp.setContentType(&#8220;text\/html;charset=&#8221; + encoding);<\/p>\n<p>chain.doFilter(req, resp);<br \/>\n}<\/p>\n<p>@Override<br \/>\npublic void destroy() {<br \/>\n\/\/ do nothing<br \/>\n}<br \/>\n}<br \/>\n\u4ee5\u4e0a\u4ee3\u7801\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a CharsetFilter \u7684\u8fc7\u6ee4\u5668\uff0c\u5e76\u5c06\u5176\u5e94\u7528\u4e8e\u6240\u6709\u7684\u8bf7\u6c42\u548c\u54cd\u5e94\u3002\u5728\u8fc7\u6ee4\u5668\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7 FilterConfig \u5bf9\u8c61\u83b7\u53d6\u914d\u7f6e\u7684\u5b57\u7b26\u7f16\u7801\u65b9\u5f0f\uff0c\u9ed8\u8ba4\u4e3a \u201cUTF-8\u201d\u3002\u7136\u540e\uff0c\u6211\u4eec\u901a\u8fc7 setCharacterEncoding \u65b9\u6cd5\u8bbe\u7f6e\u8bf7\u6c42\u548c\u54cd\u5e94\u7684\u5b57\u7b26\u7f16\u7801\u65b9\u5f0f\u4e3a\u914d\u7f6e\u7684\u65b9\u5f0f\uff0c\u5e76\u8bbe\u7f6e\u54cd\u5e94\u7684 Content-Type \u5934\u90e8\u4e3a\u76f8\u5e94\u7684\u5b57\u7b26\u7f16\u7801\u3002<\/p>\n<p>\u901a\u8fc7\u4f7f\u7528\u8fd9\u4e2a\u8fc7\u6ee4\u5668\uff0c\u6211\u4eec\u53ef\u4ee5\u786e\u4fdd\u6240\u6709\u7684\u8bf7\u6c42\u548c\u54cd\u5e94\u90fd\u4f7f\u7528\u76f8\u540c\u7684\u5b57\u7b26\u7f16\u7801\u65b9\u5f0f\uff0c\u907f\u514d\u4e71\u7801\u95ee\u9898\u7684\u53d1\u751f\u3002<\/p>\n<p>\u5728 web.xml \u4e2d\u914d\u7f6e\u8fc7\u6ee4\u5668<br \/>\n\u4ee5\u4e0a\u793a\u4f8b\u4ee3\u7801\u4f7f\u7528\u4e86 @WebFilter \u6ce8\u89e3\u6765\u914d\u7f6e\u8fc7\u6ee4\u5668\uff0c\u8fd9\u662f\u4e00\u79cd\u57fa\u4e8e\u6ce8\u89e3\u7684\u914d\u7f6e\u65b9\u5f0f\u3002\u53e6\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u4f20\u7edf\u7684 web.xml \u6587\u4ef6\u6765\u914d\u7f6e\u8fc7\u6ee4\u5668\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u5728 web.xml \u4e2d\u914d\u7f6e\u8fc7\u6ee4\u5668\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<p>&lt;filter&gt;<br \/>\n&lt;filter-name&gt;CharsetFilter&lt;\/filter-name&gt;<br \/>\n&lt;filter-class&gt;com.example.CharsetFilter&lt;\/filter-class&gt;<br \/>\n&lt;init-param&gt;<br \/>\n&lt;param-name&gt;encoding&lt;\/param-name&gt;<br \/>\n&lt;param-value&gt;UTF-8&lt;\/param-value&gt;<br \/>\n&lt;\/init-param&gt;<br \/>\n&lt;\/filter&gt;<\/p>\n<p>&lt;filter-mapping&gt;<br \/>\n&lt;filter-name&gt;CharsetFilter&lt;\/filter-name&gt;<br \/>\n&lt;url-pattern&gt;\/*&lt;\/url-pattern&gt;<br \/>\n&lt;\/filter-mapping&gt;<br \/>\n\u5728\u4ee5\u4e0a\u793a\u4f8b\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a CharsetFilter \u7684\u8fc7\u6ee4\u5668\uff0c\u5e76\u6307\u5b9a\u4e86\u8fc7\u6ee4\u5668\u7684\u7c7b\u8def\u5f84\u3002\u7136\u540e\uff0c\u6211\u4eec\u901a\u8fc7 &lt;init-param&gt; \u5143\u7d20\u8bbe\u7f6e\u4e86\u8fc7\u6ee4\u5668\u7684\u521d\u59cb\u5316\u53c2\u6570\uff0c\u5176\u4e2d encoding \u53c2\u6570\u7528\u4e8e\u6307\u5b9a\u5b57\u7b26\u7f16\u7801\u65b9\u5f0f\u3002\u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528 &lt;filter-mapping&gt; \u5143\u7d20\u5c06\u8fc7<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java \u4e2d\u7684\u8fc7\u6ee4\u5668\u89e3\u51b3\u4e71\u7801\u95ee\u9898 \u5728 Java \u7f16\u7a0b\u4e2d\uff0c\u4e71\u7801&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-1419","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts\/1419","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1419"}],"version-history":[{"count":1,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts\/1419\/revisions"}],"predecessor-version":[{"id":1420,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts\/1419\/revisions\/1420"}],"wp:attachment":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}