{"id":1587,"date":"2017-12-15T10:38:36","date_gmt":"2017-12-15T02:38:36","guid":{"rendered":"http:\/\/coderbee.net\/?p=1587"},"modified":"2017-12-15T10:38:36","modified_gmt":"2017-12-15T02:38:36","slug":"spring-mvc-%e6%96%87%e4%bb%b6%e4%b8%8a%e4%bc%a0%e4%bc%98%e5%8c%96","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/java\/20171215\/1587","title":{"rendered":"Spring-MVC \u6587\u4ef6\u4e0a\u4f20\u4f18\u5316"},"content":{"rendered":"<p>Spring-MVC \u6587\u4ef6\u4e0a\u4f20\u4f18\u5316<\/p>\n<ol>\n<li>\n<p>\u914d\u7f6e <code>CommonsMultipartResolver<\/code> \u65f6\u628a <code>maxInMemorySize<\/code> \u914d\u7f6e\u4e3a\u5408\u9002\u7684\u5927\u5c0f\uff0c\u8ba9\u5c0f\u6587\u4ef6\u53ef\u4ee5\u7f13\u5b58\u5728\u5185\u5b58\u4e2d\uff0c\u5bf9\u78c1\u76d8\u53ea\u9700\u4e00\u6b21\u5199\u64cd\u4f5c\uff1b<\/p>\n<\/li>\n<li>\n<p>\u5728 Controller \u91cc\u8c03\u7528 <code>multipartFile.transferTo(file);<\/code> \u628a\u6587\u4ef6\u4fdd\u5b58\u5230\u76ee\u6807\u8def\u5f84\u3002\u8be5\u65b9\u6cd5\u9996\u5148\u8fdb\u884c\u91cd\u547d\u540d\uff0c\u5982\u679c\u4e0d\u6210\u529f\u5219\u8fdb\u884c\u6d41\u62f7\u8d1d\uff0c\u5982\u679c\u6210\u529f\u5219\u53ef\u4ee5\u7701\u4e0b\u4e00\u6b21\u8bfb\u3001\u5199\u64cd\u4f5c\u3002\u5bf9\u4e8e <code>org.apache.commons.fileupload.disk.DiskFileItem<\/code> \u7c7b\uff0c\u8c03\u7528\u7684\u65b9\u6cd5\u662f\u4e0b\u9762\u8fd9\u4e2a\uff1a<\/p>\n<\/li>\n<\/ol>\n<pre><code class=\"java\">public void write(File file) throws Exception {\n    if (isInMemory()) {\n        FileOutputStream fout = null;\n        try {\n            fout = new FileOutputStream(file);\n            fout.write(get());\n        } finally {\n            if (fout != null) {\n                fout.close();\n            }\n        }\n    } else {\n        File outputFile = getStoreLocation();\n        if (outputFile != null) {\n            \/\/ Save the length of the file\n            size = outputFile.length();\n            \/*\n             * The uploaded file is being stored on disk\n             * in a temporary location so move it to the\n             * desired file.\n             *\/\n            if (!outputFile.renameTo(file)) {\n                BufferedInputStream in = null;\n                BufferedOutputStream out = null;\n                try {\n                    in = new BufferedInputStream(\n                        new FileInputStream(outputFile));\n                    out = new BufferedOutputStream(\n                            new FileOutputStream(file));\n                    IOUtils.copy(in, out);\n                } finally {\n                    if (in != null) {\n                        try {\n                            in.close();\n                        } catch (IOException e) {\n                            \/\/ ignore\n                        }\n                    }\n                    if (out != null) {\n                        try {\n                            out.close();\n                        } catch (IOException e) {\n                            \/\/ ignore\n                        }\n                    }\n                }\n            }\n        } else {\n            \/*\n             * For whatever reason we cannot write the\n             * file to disk.\n             *\/\n            throw new FileUploadException(\n                \"Cannot write uploaded file to disk!\");\n        }\n    }\n}\n<\/code><\/pre>\n<hr\/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong>\uff0c\u53ef\u4ee5\u66f4\u53ca\u65f6\u56de\u590d\u4f60\u7684\u8ba8\u8bba\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"258\" height=\"258\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/01\/coderbee-note.jpg\" class=\"alignnone size-full wp-image-1707\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spring-MVC \u6587\u4ef6\u4e0a\u4f20\u4f18\u5316 \u914d\u7f6e CommonsMultipartRes &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/java\/20171215\/1587\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[292],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1587"}],"collection":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/comments?post=1587"}],"version-history":[{"count":2,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1587\/revisions"}],"predecessor-version":[{"id":1589,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1587\/revisions\/1589"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=1587"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=1587"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=1587"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}