{"id":434,"date":"2025-12-04T15:00:57","date_gmt":"2025-12-04T07:00:57","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=434"},"modified":"2025-12-04T15:00:57","modified_gmt":"2025-12-04T07:00:57","slug":"org-apache-commons-fileupload%e7%9a%84%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2025\/12\/04\/org-apache-commons-fileupload%e7%9a%84%e4%bd%bf%e7%94%a8\/","title":{"rendered":"org.apache.commons.fileupload\u7684\u4f7f\u7528"},"content":{"rendered":"\n<p>\u4e0b\u9762\u7ed9\u4f60\u4e00\u7bc7 <strong>\u6e05\u6670\u3001\u5b8c\u6574\u3001\u53ef\u76f4\u63a5\u7528\u4e8e\u5b66\u4e60\u6216\u5199\u6280\u672f\u6587\u6863\u7684<\/strong><br><strong>\u300aorg.apache.commons.fileupload \u4f7f\u7528\u6559\u7a0b\u300b<\/strong>\uff08\u542b\u793a\u4f8b\u4ee3\u7801 + \u5e38\u89c1\u95ee\u9898\uff09\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u4f60\u9700\u8981\uff0c\u6211\u4e5f\u53ef\u4ee5\u5e2e\u4f60\u751f\u6210 <strong>Spring MVC \/ Spring Boot \u7248\u672c\u7684\u6587\u4ef6\u4e0a\u4f20 Demo<\/strong>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>org.apache.commons.fileupload \u4f7f\u7528\u8be6\u89e3\uff08\u6700\u5b9e\u7528\u6559\u7a0b\uff09<\/strong><\/h1>\n\n\n\n<p>\u9002\u7528\u4e8e\uff1aJava Web\u3001Servlet\u3001Tomcat\u3001JSP \u4e0a\u4f20\u6587\u4ef6<\/p>\n\n\n\n<p><code>commons-fileupload<\/code> \u662f Apache \u6700\u7ecf\u5178\u7684\u6587\u4ef6\u4e0a\u4f20\u7ec4\u4ef6\uff0c\u5e38\u4e0e <code>commons-io<\/code> \u642d\u914d\uff0c\u7528\u4e8e <strong>Servlet 3.0 \u4e4b\u524d<\/strong>\u7684\u6587\u4ef6\u4e0a\u4f20\u5904\u7406\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83e\udde9 1. <strong>\u73af\u5883\u4f9d\u8d56<\/strong><\/h1>\n\n\n\n<p>\u5728 Maven \u9879\u76ee\u4e2d\u52a0\u5165\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;dependency&gt;\n    &amp;lt;groupId&gt;commons-fileupload&amp;lt;\/groupId&gt;\n    &amp;lt;artifactId&gt;commons-fileupload&amp;lt;\/artifactId&gt;\n    &amp;lt;version&gt;1.4&amp;lt;\/version&gt;\n&amp;lt;\/dependency&gt;\n\n&amp;lt;dependency&gt;\n    &amp;lt;groupId&gt;commons-io&amp;lt;\/groupId&gt;\n    &amp;lt;artifactId&gt;commons-io&amp;lt;\/artifactId&gt;\n    &amp;lt;version&gt;2.11.0&amp;lt;\/version&gt;\n&amp;lt;\/dependency&gt;\n\n<\/pre><\/div>\n\n\n<p>\u5982\u679c\u4e0d\u662f Maven\uff0c\u5219\u52a0\u5165 JAR\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>commons-fileupload-x.x.jar<\/li>\n\n\n\n<li>commons-io-x.x.jar<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83e\udde9 2. <strong>\u524d\u7aef\u4e0a\u4f20\u8868\u5355\uff08JSP \/ HTML\uff09<\/strong><\/h1>\n\n\n\n<p>\u5fc5\u987b\u5305\u542b\u4e24\u70b9\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>method=\"post\"<\/code><\/li>\n\n\n\n<li><code>enctype=\"multipart\/form-data\"<\/code><\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;form action=&quot;\/upload&quot; method=&quot;post&quot; enctype=&quot;multipart\/form-data&quot;&gt;\n    &amp;lt;input type=&quot;file&quot; name=&quot;file&quot;&gt;&amp;lt;br&gt;\n    &amp;lt;input type=&quot;submit&quot; value=&quot;\u4e0a\u4f20&quot;&gt;\n&amp;lt;\/form&gt;\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83e\udde9 3. <strong>\u6838\u5fc3\u5904\u7406\uff1aServlet \u5904\u7406\u4e0a\u4f20\u6587\u4ef6<\/strong><\/h1>\n\n\n\n<p>\u4e0b\u9762\u662f\u6700\u7ecf\u5178\u7684\u539f\u751f Servlet \u4e0a\u4f20\u4ee3\u7801\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n@WebServlet(&quot;\/upload&quot;)\npublic class UploadServlet extends HttpServlet {\n\n    @Override\n    protected void doPost(HttpServletRequest request, HttpServletResponse response)\n            throws ServletException, IOException {\n\n        \/\/ \u9996\u5148\u5224\u65ad\u662f\u5426\u4e3a multipart\/form-data\n        boolean isMultipart = ServletFileUpload.isMultipartContent(request);\n        if (!isMultipart) {\n            response.getWriter().println(&quot;Error: \u8868\u5355\u5fc5\u987b\u5305\u542b enctype=multipart\/form-data&quot;);\n            return;\n        }\n\n        \/\/ 1. \u521b\u5efa\u6587\u4ef6\u5de5\u5382\uff08\u4e34\u65f6\u6587\u4ef6\uff09\n        DiskFileItemFactory factory = new DiskFileItemFactory();\n        factory.setSizeThreshold(1024 * 1024);   \/\/ \u8bbe\u7f6e\u5185\u5b58\u7f13\u51b2\u533a\u5927\u5c0f (1MB)\n        factory.setRepository(new File(&quot;D:\/temp&quot;)); \/\/ \u4e34\u65f6\u76ee\u5f55\n\n        \/\/ 2. \u521b\u5efa\u6587\u4ef6\u4e0a\u4f20\u89e3\u6790\u5668\n        ServletFileUpload upload = new ServletFileUpload(factory);\n        upload.setHeaderEncoding(&quot;UTF-8&quot;);  \/\/ \u907f\u514d\u4e2d\u6587\u6587\u4ef6\u540d\u4e71\u7801\n\n        \/\/ 3. \u8bbe\u7f6e\u5355\u4e2a\u6587\u4ef6 \/ \u7ed3\u5408\u5927\u5c0f\u9650\u5236\n        upload.setFileSizeMax(10 * 1024 * 1024); \/\/ 10MB\n        upload.setSizeMax(20 * 1024 * 1024);     \/\/ \u8bf7\u6c42\u603b\u5927\u5c0f 20MB\n\n        try {\n            \/\/ 4. \u89e3\u6790\u8bf7\u6c42\n            List&amp;lt;FileItem&gt; formItems = upload.parseRequest(request);\n\n            if (formItems != null &amp;amp;&amp;amp; formItems.size() &gt; 0) {\n                for (FileItem item : formItems) {\n\n                    if (!item.isFormField()) {\n                        \/\/ 5. \u5904\u7406\u6587\u4ef6\u5b57\u6bb5\n                        String fileName = new File(item.getName()).getName();\n                        String uploadPath = &quot;D:\/upload\/&quot;;\n\n                        File storeFile = new File(uploadPath + fileName);\n\n                        \/\/ \u521b\u5efa\u76ee\u5f55\n                        storeFile.getParentFile().mkdirs();\n\n                        \/\/ 6. \u4fdd\u5b58\u6587\u4ef6\n                        item.write(storeFile);\n\n                        response.getWriter().println(&quot;\u4e0a\u4f20\u6210\u529f: &quot; + fileName);\n                    } else {\n                        \/\/ \u5904\u7406\u666e\u901a\u8868\u5355\u5b57\u6bb5\n                        String fieldName = item.getFieldName();\n                        String fieldValue = item.getString(&quot;UTF-8&quot;);\n                        System.out.println(fieldName + &quot;=&quot; + fieldValue);\n                    }\n                }\n            }\n        } catch (Exception ex) {\n            response.getWriter().println(&quot;\u4e0a\u4f20\u5931\u8d25\uff1a&quot; + ex.getMessage());\n        }\n    }\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83e\udde9 4. <strong>\u5173\u952e API \u89e3\u91ca<\/strong><\/h1>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u2714 DiskFileItemFactory<\/strong><\/h3>\n\n\n\n<p>\u7528\u4e8e\u7ba1\u7406\u4e0a\u4f20\u7684\u6587\u4ef6\u548c\u4e34\u65f6\u7f13\u51b2\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u65b9\u6cd5<\/th><th>\u4f5c\u7528<\/th><\/tr><\/thead><tbody><tr><td><code>setSizeThreshold()<\/code><\/td><td>\u5185\u5b58\u7f13\u51b2\u533a\u5927\u5c0f<\/td><\/tr><tr><td><code>setRepository()<\/code><\/td><td>\u4e34\u65f6\u6587\u4ef6\u76ee\u5f55\uff08\u5b58\u786c\u76d8\u7684\u5730\u65b9\uff09<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u2714 ServletFileUpload<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u65b9\u6cd5<\/th><th>\u4f5c\u7528<\/th><\/tr><\/thead><tbody><tr><td><code>parseRequest()<\/code><\/td><td>\u89e3\u6790\u4e0a\u4f20\u8bf7\u6c42<\/td><\/tr><tr><td><code>setFileSizeMax()<\/code><\/td><td>\u5355\u4e2a\u6587\u4ef6\u9650\u5236<\/td><\/tr><tr><td><code>setSizeMax()<\/code><\/td><td>\u603b\u5927\u5c0f\u9650\u5236<\/td><\/tr><tr><td><code>setHeaderEncoding(\"UTF-8\")<\/code><\/td><td>\u907f\u514d\u4e2d\u6587\u5b57\u6bb5\u4e71\u7801<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u2714 FileItem<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u65b9\u6cd5<\/th><th>\u4f5c\u7528<\/th><\/tr><\/thead><tbody><tr><td><code>isFormField()<\/code><\/td><td>\u5224\u65ad\u662f\u5426\u4e3a\u666e\u901a\u8868\u5355\u9879<\/td><\/tr><tr><td><code>getName()<\/code><\/td><td>\u83b7\u53d6\u6587\u4ef6\u540d<\/td><\/tr><tr><td><code>write(File)<\/code><\/td><td>\u4fdd\u5b58\u6587\u4ef6<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83e\udde9 5. \u6587\u4ef6\u540d\u4e71\u7801\u3001\u8def\u5f84\u95ee\u9898\uff08\u5e38\u89c1\u9519\u8bef\uff09<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u95ee\u9898 1\uff1a\u4e2d\u6587\u6587\u4ef6\u540d\u4e71\u7801<\/strong><\/h3>\n\n\n\n<p>\u89e3\u51b3\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nupload.setHeaderEncoding(&quot;UTF-8&quot;);\nitem.getString(&quot;UTF-8&quot;);\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u95ee\u9898 2\uff1a\u4e0a\u4f20\u76ee\u5f55\u65e0\u6cd5\u5199\u5165<\/strong><\/h3>\n\n\n\n<p>\u786e\u4fdd\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Tomcat \u6709\u5199\u6743\u9650<\/li>\n\n\n\n<li>Windows \u76ee\u5f55\u4e0d\u80fd\u5305\u542b\u4e2d\u6587<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u95ee\u9898 3\uff1a\u6587\u4ef6\u592a\u5927\uff0c\u62a5\uff1aSizeLimitExceededException<\/strong><\/h3>\n\n\n\n<p>\u589e\u52a0\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nupload.setFileSizeMax(50 * 1024 * 1024);\nupload.setSizeMax(100 * 1024 * 1024);\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83e\udde9 6. \u591a\u6587\u4ef6\u4e0a\u4f20\u793a\u4f8b<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;input type=&quot;file&quot; name=&quot;files&quot; multiple&gt;\n\n<\/pre><\/div>\n\n\n<p>\u540e\u7aef\u81ea\u52a8\u904d\u5386\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nif (!item.isFormField()) {\n    File storeFile = new File(uploadPath + item.getName());\n    item.write(storeFile);\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83e\udde9 7. \u7ed3\u5408 Spring MVC \u7684\u4f7f\u7528\uff08\u4f60\u9700\u8981\u6211\u53ef\u4ee5\u5199\u66f4\u5b8c\u6574\uff09<\/h1>\n\n\n\n<p>\u53ea\u9700\u4f7f\u7528\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nCommonsMultipartResolver\n\n<\/pre><\/div>\n\n\n<p>\u66f4\u73b0\u4ee3\u7684\u9879\u76ee\u4f7f\u7528\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Spring Boot \u2192 <code>MultipartFile<\/code><\/li>\n\n\n\n<li>Servlet 3.1+ \u5185\u7f6e\u6587\u4ef6\u4e0a\u4f20 API \u2192 \u4e0d\u9700\u8981 fileupload<\/li>\n<\/ul>\n\n\n\n<p>\u5982\u4f60\u9700\u8981\uff0c\u6211\u53ef\u4ee5\u5199\uff1a<\/p>\n\n\n\n<p>\u2714 Spring MVC + fileupload \u914d\u7f6e<br>\u2714 Spring Boot \u7248\u672c\uff08\u65e0\u9700 fileupload\uff09<br>\u2714 JSP \u4e0a\u4f20\u9875\u9762 + Controller<br>\u2714 \u5b8c\u6574 Demo \u5de5\u7a0b\u7ed3\u6784<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u7ed9\u4f60\u4e00\u7bc7 \u6e05\u6670\u3001\u5b8c\u6574\u3001\u53ef\u76f4\u63a5\u7528\u4e8e\u5b66\u4e60\u6216\u5199\u6280\u672f\u6587\u6863\u7684\u300aorg.apache.c&#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2025\/12\/04\/org-apache-commons-fileupload%e7%9a%84%e4%bd%bf%e7%94%a8\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-434","post","type-post","status-publish","format-standard","hentry","category-12"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/434","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=434"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/434\/revisions"}],"predecessor-version":[{"id":435,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/434\/revisions\/435"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=434"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=434"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=434"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}