{"id":2291,"date":"2022-12-08T22:26:31","date_gmt":"2022-12-08T14:26:31","guid":{"rendered":"https:\/\/qaqaq.top\/?p=2291"},"modified":"2022-12-19T20:42:36","modified_gmt":"2022-12-19T12:42:36","slug":"%e6%96%87%e4%bb%b6%e4%b8%8a%e4%bc%a0-%e6%96%87%e4%bb%b6%e4%b8%8a%e4%bc%a0%e7%9a%84%e4%bb%8b%e7%bb%8d%e3%80%81%e6%96%87%e4%bb%b6%e4%b8%8a%e4%bc%a0-%e4%b8%8a%e4%bc%a0%e7%9a%84http%e5%8d%8f%e8%ae%ae","status":"publish","type":"post","link":"https:\/\/qaqaq.top\/?p=2291","title":{"rendered":"\u6587\u4ef6\u4e0a\u4f20-\u6587\u4ef6\u4e0a\u4f20\u7684\u4ecb\u7ecd\u3001\u4e0a\u4f20\u7684http\u534f\u8bae\u5185\u5bb9\u4ecb\u7ecd\u3001\u4e0a\u4f20\u5408\u7528\u5230\u7684\u7c7b\u548c\u65b9\u6cd5\u7684\u4ecb\u7ecd\u3001\u4f7f\u7528fileupload\u89e3\u6790\u4e0a\u4f20\u7684\u6570\u636e"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">\u6587\u4ef6\u7684\u4e0a\u4f20\u548c\u4e0b\u8f7d<\/h1>\n\n\n\n<p>\u6587\u4ef6\u7684\u4e0a\u4f20\u548c\u4e0b\u8f7d\uff0c\u662f\u975e\u5e38\u5e38\u89c1\u7684\u529f\u80fd\u3002\u5f88\u591a\u7684\u7cfb\u7edf\u4e2d\uff0c\u6216\u8005\u8f6f\u4ef6\u4e2d\u90fd\u7ecf\u5e38\u4f7f\u7528\u6587\u4ef6\u7684\u4e0a\u4f20\u548c\u4e0b\u8f7d\u3002<br>\u6bd4\u5982\uff1aQQ \u5934\u50cf\uff0c\u5c31\u4f7f\u7528\u4e86\u4e0a\u4f20\u3002<br>\u90ae\u7bb1\u4e2d\u4e5f\u6709\u9644\u4ef6\u7684\u4e0a\u4f20\u548c\u4e0b\u8f7d\u529f\u80fd\u3002<br>OA \u7cfb\u7edf\u4e2d\u5ba1\u6279\u6709\u9644\u4ef6\u6750\u6599\u7684\u4e0a\u4f20\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1\u3001\u6587\u4ef6\u7684\u4e0a\u4f20\u4ecb\u7ecd\uff08<strong>*<\/strong>\u91cd\u70b9\uff09<\/h2>\n\n\n\n<p>1\u3001\u8981\u6709\u4e00\u4e2a form \u6807\u7b7e\uff0cmethod=post \u8bf7\u6c42<br>2\u3001form \u6807\u7b7e\u7684 encType \u5c5e\u6027\u503c\u5fc5\u987b\u4e3a multipart\/form-data \u503c<br>3\u3001\u5728 form \u6807\u7b7e\u4e2d\u4f7f\u7528 input type=file \u6dfb\u52a0\u4e0a\u4f20\u7684\u6587\u4ef6<br>4\u3001\u7f16\u5199\u670d\u52a1\u5668\u4ee3\u7801\uff08Servlet \u7a0b\u5e8f\uff09\u63a5\u6536\uff0c\u5904\u7406\u4e0a\u4f20\u7684\u6570\u636e\u3002<\/p>\n\n\n\n<p>encType=multipart\/form-data \u8868\u793a\u63d0\u4ea4\u7684\u6570\u636e\uff0c\u4ee5\u591a\u6bb5\uff08\u6bcf\u4e00\u4e2a\u8868\u5355\u9879\u4e00\u4e2a\u6570\u636e\u6bb5\uff09\u7684\u5f62\u5f0f\u8fdb\u884c\u62fc\u63a5\uff0c\u7136\u540e\u4ee5\u4e8c\u8fdb\u5236\u6d41\u7684\u5f62\u5f0f\u53d1\u9001\u7ed9\u670d\u52a1\u5668<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1.1\u3001 \u6587\u4ef6\u4e0a\u4f20\uff0cHTTP \u534f\u8bae\u7684\u8bf4\u660e\u3002<\/h3>\n\n\n\n<figure class=\"wp-block-image size-large\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/qaqaq.top\/wp-content\/uploads\/2022\/12\/image-128-1024x451.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"451\" data-original=\"https:\/\/qaqaq.top\/wp-content\/uploads\/2022\/12\/image-128-1024x451.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-2292\"  sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/div><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">1.2\u3001commons-fileupload.jar \u5e38\u7528 API \u4ecb\u7ecd\u8bf4\u660e<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>commons-fileupload.jar \u5e38\u7528 API \u4ecb\u7ecd\u8bf4\u660e\n\n        commons-fileupload.jar \u9700\u8981\u4f9d\u8d56 commons-io.jar \u8fd9\u4e2a\u5305\uff0c\u6240\u4ee5\u4e24\u4e2a\u5305\u6211\u4eec\u90fd\u8981\u5f15\u5165\u3002\n\n        \u7b2c\u4e00\u6b65\uff0c\u5c31\u662f\u9700\u8981\u5bfc\u5165\u4e24\u4e2a jar \u5305\uff1a\n            commons-fileupload-1.2.1.jar\n            commons-io-1.4.jar\n\ncommons-fileupload.jar \u548c commons-io.jar \u5305\u4e2d\uff0c\u6211\u4eec\u5e38\u7528\u7684\u7c7b\u6709\u54ea\u4e9b\uff1f\n\n            ServletFileUpload \u7c7b\uff0c\u7528\u4e8e\u89e3\u6790\u4e0a\u4f20\u7684\u6570\u636e\u3002\n\n            FileItem \u7c7b\uff0c\u8868\u793a\u6bcf\u4e00\u4e2a\u8868\u5355\u9879\u3002\n\n            boolean ServletFileUpload.isMultipartContent(HttpServletRequest request);\n            \u5224\u65ad\u5f53\u524d\u4e0a\u4f20\u7684\u6570\u636e\u683c\u5f0f\u662f\u5426\u662f\u591a\u6bb5\u7684\u683c\u5f0f\u3002\n\n            public List&lt;FileItem&gt; parseRequest(HttpServletRequest request)\n            \u89e3\u6790\u4e0a\u4f20\u7684\u6570\u636e\n\n            boolean FileItem.isFormField()\n            \u5224\u65ad\u5f53\u524d\u8fd9\u4e2a\u8868\u5355\u9879\uff0c\u662f\u5426\u662f\u666e\u901a\u7684\u8868\u5355\u9879\u3002\u8fd8\u662f\u4e0a\u4f20\u7684\u6587\u4ef6\u7c7b\u578b\u3002\n            true \u8868\u793a\u666e\u901a\u7c7b\u578b\u7684\u8868\u5355\u9879\n            false \u8868\u793a\u4e0a\u4f20\u7684\u6587\u4ef6\u7c7b\u578b\n\n            String FileItem.getFieldName()\n            \u83b7\u53d6\u8868\u5355\u9879\u7684 name \u5c5e\u6027\u503c\n\n            String FileItem.getString()\n            \u83b7\u53d6\u5f53\u524d\u8868\u5355\u9879\u7684\u503c\u3002\n\n            String FileItem.getName();\n            \u83b7\u53d6\u4e0a\u4f20\u7684\u6587\u4ef6\u540d\n\n            void FileItem.write( file );\n            \u5c06\u4e0a\u4f20\u7684\u6587\u4ef6\u5199\u5230 \u53c2\u6570 file \u6240\u6307\u5411\u7684\u78c1\u76d8\u4f4d\u7f6e \u3002<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">1.3\u3001fileupload \u7c7b\u5e93\u7684\u4f7f\u7528\uff1a<\/h3>\n\n\n\n<p>\u4e0a\u4f20\u6587\u4ef6\u7684\u8868\u5355\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;form action=\"http:\/\/192.168.31.74:8080\/09_EL_JSTL\/uploadServlet\" method=\"post\"\nenctype=\"multipart\/form-data\"&gt;\n\u7528\u6237\u540d\uff1a&lt;input type=\"text\" name=\"username\" \/&gt; &lt;br&gt;\n\u5934\u50cf\uff1a&lt;input type=\"file\" name=\"photo\" &gt; &lt;br&gt;\n&lt;input type=\"submit\" value=\"\u4e0a\u4f20\"&gt;&lt;\/form&gt;<\/code><\/pre>\n\n\n\n<p>\u89e3\u6790\u4e0a\u4f20\u7684\u6570\u636e\u7684\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**\n* \u7528\u6765\u5904\u7406\u4e0a\u4f20\u7684\u6570\u636e\n* @param req\n* @param resp\n* @throws ServletException\n* @throws IOException\n*\/\n@Override\nprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException,\nIOException {\n\/\/1 \u5148\u5224\u65ad\u4e0a\u4f20\u7684\u6570\u636e\u662f\u5426\u591a\u6bb5\u6570\u636e\uff08\u53ea\u6709\u662f\u591a\u6bb5\u7684\u6570\u636e\uff0c\u624d\u662f\u6587\u4ef6\u4e0a\u4f20\u7684\uff09\nif (ServletFileUpload.isMultipartContent(req)) {\n\/\/\n\u521b\u5efa FileItemFactory \u5de5\u5382\u5b9e\u73b0\u7c7b\nFileItemFactory fileItemFactory = new DiskFileItemFactory();\n\/\/ \u521b\u5efa\u7528\u4e8e\u89e3\u6790\u4e0a\u4f20\u6570\u636e\u7684\u5de5\u5177\u7c7b ServletFileUpload \u7c7b\nServletFileUpload servletFileUpload = new ServletFileUpload(fileItemFactory);\ntry {\n\/\/ \u89e3\u6790\u4e0a\u4f20\u7684\u6570\u636e\uff0c\u5f97\u5230\u6bcf\u4e00\u4e2a\u8868\u5355\u9879 FileItem\nList&lt;FileItem&gt; list = servletFileUpload.parseRequest(req);\n\/\/ \u5faa\u73af\u5224\u65ad\uff0c\u6bcf\u4e00\u4e2a\u8868\u5355\u9879\uff0c\u662f\u666e\u901a\u7c7b\u578b\uff0c\u8fd8\u662f\u4e0a\u4f20\u7684\u6587\u4ef6\nfor (FileItem fileItem : list) {\nif (fileItem.isFormField()) {\n\/\/ \u666e\u901a\u8868\u5355\u9879\nSystem.out.println(\"\u8868\u5355\u9879\u7684 name \u5c5e\u6027\u503c\uff1a\" + fileItem.getFieldName());\n\/\/ \u53c2\u6570 UTF-8.\u89e3\u51b3\u4e71\u7801\u95ee\u9898\nSystem.out.println(\"\u8868\u5355\u9879\u7684 value \u5c5e\u6027\u503c\uff1a\" + fileItem.getString(\"UTF-8\"));\n} else {\n\/\/ \u4e0a\u4f20\u7684\u6587\u4ef6\nSystem.out.println(\"\u8868\u5355\u9879\u7684 name \u5c5e\u6027\u503c\uff1a\" + fileItem.getFieldName());\nSystem.out.println(\"\u4e0a\u4f20\u7684\u6587\u4ef6\u540d\uff1a\" + fileItem.getName());\nfileItem.write(new File(\"e:\\\\\" + fileItem.getName()));\n}\n}\n} catch (Exception e) {\ne.printStackTrace();\n}\n}\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u4ef6\u7684\u4e0a\u4f20\u548c\u4e0b\u8f7d \u6587\u4ef6\u7684\u4e0a\u4f20\u548c\u4e0b\u8f7d\uff0c\u662f\u975e\u5e38\u5e38\u89c1\u7684\u529f\u80fd\u3002\u5f88\u591a\u7684\u7cfb\u7edf\u4e2d\uff0c\u6216\u8005\u8f6f\u4ef6\u4e2d\u90fd\u7ecf\u5e38\u4f7f\u7528\u6587\u4ef6\u7684\u4e0a\u4f20\u548c\u4e0b\u8f7d\u3002\u6bd4\u5982 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59],"tags":[61],"class_list":["post-2291","post","type-post","status-publish","format-standard","hentry","category-javaweb-code","tag-javaweb"],"_links":{"self":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/2291"}],"collection":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2291"}],"version-history":[{"count":2,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/2291\/revisions"}],"predecessor-version":[{"id":2712,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/2291\/revisions\/2712"}],"wp:attachment":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2291"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2291"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2291"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}