{"id":1507,"date":"2023-03-25T13:58:31","date_gmt":"2023-03-25T05:58:31","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1507"},"modified":"2023-04-28T20:22:14","modified_gmt":"2023-04-28T12:22:14","slug":"java-urlencoder-encoding-space-is-plus","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/25\/java-urlencoder-encoding-space-is-plus\/","title":{"rendered":"Java URLEncoder \u7f16\u7801\u7a7a\u683c\u4e3a\u201c+\u201d"},"content":{"rendered":"<p>\u4f7f\u7528<code>java.net.URLEncoder<\/code>\u7f16\u7801\u5b57\u7b26\u4e32\u540e\u4f1a\u5c06\u7a7a\u683c\u66ff\u6362\u4e3a<code>+<\/code>\uff0c\u5bfc\u81f4\u9a8c\u7b7e\u51fa\u9519\u6216\u524d\u7aef\u4e0d\u80fd\u8fd8\u539f\u539f\u5b57\u7b26\u4e32\u7684\u7a7a\u683c\u3002<\/p>\n<pre><code class=\"language-java\">String s = &quot;\u6587\u4ef6 -\u6587\u4ef61&quot;;\nString encode = URLEncoder.encode(s, StandardCharsets.UTF_8);<\/code><\/pre>\n<p><!-- more --><\/p>\n<p>\u7ed3\u679c\u4e3a\uff1a<code>%E6%96%87%E4%BB%B6+-%E6%96%87%E4%BB%B61<\/code>\uff0c\u7a7a\u683c\u88ab\u66ff\u6362\u4e3a<code>+<\/code>\uff0c\u800c\u4e0d\u662f<code>%20<\/code>\uff0cJS\u4f7f\u7528<code>DecodeURIComponent<\/code>\u5f97\u5230<code>\u6587\u4ef6+-\u6587\u4ef61<\/code>\u3002<\/p>\n<p>\u67e5\u770b\u6e90\u7801\u53ef\u5f97<code>java.net.URLEncoder<\/code>\u5b9e\u73b0\u7684\u662f`application\/x-www-form-urlencoded: <a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/www.w3.org\/TR\/html4\/interact\/forms.html#h-17.13.4\">https:\/\/www.w3.org\/TR\/html4\/interact\/forms.html#h-17.13.4<\/a>\u3002<\/p>\n<p>\u5904\u7406\u65b9\u5f0f\u4e3a\uff1a<\/p>\n<p>\uff081\uff09\u4f7f\u7528String\u7684<code>replace<\/code><\/p>\n<pre><code class=\"language-java\">URLEncoder.encode(&quot;Hello World&quot;, &quot;UTF-8&quot;).replace(&quot;+&quot;, &quot;%20&quot;);<\/code><\/pre>\n<p>\uff082\uff09\u4f7f\u7528\u5176\u4ed6\u7c7b\u5e93\u7684\u65b9\u6cd5\uff0c\u4f8b\u5982Spring\u7684<code>UriUtils<\/code><\/p>\n<pre><code class=\"language-java\">UriUtils.encode(fileName, StandardCharsets.UTF_8.name());<\/code><\/pre>\n<p>\u53c2\u8003\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/stackoverflow.com\/questions\/4737841\/urlencoder-not-able-to-translate-space-character\">https:\/\/stackoverflow.com\/questions\/4737841\/urlencoder-not-able-to-translate-space-character<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528java.net.URLEncoder\u7f16\u7801\u5b57\u7b26\u4e32\u540e\u4f1a\u5c06\u7a7a\u683c\u66ff\u6362\u4e3a+\uff0c\u5bfc\u81f4\u9a8c\u7b7e\u51fa\u9519\u6216\u524d\u7aef\u4e0d\u80fd\u8fd8\u539f\u539f\u5b57\u7b26\u4e32\u7684\u7a7a [&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-1507","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\/1507","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=1507"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1507\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1507"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1507"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1507"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}