{"id":66189,"date":"2025-05-03T12:11:43","date_gmt":"2025-05-03T04:11:43","guid":{"rendered":"https:\/\/fwq.ai\/blog\/66189\/"},"modified":"2025-05-03T12:11:43","modified_gmt":"2025-05-03T04:11:43","slug":"java%e6%80%8e%e4%b9%88%e5%b0%86%e8%be%93%e5%85%a5%e6%b5%81%e4%bc%a0%e5%85%a5%e6%95%b0%e7%bb%84-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/66189\/","title":{"rendered":"java\u600e\u4e48\u5c06\u8f93\u5165\u6d41\u4f20\u5165\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  \u5c06\u8f93\u5165\u6d41\u4f20\u5165 java \u6570\u7ec4\u6b65\u9aa4\uff1a1. \u521b\u5efa\u4e00\u4e2a\u4e0e\u8f93\u5165\u6d41\u5927\u5c0f\u76f8\u540c\u7684\u5b57\u8282\u6570\u7ec4\u30022. \u4f7f\u7528 read() \u65b9\u6cd5\u5c06\u8f93\u5165\u6d41\u8bfb\u5165\u5b57\u8282\u6570\u7ec4\u30023. \u68c0\u67e5\u8bfb\u53d6\u7684\u5b57\u8282\u6570\uff0c\u4e3a -1 \u8868\u793a\u6d41\u5df2\u7ed3\u675f\u30024. \u5c06\u4ece\u8f93\u5165\u6d41\u8bfb\u53d6\u7684\u5b57\u8282\u590d\u5236\u5230\u6570\u7ec4\u4e2d\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/15\/2024111503244273879.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u5c06\u8f93\u5165\u6d41\u4f20\u5165\u6570\u7ec4\u63d2\u56fe\" alt=\"java\u600e\u4e48\u5c06\u8f93\u5165\u6d41\u4f20\u5165\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5c06\u8f93\u5165\u6d41\u4f20\u5165 Java \u6570\u7ec4<\/strong><\/p>\n<p>\u5c06\u8f93\u5165\u6d41\u4f20\u5165 Java \u6570\u7ec4\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<p><strong>1. \u521b\u5efa\u4e00\u4e2a\u5b57\u8282\u6570\u7ec4<\/strong><\/p>\n<p>\u521b\u5efa\u4e0e\u8f93\u5165\u6d41\u5927\u5c0f\u76f8\u540c\u7684\u5b57\u8282\u6570\u7ec4\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>byte[] buffer = new byte[inputstream.available()];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u8bfb\u5165\u8f93\u5165\u6d41<\/strong><\/p>\n<p>\u4f7f\u7528 read() \u65b9\u6cd5\u5c06\u8f93\u5165\u6d41\u8bfb\u5165\u5b57\u8282\u6570\u7ec4\u3002<\/p>\n<pre>int bytesRead = inputstream.read(buffer);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u68c0\u67e5\u8bfb\u53d6\u7684\u5b57\u8282\u6570<\/strong><\/p>\n<p>\u68c0\u67e5 bytesRead \u7684\u503c\u4ee5\u786e\u5b9a\u8bfb\u53d6\u4e86\u591a\u5c11\u5b57\u8282\u3002\u5982\u679c bytesRead \u4e3a -1\uff0c\u5219\u8868\u793a\u6d41\u5df2\u7ed3\u675f\u3002<\/p>\n<p><strong>4. \u5c06\u5b57\u8282\u590d\u5236\u5230\u6570\u7ec4\u4e2d<\/strong><\/p>\n<p>\u5c06\u4ece\u8f93\u5165\u6d41\u8bfb\u53d6\u7684\u5b57\u8282\u590d\u5236\u5230\u6570\u7ec4\u4e2d\u3002<\/p>\n<pre>for (int i = 0; i &lt; bytesRead; i++) {\n    yourArray[i] = buffer[i];\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<pre>import java.io.ByteArrayInputStream;\nimport java.io.IOException;\n\npublic class InputStreamToByteArray {\n\n    public static void main(String[] args) throws IOException {\n        \/\/ \u521b\u5efa\u4e00\u4e2a\u8f93\u5165\u6d41\n        ByteArrayInputStream inputstream = new ByteArrayInputStream(\"Hello World\".getBytes());\n\n        \/\/ \u521b\u5efa\u4e00\u4e2a\u5b57\u8282\u6570\u7ec4\n        byte[] buffer = new byte[inputstream.available()];\n\n        \/\/ \u8bfb\u5165\u8f93\u5165\u6d41\n        int bytesRead = inputstream.read(buffer);\n\n        \/\/ \u68c0\u67e5\u8bfb\u53d6\u7684\u5b57\u8282\u6570\n        if (bytesRead == -1) {\n            System.out.println(\"\u6d41\u5df2\u7ed3\u675f\");\n        }\n\n        \/\/ \u5c06\u5b57\u8282\u590d\u5236\u5230\u6570\u7ec4\u4e2d\n        byte[] yourArray = new byte[bytesRead];\n        for (int i = 0; i &lt; bytesRead; i++) {\n            yourArray[i] = buffer[i];\n        }\n\n        \/\/ \u8f6c\u6362\u6570\u7ec4\u4e3a\u5b57\u7b26\u4e32\u5e76\u6253\u5370\n        System.out.println(new String(yourArray));\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre>Hello World<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u5c06\u8f93\u5165\u6d41\u4f20\u5165\u6570\u7ec4\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8IDCBABY\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5c06\u8f93\u5165\u6d41\u4f20\u5165 java \u6570\u7ec4\u6b65\u9aa4\uff1a1. \u521b\u5efa\u4e00\u4e2a\u4e0e\u8f93\u5165\u6d41\u5927\u5c0f\u76f8\u540c\u7684\u5b57\u8282\u6570\u7ec4\u30022. \u4f7f\u7528 read() \u65b9\u6cd5\u5c06\u8f93\u5165\u6d41\u8bfb\u5165\u5b57\u8282\u6570\u7ec4\u30023. \u68c0\u67e5\u8bfb\u53d6\u7684\u5b57\u8282\u6570\uff0c\u4e3a -1 \u8868\u793a\u6d41\u5df2\u7ed3\u675f\u30024. \u5c06\u4ece\u8f93\u5165\u6d41\u8bfb\u53d6\u7684\u5b57\u8282\u590d\u5236\u5230\u6570\u7ec4\u4e2d\u3002 \u5982\u4f55\u5c06\u8f93\u5165\u6d41\u4f20\u5165 Java \u6570\u7ec4 \u5c06\u8f93\u5165\u6d41\u4f20\u5165 Java \u6570\u7ec4\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\uff1a 1. \u521b\u5efa\u4e00\u4e2a\u5b57\u8282\u6570\u7ec4 \u521b\u5efa\u4e0e\u8f93\u5165\u6d41\u5927\u5c0f\u76f8\u540c\u7684\u5b57\u8282\u6570\u7ec4\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b byte[] buffer = new byte[inputstream.available()]; \u767b\u5f55\u540e\u590d\u5236 2. \u8bfb\u5165\u8f93\u5165\u6d41 \u4f7f\u7528 read() \u65b9\u6cd5\u5c06\u8f93\u5165\u6d41\u8bfb\u5165\u5b57\u8282\u6570\u7ec4\u3002 int bytesRead = inputstream.read(buffer); \u767b\u5f55\u540e\u590d\u5236 3. \u68c0\u67e5\u8bfb\u53d6\u7684\u5b57\u8282\u6570 \u68c0\u67e5 bytesRead \u7684\u503c\u4ee5\u786e\u5b9a\u8bfb\u53d6\u4e86\u591a\u5c11\u5b57\u8282\u3002\u5982\u679c bytesRead \u4e3a -1\uff0c\u5219\u8868\u793a\u6d41\u5df2\u7ed3\u675f\u3002 4. \u5c06\u5b57\u8282\u590d\u5236\u5230\u6570\u7ec4\u4e2d \u5c06\u4ece\u8f93\u5165\u6d41\u8bfb\u53d6\u7684\u5b57\u8282\u590d\u5236\u5230\u6570\u7ec4\u4e2d\u3002 for (int i = 0; i &lt; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-66189","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66189","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=66189"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66189\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=66189"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=66189"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=66189"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}