{"id":97116,"date":"2024-10-20T15:08:16","date_gmt":"2024-10-20T07:08:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/97116\/"},"modified":"2024-10-20T15:08:17","modified_gmt":"2024-10-20T07:08:17","slug":"p%e9%a1%af%e7%a4%ba%e6%95%b8%e6%93%9a%e5%ba%ab%e5%9c%96%e7%89%87%e8%a9%b3%e8%a7%a3-jsp%e7%8d%b2%e5%8f%96%e6%95%b8%e6%93%9a%e5%ba%ab%e5%9c%96%e7%89%87%e4%b8%a6%e4%b8%94%e9%a1%af%e7%a4%ba","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/97116\/","title":{"rendered":"P\u986f\u793a\u6578\u64da\u5eab\u5716\u7247\u8a73\u89e3 (jsp\u7372\u53d6\u6578\u64da\u5eab\u5716\u7247\u4e26\u4e14\u986f\u793a)"},"content":{"rendered":"<h1 id=\"p%e9%a1%af%e7%a4%ba%e6%95%b8%e6%93%9a%e5%ba%ab%e5%9c%96%e7%89%87%e8%a9%b3%e8%a7%a3-jsp%e7%8d%b2%e5%8f%96%e6%95%b8%e6%93%9a%e5%ba%ab%e5%9c%96%e7%89%87%e4%b8%a6%e4%b8%94%e9%a1%af%e7%a4%ba-tOeGXBxORn\">P\u986f\u793a\u6578\u64da\u5eab\u5716\u7247\u8a73\u89e3 (jsp\u7372\u53d6\u6578\u64da\u5eab\u5716\u7247\u4e26\u4e14\u986f\u793a)<\/h1>\n<p>\u5728\u73fe\u4eca\u7684\u7db2\u7d61\u61c9\u7528\u4e2d\uff0c\u5716\u7247\u7684\u986f\u793a\u662f\u7528\u6236\u9ad4\u9a57\u7684\u91cd\u8981\u7d44\u6210\u90e8\u5206\u3002\u5c24\u5176\u662f\u5728\u4f7f\u7528Java Server Pages (JSP)\u6280\u8853\u6642\uff0c\u5f9e\u6578\u64da\u5eab\u4e2d\u7372\u53d6\u4e26\u986f\u793a\u5716\u7247\u7684\u904e\u7a0b\u662f\u958b\u767c\u8005\u9700\u8981\u638c\u63e1\u7684\u57fa\u672c\u6280\u80fd\u4e4b\u4e00\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39\u5982\u4f55\u5728JSP\u4e2d\u7372\u53d6\u6578\u64da\u5eab\u4e2d\u7684\u5716\u7247\u4e26\u986f\u793a\u51fa\u4f86\u3002<\/p>\n<h2 id=\"%e6%95%b8%e6%93%9a%e5%ba%ab%e8%a8%ad%e8%a8%88-tOeGXBxORn\">\u6578\u64da\u5eab\u8a2d\u8a08<\/h2>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u8a2d\u8a08\u4e00\u500b\u6578\u64da\u5eab\u4f86\u5b58\u5132\u5716\u7247\u3002\u901a\u5e38\uff0c\u5716\u7247\u53ef\u4ee5\u4ee5\u4e8c\u9032\u5236\u683c\u5f0f\uff08BLOB\uff09\u5b58\u5132\u5728\u6578\u64da\u5eab\u4e2d\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u6578\u64da\u5eab\u8868\u7d50\u69cb\u793a\u4f8b\uff1a<\/p>\n<pre>\nCREATE TABLE images (\n    id INT PRIMARY KEY AUTO_INCREMENT,\n    name VARCHAR(255),\n    image BLOB\n);\n<\/pre>\n<p>\u5728\u9019\u500b\u8868\u4e2d\uff0c`id`\u662f\u5716\u7247\u7684\u552f\u4e00\u6a19\u8b58\uff0c`name`\u662f\u5716\u7247\u7684\u540d\u7a31\uff0c\u800c`image`\u5247\u662f\u5b58\u5132\u5716\u7247\u7684\u4e8c\u9032\u5236\u6578\u64da\u3002<\/p>\n<h2 id=\"%e4%b8%8a%e5%82%b3%e5%9c%96%e7%89%87%e5%88%b0%e6%95%b8%e6%93%9a%e5%ba%ab-tOeGXBxORn\">\u4e0a\u50b3\u5716\u7247\u5230\u6578\u64da\u5eab<\/h2>\n<p>\u5728\u986f\u793a\u5716\u7247\u4e4b\u524d\uff0c\u6211\u5011\u9700\u8981\u5c07\u5716\u7247\u4e0a\u50b3\u5230\u6578\u64da\u5eab\u3002\u9019\u901a\u5e38\u6d89\u53ca\u5230\u4e00\u500bHTML\u8868\u55ae\u548c\u4e00\u500bServlet\u4f86\u8655\u7406\u4e0a\u50b3\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684HTML\u8868\u55ae\u793a\u4f8b\uff1a<\/p>\n<pre>\n\n    \n    \n    \n\n<\/pre>\n<p>\u5728Servlet\u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u78bc\u4f86\u8655\u7406\u5716\u7247\u7684\u4e0a\u50b3\uff1a<\/p>\n<pre>\nprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {\n    Part filePart = request.getPart(\"image\");\n    String name = request.getParameter(\"name\");\n    InputStream inputStream = filePart.getInputStream();\n\n    Connection conn = null; \/\/ \u7372\u53d6\u6578\u64da\u5eab\u9023\u63a5\n    PreparedStatement statement = null;\n\n    try {\n        String sql = \"INSERT INTO images (name, image) VALUES (?, ?)\";\n        statement = conn.prepareStatement(sql);\n        statement.setString(1, name);\n        statement.setBlob(2, inputStream);\n        statement.executeUpdate();\n    } catch (SQLException e) {\n        e.printStackTrace();\n    } finally {\n        \/\/ \u95dc\u9589\u8cc7\u6e90\n    }\n}\n<\/pre>\n<h2 id=\"%e5%be%9e%e6%95%b8%e6%93%9a%e5%ba%ab%e4%b8%ad%e7%8d%b2%e5%8f%96%e5%9c%96%e7%89%87-tOeGXBxORn\">\u5f9e\u6578\u64da\u5eab\u4e2d\u7372\u53d6\u5716\u7247<\/h2>\n<p>\u4e00\u65e6\u5716\u7247\u4e0a\u50b3\u6210\u529f\uff0c\u6211\u5011\u5c31\u53ef\u4ee5\u5f9e\u6578\u64da\u5eab\u4e2d\u7372\u53d6\u4e26\u986f\u793a\u5b83\u5011\u3002\u4ee5\u4e0b\u662f\u4e00\u500bJSP\u9801\u9762\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5f9e\u6578\u64da\u5eab\u4e2d\u7372\u53d6\u5716\u7247\u4e26\u986f\u793a\uff1a<\/p>\n<pre>\n\n\n\n            <h3><\/h3>\n            &lt;img src=&quot;data:image\/jpeg;base64,\" \/&gt;\n\n<\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-tOeGXBxORn\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u4ee5\u4e0a\u6b65\u9a5f\uff0c\u6211\u5011\u53ef\u4ee5\u6210\u529f\u5730\u5728JSP\u4e2d\u7372\u53d6\u4e26\u986f\u793a\u6578\u64da\u5eab\u4e2d\u7684\u5716\u7247\u3002\u9019\u4e0d\u50c5\u63d0\u5347\u4e86\u7528\u6236\u9ad4\u9a57\uff0c\u9084\u4f7f\u5f97\u7db2\u7ad9\u7684\u5167\u5bb9\u66f4\u52a0\u8c50\u5bcc\u3002\u5c0d\u65bc\u9700\u8981\u8655\u7406\u5927\u91cf\u5716\u7247\u7684\u61c9\u7528\uff0c\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u5eab\u548c\u4f3a\u670d\u5668\u67b6\u69cb\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002\u82e5\u60a8\u9700\u8981\u9032\u4e00\u6b65\u4e86\u89e3\u5982\u4f55\u9078\u64c7\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u60a8\u7684\u61c9\u7528\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u4ee5\u7372\u53d6\u66f4\u591a\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3\u5982\u4f55\u4f7f\u7528JSP\u5f9e\u6578\u64da\u5eab\u7372\u53d6\u4e26\u986f\u793a\u5716\u7247\uff0c\u8a73\u7d30\u6b65\u9a5f\u548c\u793a\u4f8b\uff0c\u52a9\u60a8\u8f15\u9b06\u5be6\u73fe\u5716\u7247\u5c55\u793a\u529f\u80fd\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-97116","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97116","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=97116"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97116\/revisions"}],"predecessor-version":[{"id":97117,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97116\/revisions\/97117"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=97116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=97116"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=97116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}