
{"id":2744,"date":"2020-12-17T11:01:20","date_gmt":"2020-12-17T03:01:20","guid":{"rendered":"http:\/\/127.0.0.2\/?p=2744"},"modified":"2021-01-23T09:00:21","modified_gmt":"2021-01-23T01:00:21","slug":"2020java%e6%a1%86%e6%9e%b6%e6%95%99%e7%a8%8b%e4%b9%8bspringmvc%e7%9a%84%e5%9b%be%e7%89%87%e4%b8%8a%e4%bc%a0","status":"publish","type":"post","link":"https:\/\/www.9713job.com\/?p=2744","title":{"rendered":"2020java\u6846\u67b6\u6559\u7a0b\u4e4bspringMVC\u7684\u56fe\u7247\u4e0a\u4f20"},"content":{"rendered":"<h3>2020java\u6846\u67b6\u6559\u7a0b\u4e4bspringMVC\u7684\u56fe\u7247\u4e0a\u4f20<\/h3>\n<p><strong>1.\u5f15\u5165\u76f8\u5173\u4f9d\u8d56<\/strong><\/p>\n<pre>&lt;!-- \u6587\u4ef6\u5904\u7406 --&gt;\r\n&lt;dependency&gt;\r\n  &lt;groupId&gt;commons-fileupload&lt;\/groupId&gt;\r\n  &lt;artifactId&gt;commons-fileupload&lt;\/artifactId&gt;\r\n  &lt;version&gt;1.3.1&lt;\/version&gt;\r\n&lt;\/dependency&gt;<\/pre>\n<p><strong>2.\u5728springmvc\u7684\u914d\u7f6e\u4e2d\u6dfb\u52a0\u5982\u4e0b\u914d\u7f6e<\/strong><!--more--><\/p>\n<pre>&lt;!-- \u914d\u7f6e MultipartResolver \u5904\u7406\u6587\u4ef6\u4e0a\u4f20  --&gt;\r\n&lt;bean id=\"multipartResolver\"\r\n      class=\"org.springframework.web.multipart.commons.CommonsMultipartResolver\"&gt;\r\n    &lt;!-- \u9ed8\u8ba4\u7f16\u7801(ISO-8859-1) --&gt;\r\n    &lt;property name=\"defaultEncoding\" value=\"UTF-8\"\/&gt;\r\n    &lt;!-- \u6700\u5927\u5185\u5b58\u5927\u5c0f(10240) --&gt;\r\n    &lt;property name=\"maxInMemorySize\" value=\"10240\"\/&gt;\r\n    &lt;!-- \u6700\u5927\u6587\u4ef6\u5927\u5c0f\uff0c\u5355\u4f4d\u662f\u5b57\u8282\uff0c-1\u4e3a\u65e0\u9650\u5236 --&gt;\r\n    &lt;property name=\"maxUploadSize\" value=\"-1\"\/&gt;\r\n    &lt;!-- \u4e0a\u4f20\u7684\u76ee\u5f55\u540d --&gt;\r\n    &lt;property name=\"uploadTempDir\" value=\"\/upload\/\"\/&gt;\r\n&lt;\/bean&gt;<\/pre>\n<p><!--more--><\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-2254\" src=\"\/wp-content\/uploads\/2020\/12\/17\/1.png\" alt=\"\" width=\"680\" \/><\/p>\n<p><strong>3.\u4e0a\u4f20\u8868\u5355<\/strong><\/p>\n<pre>&lt;form action=\"fileupload\" method=\"post\" enctype=\"multipart\/form-data\"&gt;\r\n    file : &lt;input type=\"file\" name=\"file\"&gt;&lt;br&gt;\r\n    description : &lt;input type=\"text\" name=\"desc\"&gt; &lt;br&gt;\r\n    &lt;button&gt;\u4e0a\u4f20&lt;\/button&gt;\r\n&lt;\/form&gt;<\/pre>\n<p><strong>4.\u63a7\u5236\u5668<\/strong><\/p>\n<pre>\/\/\u6587\u4ef6\u4e0a\u4f20\r\n@RequestMapping(value = \"\/fileupload\")\r\npublic String fieupload(String desc, MultipartFile file,Model model) throws Exception{\r\n    String origName=file.getOriginalFilename();\r\n    System.out.println(desc);\r\n    System.out.println(file);\r\n    System.out.println(origName);\r\n    if(file!=null &amp;&amp; origName!=null){\r\n        String filepath=\"D:\\\\upload\\\\\";\r\n        \/\/\u83b7\u53d6\u6269\u5c55\u540d\r\n        String extendsName=origName.substring(origName.lastIndexOf(\".\"));\r\n        \/\/\u83b7\u53d6\u6587\u4ef6\u540d\r\n        String newFilename= UUID.randomUUID().toString()+extendsName;\r\n        \/\/\u6587\u4ef6\r\n        File uploadFile=new File(filepath+newFilename);\r\n        file.transferTo(uploadFile);\r\n        model.addAttribute(\"filepath\",\"\/file\/\"+newFilename);\r\n    }\r\n    return \"success\";\r\n}<\/pre>\n<p><strong>5.\u914d\u7f6e\u865a\u62df\u8def\u5f84<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>2020java\u6846\u67b6\u6559\u7a0b\u4e4bspringMVC\u7684\u56fe\u7247\u4e0a\u4f20 1.\u5f15\u5165\u76f8\u5173\u4f9d\u8d56 &lt;!&#8211; \u6587\u4ef6\u5904\u7406 &#8211;&#038;gt [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-2744","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/2744","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2744"}],"version-history":[{"count":3,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/2744\/revisions"}],"predecessor-version":[{"id":2977,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/2744\/revisions\/2977"}],"wp:attachment":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2744"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2744"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2744"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}