{"id":39407,"date":"2024-11-26T15:47:49","date_gmt":"2024-11-26T07:47:49","guid":{"rendered":"https:\/\/fwq.ai\/blog\/39407\/"},"modified":"2024-11-26T15:47:49","modified_gmt":"2024-11-26T07:47:49","slug":"java%e6%95%b0%e7%bb%84%e6%80%8e%e4%b9%88%e5%ad%98%e5%88%b0%e6%95%b0%e6%8d%ae%e5%ba%93%e4%b8%ad","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/39407\/","title":{"rendered":"java\u6570\u7ec4\u600e\u4e48\u5b58\u5230\u6570\u636e\u5e93\u4e2d"},"content":{"rendered":"<blockquote><p>\n  java\u6570\u7ec4\u5b58\u50a8\u5230\u6570\u636e\u5e93\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a\u5e8f\u5217\u5316\u6570\u7ec4\uff1a\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u5b57\u8282\u6570\u7ec4\u3002\u5199\u5165\u6570\u636e\u5e93\uff1a\u521b\u5efa\u4e00\u4e2ablob\u6216bytea\u7c7b\u578b\u5b57\u6bb5\uff0c\u7136\u540e\u5199\u5165\u5e8f\u5217\u5316\u540e\u7684\u6570\u7ec4\u3002\u53cd\u5e8f\u5217\u5316\u6570\u7ec4\uff1a\u4ece\u6570\u636e\u5e93\u4e2d\u68c0\u7d22\u5e8f\u5217\u5316\u540e\u7684\u6570\u7ec4\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u539f\u59cb\u6570\u7ec4\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/16\/2024111607214527359.jpg\" class=\"aligncenter\" title=\"java\u6570\u7ec4\u600e\u4e48\u5b58\u5230\u6570\u636e\u5e93\u4e2d\u63d2\u56fe\" alt=\"java\u6570\u7ec4\u600e\u4e48\u5b58\u5230\u6570\u636e\u5e93\u4e2d\u63d2\u56fe\" \/><\/p>\n<p><strong>Java\u6570\u7ec4\u5b58\u5165\u6570\u636e\u5e93<\/strong><\/p>\n<p>\u5c06Java\u6570\u7ec4\u5b58\u50a8\u5230\u6570\u636e\u5e93\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\n<p><strong>\u5e8f\u5217\u5316\u6570\u7ec4\uff1a<\/strong><\/p>\n<ul>\n<li>\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u5b57\u8282\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528Java\u5e8f\u5217\u5316\u673a\u5236\uff08\u4f8b\u5982\u4f7f\u7528ObjectOutputStream\uff09\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u5199\u5165\u6570\u636e\u5e93\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ul>\n<li>\u521b\u5efa\u4e00\u4e2aBLOB\u6216BYTEA\u7c7b\u578b\u5b57\u6bb5\u6765\u5b58\u50a8\u5e8f\u5217\u5316\u7684\u6570\u7ec4\u3002<\/li>\n<li>\u4f7f\u7528PreparedStatement\u6216ResultSet\u7684setBlob\u6216setBytes\u65b9\u6cd5\u5c06\u5e8f\u5217\u5316\u540e\u7684\u6570\u7ec4\u5199\u5165\u6570\u636e\u5e93\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u53cd\u5e8f\u5217\u5316\u6570\u7ec4\uff1a<\/strong><\/p>\n<ul>\n<li>\u4ece\u6570\u636e\u5e93\u4e2d\u68c0\u7d22\u5e8f\u5217\u5316\u540e\u7684\u6570\u7ec4\u3002<\/li>\n<li>\u4f7f\u7528Java\u53cd\u5e8f\u5217\u5316\u673a\u5236\uff08\u4f8b\u5982\u4f7f\u7528ObjectInputStream\uff09\u5c06\u5176\u8f6c\u6362\u4e3a\u539f\u59cb\u6570\u7ec4\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<p><strong>\u5e8f\u5217\u5316\u6570\u7ec4\uff1a<\/strong><\/p>\n<pre>import java.io.ByteArrayOutputStream;\nimport java.io.ObjectOutputStream;\n\npublic class SerializeArray {\n\n    public static byte[] serializeArray(int[] array) {\n        try (ByteArrayOutputStream baos = new ByteArrayOutputStream();\n             ObjectOutputStream oos = new ObjectOutputStream(baos)) {\n            oos.writeObject(array);\n            return baos.toByteArray();\n        } catch (Exception e) {\n            e.printStackTrace();\n            return null;\n        }\n    }\n\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u5199\u5165\u6570\u636e\u5e93\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>import java.sql.Connection;\nimport java.sql.PreparedStatement;\n\npublic class WriteArrayToDB {\n\n    public static void writeToDB(Connection conn, byte[] serializedArray) {\n        try (PreparedStatement stmt = conn.prepareStatement(\"INSERT INTO table_name (array_field) VALUES (?)\")) {\n            stmt.setBytes(1, serializedArray);\n            stmt.executeUpdate();\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n    }\n\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u53cd\u5e8f\u5217\u5316\u6570\u7ec4\uff1a<\/strong><\/p>\n<pre>import java.io.ByteArrayInputStream;\nimport java.io.ObjectInputStream;\n\npublic class DeserializeArray {\n\n    public static void deserializeArray(byte[] serializedArray) {\n        try (ByteArrayInputStream bais = new ByteArrayInputStream(serializedArray);\n             ObjectInputStream ois = new ObjectInputStream(bais)) {\n            int[] array = (int[]) ois.readObject();\n            System.out.println(Arrays.toString(array));\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n    }\n\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u6570\u7ec4\u600e\u4e48\u5b58\u5230\u6570\u636e\u5e93\u4e2d\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\u6570\u7ec4\u5b58\u50a8\u5230\u6570\u636e\u5e93\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a\u5e8f\u5217\u5316\u6570\u7ec4\uff1a\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u5b57\u8282\u6570\u7ec4\u3002\u5199\u5165\u6570\u636e\u5e93\uff1a\u521b\u5efa\u4e00\u4e2ablob\u6216bytea\u7c7b\u578b\u5b57\u6bb5\uff0c\u7136\u540e\u5199\u5165\u5e8f\u5217\u5316\u540e\u7684\u6570\u7ec4\u3002\u53cd\u5e8f\u5217\u5316\u6570\u7ec4\uff1a\u4ece\u6570\u636e\u5e93\u4e2d\u68c0\u7d22\u5e8f\u5217\u5316\u540e\u7684\u6570\u7ec4\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u539f\u59cb\u6570\u7ec4\u3002 Java\u6570\u7ec4\u5b58\u5165\u6570\u636e\u5e93 \u5c06Java\u6570\u7ec4\u5b58\u50a8\u5230\u6570\u636e\u5e93\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a \u5e8f\u5217\u5316\u6570\u7ec4\uff1a \u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u5b57\u8282\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528Java\u5e8f\u5217\u5316\u673a\u5236\uff08\u4f8b\u5982\u4f7f\u7528ObjectOutputStream\uff09\u3002 \u5199\u5165\u6570\u636e\u5e93\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u521b\u5efa\u4e00\u4e2aBLOB\u6216BYTEA\u7c7b\u578b\u5b57\u6bb5\u6765\u5b58\u50a8\u5e8f\u5217\u5316\u7684\u6570\u7ec4\u3002 \u4f7f\u7528PreparedStatement\u6216ResultSet\u7684setBlob\u6216setBytes\u65b9\u6cd5\u5c06\u5e8f\u5217\u5316\u540e\u7684\u6570\u7ec4\u5199\u5165\u6570\u636e\u5e93\u3002 \u53cd\u5e8f\u5217\u5316\u6570\u7ec4\uff1a \u4ece\u6570\u636e\u5e93\u4e2d\u68c0\u7d22\u5e8f\u5217\u5316\u540e\u7684\u6570\u7ec4\u3002 \u4f7f\u7528Java\u53cd\u5e8f\u5217\u5316\u673a\u5236\uff08\u4f8b\u5982\u4f7f\u7528ObjectInputStream\uff09\u5c06\u5176\u8f6c\u6362\u4e3a\u539f\u59cb\u6570\u7ec4\u3002 \u793a\u4f8b\u4ee3\u7801\uff1a \u5e8f\u5217\u5316\u6570\u7ec4\uff1a import java.io.ByteArrayOutputStream; import java.io.ObjectOutputStream; public class SerializeArray { public static byte[] serializeArray(int[] array) { try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos)) { oos.writeObject(array); return baos.toByteArray(); } catch (Exception e) { e.printStackTrace(); return null; } } } \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-39407","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/39407","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=39407"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/39407\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=39407"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=39407"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=39407"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}