{"id":2299,"date":"2022-12-08T22:32:22","date_gmt":"2022-12-08T14:32:22","guid":{"rendered":"https:\/\/qaqaq.top\/?p=2299"},"modified":"2022-12-08T22:32:22","modified_gmt":"2022-12-08T14:32:22","slug":"%e4%bd%bf%e7%94%a8-tomcat-10-%e7%9a%84servletfileuploadparserequestrequest-%e4%b8%ad%e7%9a%84%e9%94%99%e8%af%af","status":"publish","type":"post","link":"https:\/\/qaqaq.top\/?p=2299","title":{"rendered":"\u4f7f\u7528 tomcat 10 \u7684ServletFileUpload#parseRequest(request) \u4e2d\u7684\u9519\u8bef"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">\u5982\u4f55\u89e3\u51b3\u4f7f\u7528 tomcat 10 \u7684 ServletFileUpload#parseRequest(request) \u4e2d\u7684\u9519\u8bef\uff1f<\/h3>\n\n\n\n<p>\u6b63\u5728\u5f00\u53d1\u4e00\u4e2a\u7b80\u5355\u7684\u6587\u4ef6\u4e0a\u4f20\u7a0b\u5e8f\u3002\u6211\u5fc5\u987b\u4f7f\u7528 jakarta.servlet.* \u7c7b\uff0c\u56e0\u4e3a\u6211\u4f7f\u7528\u7684\u662f Tomcat v10\u3002\u6211\u5728 parseRequest(request) \u884c\u4e0a\u9047\u5230\u7f16\u8bd1\u65f6\u9519\u8bef\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ee3\u7801\uff1a<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>protected void doPost(HttpServletRequest request,HttpServletResponse response) throws servletexception,IOException {\r\n        \/\/ Todo Auto-generated method stub\r\n        ServletFileUpload sf = new ServletFileUpload(new diskFileItemFactory());\r\n        try {\r\n            List&lt;FileItem> multifiles = sf.parseRequest(request);\r\n            \r\n            for(FileItem i : multifiles) {\r\n                i.write(new File(\"C:\/Users\/Luffy\/Documents\/FileUploadDemo\/\"+i.getName()));\r\n            }\r\n            response.getWriter().print(\"The file is uploaded\");\r\n        } catch (Exception e) {\r\n            \/\/ Todo Auto-generated catch block\r\n            e.printstacktrace();\r\n        } \r\n        response.getWriter().print(\"The file is uploaded\");\r\n    }<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u9519\u8bef\u5982\u4e0b\uff1a<\/h3>\n\n\n\n<p>ServletFileUpload \u7c7b\u578b\u4e2d\u7684 parseRequest(javax.servlet.http.HttpServletRequest)\u00a0\u65b9\u6cd5\u4e0d\u9002\u7528\u4e8e\u53c2\u6570 (jakarta.servlet.http.HttpServletRequest)<\/p>\n\n\n\n<p>\u6211\u5728\u8c37\u6b4c\u4e0a\u641c\u7d22\u4e86\u5f88\u591a\uff0c\u4f46\u627e\u4e0d\u5230\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n\n\n\n<p>\u8bf7\u63d0\u51fa\u89e3\u51b3\u65b9\u6cd5\u6216\u53ef\u80fd\u7684\u89e3\u51b3\u65b9\u6848\u3002\u63d0\u524d\u81f4\u8c22\u3002<\/p>\n\n\n\n<p>\u8fd9\u662f\u6211\u5728\u5806\u6808\u6ea2\u51fa\u4e2d\u7684\u7b2c\u4e00\u7bc7\u6587\u7ae0\u3002\u6240\u4ee5\u5982\u679c\u6709\u7684\u8bdd\u8bf7\u5ffd\u7565\u6211\u7684\u9519\u8bef:)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u89e3\u51b3\u65b9\u6cd5<\/h3>\n\n\n\n<p>\u60a8\u6b63\u5728\u5c1d\u8bd5\u4f7f\u7528&nbsp;<code>commons-fileupload<\/code>&nbsp;\u4e2d\u7684&nbsp;<a href=\"https:\/\/commons.apache.org\/proper\/commons-fileupload\/apidocs\/org\/apache\/commons\/fileupload\/servlet\/ServletFileUpload.html\"><code>ServletFileUpload<\/code><\/a>&nbsp;\u7c7b\uff0c\u8be5\u7c7b\u4e0d\u9002\u7528\u4e8e&nbsp;<code>jakarta.servlet.http.HttpServletRequest<\/code>\u3002\u8be5\u5e93\u5fc5\u987b\u9002\u7528\u4e8e Servlet 5.0 \u7c7b\u3002<\/p>\n\n\n\n<p>\u5e78\u8fd0\u7684\u662f\uff0c\u4ece Servlet 3.0 (Tomcat 8.0)&nbsp;<code>multipart\/form-data<\/code>&nbsp;\u8bf7\u6c42\u53ef\u4ee5\u88ab servlet \u89e3\u6790\u3002\u4f60\u53ea\u9700\u8981\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u5411\u60a8\u7684 servlet \u6dfb\u52a0\u00a0<a href=\"https:\/\/jakarta.ee\/specifications\/servlet\/5.0\/apidocs\/jakarta\/servlet\/annotation\/multipartconfig\"><code>@MultipartConfig<\/code><\/a>\u00a0\u6ce8\u91ca\uff0c<\/li><li>\u4f7f\u7528<a href=\"https:\/\/jakarta.ee\/specifications\/servlet\/5.0\/apidocs\/jakarta\/servlet\/http\/httpservletrequest#getParts()\"><code>HttpServletRequest#getParts()<\/code><\/a>\uff1a<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>try {\r\n    final Collection&lt;Part> parts = request.getParts();\r\n    for (final Part part : parts) {\r\n       part.write(\"C:\/Users\/Luffy\/Documents\/FileUploadDemo\/\"+part.getSubmittedFileName());\r\n    }\r\n    response.getWriter().print(\"The file has been uploaded successfully.\");\r\n} catch (Exception e) {\r\n    response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,\"Upload failed.\");\r\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u89e3\u51b3\u4f7f\u7528 tomcat 10 \u7684 ServletFileUpload#parseRequest(reque [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[60],"tags":[61],"class_list":["post-2299","post","type-post","status-publish","format-standard","hentry","category-javaweb-","tag-javaweb"],"_links":{"self":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/2299"}],"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=2299"}],"version-history":[{"count":1,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/2299\/revisions"}],"predecessor-version":[{"id":2300,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/2299\/revisions\/2300"}],"wp:attachment":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2299"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2299"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2299"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}