{"id":39598,"date":"2024-11-26T13:55:59","date_gmt":"2024-11-26T05:55:59","guid":{"rendered":"https:\/\/fwq.ai\/blog\/39598\/"},"modified":"2024-11-26T13:55:59","modified_gmt":"2024-11-26T05:55:59","slug":"java%e4%b8%ad%e5%ad%97%e8%8a%82%e6%95%b0%e7%bb%84%e6%80%8e%e4%b9%88%e5%ad%98%e5%82%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/39598\/","title":{"rendered":"java\u4e2d\u5b57\u8282\u6570\u7ec4\u600e\u4e48\u5b58\u50a8"},"content":{"rendered":"<blockquote><p>\n  java \u4e2d\u5b58\u50a8\u5b57\u8282\u6570\u7ec4\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528 byte[] \u6570\u7ec4\uff1b\u4f7f\u7528 java.nio.bytebuffer \u7c7b\uff1b\u4f7f\u7528 java.io.bytearrayinputstream \u7c7b\uff1b\u4f7f\u7528 java.io.bytearrayoutputstream \u7c7b\uff1b\u5c06\u5b57\u8282\u6570\u7ec4\u5b58\u50a8\u5728\u6570\u636e\u5e93\u7684 blob \u7c7b\u578b\u4e2d\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/16\/2024111618162353649.jpg\" class=\"aligncenter\" title=\"java\u4e2d\u5b57\u8282\u6570\u7ec4\u600e\u4e48\u5b58\u50a8\u63d2\u56fe\" alt=\"java\u4e2d\u5b57\u8282\u6570\u7ec4\u600e\u4e48\u5b58\u50a8\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u4e2d\u5b58\u50a8\u5b57\u8282\u6570\u7ec4\u7684\u65b9\u6cd5<\/strong><\/p>\n<p>\u5b57\u8282\u6570\u7ec4\u662f\u4e00\u4e2a\u53ef\u4ee5\u5b58\u50a8\u4e8c\u8fdb\u5236\u6570\u636e\u7684\u5bb9\u5668\uff0c\u5728 Java \u4e2d\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b58\u50a8\u5b57\u8282\u6570\u7ec4\uff1a<\/p>\n<p><strong>1. byte[] \u6570\u7ec4<\/strong><\/p>\n<ul>\n<li>\u6700\u57fa\u672c\u7684\u65b9\u6cd5\u662f\u521b\u5efa\u4e00\u4e2a byte[] \u6570\u7ec4\uff0c\u5e76\u76f4\u63a5\u5c06\u5176\u5206\u914d\u7ed9\u5b57\u8282\u6570\u636e\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ul>\n<pre>byte[] byteArray = new byte[] { 1, 2, 3, 4, 5 };<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. java.nio.ByteBuffer<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ul>\n<li>ByteBuffer \u7c7b\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u9ad8\u7ea7\u7684\u65b9\u6cd5\u6765\u7ba1\u7406\u5b57\u8282\u6570\u636e\u3002\u5b83\u5141\u8bb8\u5bf9\u5b57\u8282\u6570\u7ec4\u8fdb\u884c\u8bfb\u5199\u64cd\u4f5c\uff0c\u5e76\u8ddf\u8e2a\u5f53\u524d\u8bfb\u53d6\u6216\u5199\u5165\u7684\u4f4d\u7f6e\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ul>\n<pre>ByteBuffer byteBuffer = ByteBuffer.allocate(10);\nbyteBuffer.put(new byte[] { 1, 2, 3, 4, 5 });<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. java.io.ByteArrayInputStream<\/strong><\/p>\n<ul>\n<li>ByteArrayInputStream \u7c7b\u5141\u8bb8\u4ece\u5b57\u8282\u6570\u7ec4\u4e2d\u8bfb\u53d6\u6570\u636e\uff0c\u5c31\u50cf\u4ece\u4e00\u4e2a\u8f93\u5165\u6d41\u4e2d\u8bfb\u53d6\u4e00\u6837\u3002\u8fd9\u5bf9\u4e8e\u9700\u8981\u4ece\u5b57\u8282\u6570\u7ec4\u4e2d\u8bfb\u53d6\u6570\u636e\u4f46\u4e0d\u60f3\u521b\u5efa\u6587\u4ef6\u6216\u5176\u4ed6\u8f93\u5165\u6e90\u7684\u60c5\u51b5\u5f88\u6709\u7528\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ul>\n<pre>ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(new byte[] { 1, 2, 3, 4, 5 });<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. java.io.ByteArrayOutputStream<\/strong><\/p>\n<ul>\n<li>ByteArrayOutputStream \u7c7b\u5141\u8bb8\u5c06\u6570\u636e\u5199\u5165\u5b57\u8282\u6570\u7ec4\uff0c\u5c31\u50cf\u5199\u5165\u8f93\u51fa\u6d41\u4e00\u6837\u3002\u8fd9\u5bf9\u4e8e\u9700\u8981\u5c06\u6570\u636e\u6536\u96c6\u5230\u5b57\u8282\u6570\u7ec4\u4e2d\u4f46\u4e0d\u60f3\u521b\u5efa\u6587\u4ef6\u6216\u5176\u4ed6\u8f93\u51fa\u6e90\u7684\u60c5\u51b5\u5f88\u6709\u7528\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ul>\n<pre>ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();\nbyteArrayOutputStream.write(new byte[] { 1, 2, 3, 4, 5 });<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>5. \u6570\u636e\u5e93<\/strong><\/p>\n<ul>\n<li>\u5b57\u8282\u6570\u7ec4\u4e5f\u53ef\u4ee5\u5b58\u50a8\u5728\u6570\u636e\u5e93\u4e2d\u3002\u53ef\u4ee5\u4f7f\u7528 BLOB\uff08\u4e8c\u8fdb\u5236\u5927\u5bf9\u8c61\uff09\u7c7b\u578b\u6765\u5b58\u50a8\u5b57\u8282\u6570\u7ec4\u3002\u8fd9\u5bf9\u4e8e\u9700\u8981\u957f\u671f\u5b58\u50a8\u5927\u578b\u5b57\u8282\u6570\u636e\u7684\u60c5\u51b5\u5f88\u6709\u7528\u3002<\/li>\n<\/ul>\n<p><strong>\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\uff1a<\/strong><\/p>\n<ul>\n<li>\u5bf9\u4e8e\u9700\u8981\u7b80\u5355\u3001\u9ad8\u6548\u7684\u5b58\u50a8\uff0cbyte[] \u6570\u7ec4\u6216 ByteBuffer \u901a\u5e38\u662f\u6700\u597d\u7684\u9009\u62e9\u3002<\/li>\n<li>\u5bf9\u4e8e\u9700\u8981\u66f4\u9ad8\u7ea7\u7684\u529f\u80fd\uff0c\u5982\u8bfb\u5199\u64cd\u4f5c\u6216\u5bf9\u5b57\u8282\u6570\u7ec4\u7684\u76f4\u63a5\u8bbf\u95ee\uff0cByteBuffer \u53ef\u80fd\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002<\/li>\n<li>\u5bf9\u4e8e\u9700\u8981\u5c06\u5b57\u8282\u6570\u7ec4\u4f5c\u4e3a\u8f93\u5165\u6216\u8f93\u51fa\u6d41\u4f7f\u7528\u7684\u60c5\u51b5\uff0cByteArrayInputStream \u548c ByteArrayOutputStream \u5f88\u6709\u7528\u3002<\/li>\n<li>\u5bf9\u4e8e\u9700\u8981\u5b58\u50a8\u5927\u578b\u5b57\u8282\u6570\u636e\u6216\u9700\u8981\u957f\u671f\u5b58\u50a8\u5b57\u8282\u6570\u636e\u7684\u60c5\u51b5\uff0c\u6570\u636e\u5e93\u4e2d\u7684 BLOB \u7c7b\u578b\u53ef\u80fd\u662f\u6700\u5408\u9002\u7684\u9009\u62e9\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u4e2d\u5b57\u8282\u6570\u7ec4\u600e\u4e48\u5b58\u50a8\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>java \u4e2d\u5b58\u50a8\u5b57\u8282\u6570\u7ec4\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528 byte[] \u6570\u7ec4\uff1b\u4f7f\u7528 java.nio.bytebuffer \u7c7b\uff1b\u4f7f\u7528 java.io.bytearrayinputstream \u7c7b\uff1b\u4f7f\u7528 java.io.bytearrayoutputstream \u7c7b\uff1b\u5c06\u5b57\u8282\u6570\u7ec4\u5b58\u50a8\u5728\u6570\u636e\u5e93\u7684 blob \u7c7b\u578b\u4e2d\u3002 Java \u4e2d\u5b58\u50a8\u5b57\u8282\u6570\u7ec4\u7684\u65b9\u6cd5 \u5b57\u8282\u6570\u7ec4\u662f\u4e00\u4e2a\u53ef\u4ee5\u5b58\u50a8\u4e8c\u8fdb\u5236\u6570\u636e\u7684\u5bb9\u5668\uff0c\u5728 Java \u4e2d\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b58\u50a8\u5b57\u8282\u6570\u7ec4\uff1a 1. byte[] \u6570\u7ec4 \u6700\u57fa\u672c\u7684\u65b9\u6cd5\u662f\u521b\u5efa\u4e00\u4e2a byte[] \u6570\u7ec4\uff0c\u5e76\u76f4\u63a5\u5c06\u5176\u5206\u914d\u7ed9\u5b57\u8282\u6570\u636e\u3002\u4f8b\u5982\uff1a byte[] byteArray = new byte[] { 1, 2, 3, 4, 5 }; \u767b\u5f55\u540e\u590d\u5236 2. java.nio.ByteBuffer \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b ByteBuffer \u7c7b\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u9ad8\u7ea7\u7684\u65b9\u6cd5\u6765\u7ba1\u7406\u5b57\u8282\u6570\u636e\u3002\u5b83\u5141\u8bb8\u5bf9\u5b57\u8282\u6570\u7ec4\u8fdb\u884c\u8bfb\u5199\u64cd\u4f5c\uff0c\u5e76\u8ddf\u8e2a\u5f53\u524d\u8bfb\u53d6\u6216\u5199\u5165\u7684\u4f4d\u7f6e\u3002\u4f8b\u5982\uff1a ByteBuffer byteBuffer = ByteBuffer.allocate(10); byteBuffer.put(new byte[] { 1, 2, 3, 4, 5 }); \u767b\u5f55\u540e\u590d\u5236 [&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-39598","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/39598","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=39598"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/39598\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=39598"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=39598"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=39598"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}