{"id":138,"date":"2017-12-28T10:58:08","date_gmt":"2017-12-28T04:58:08","guid":{"rendered":"https:\/\/qtvz.com\/?p=138"},"modified":"2017-12-28T10:58:08","modified_gmt":"2017-12-28T04:58:08","slug":"spring-3-0spring-mvchibernate%e6%a1%86%e6%9e%b6-%e6%96%87%e4%bb%b6%e4%b8%8a%e4%bc%a0%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/qtvz.com\/138.html","title":{"rendered":"spring 3.0+spring MVC+Hibernate\u6846\u67b6 \u6587\u4ef6\u4e0a\u4f20\u529f\u80fd"},"content":{"rendered":"<!--wp-compress-html--><!--wp-compress-html no compression--><h1>1\u3001\u6ce8\u89e3<\/h1>\n<pre class=\"lang:default decode:true\">\t&lt;!-- \u4e0a\u4f20\u6587\u4ef6bean --&gt;\r\n\t&lt;bean id=\"multipartResolver\" class=\"org.springframework.web.multipart.commons.CommonsMultipartResolver\"&gt; \r\n\t\t&lt;property name=\"defaultEncoding\" value=\"utf-8\" \/&gt; \r\n\t\t&lt;property name=\"maxUploadSize\"   value=\"10485760000\" \/&gt; \r\n\t\t&lt;property name=\"maxInMemorySize\" value=\"40960\" \/&gt; \r\n\t&lt;\/bean&gt;\r\n\t&lt;!-- \u5904\u7406<a href=\"https:\/\/qtvz.com\/tag\/%e6%96%87%e4%bb%b6%e4%b8%8a%e4%bc%a0\" class=\"tag-tooltip\" data-toggle=\"tooltip\" title=\"\u67e5\u770b\u66f4\u591a\u5173\u4e8e \u6587\u4ef6\u4e0a\u4f20 \u7684\u6587\u7ae0\" target=\"_blank\">\u6587\u4ef6\u4e0a\u4f20<\/a> --&gt;\r\n&lt;!-- \t&lt;bean id=\"multipartResolver\"  \r\n    \tclass=\"org.springframework.web.multipart.commons.CommonsMultipartResolver\" &gt;  \r\n    \t&lt;property name=\"defaultEncoding\" value=\"gbk\"\/&gt; \u9ed8\u8ba4\u7f16\u7801 (ISO-8859-1)  \r\n    \t&lt;property name=\"maxInMemorySize\" value=\"10240\"\/&gt; \u6700\u5927\u5185\u5b58\u5927\u5c0f (10240)  \r\n    \t&lt;property name=\"uploadTempDir\" value=\"\/upload\/\"\/&gt; \u4e0a\u4f20\u540e\u7684\u76ee\u5f55\u540d (WebUtils#TEMP_DIR_CONTEXT_ATTRIBUTE)  \r\n    \t&lt;property name=\"maxUploadSize\" value=\"-1\"\/&gt; \u6700\u5927\u6587\u4ef6\u5927\u5c0f\uff0c-1\u4e3a\u65e0\u9650\u6b62(-1)  \r\n\t&lt;\/bean&gt; --&gt;<\/pre>\n<h1>2\u3001upload.jsp<\/h1>\n<pre class=\"lang:default decode:true \">&lt;%@ page language=\"java\" import=\"java.util.*\" pageEncoding=\"gbk\"%&gt;\r\n&lt;!DOCTYPE HTML PUBLIC \"-\/\/W3C\/\/DTD HTML 4.01 Transitional\/\/EN\"&gt;\r\n&lt;html&gt;\r\n\t&lt;head&gt;\r\n\t\t&lt;title&gt;\u6d4b\u8bd5springmvc\u4e2d\u4e0a\u4f20\u7684\u5b9e\u73b0&lt;\/title&gt;\r\n\t&lt;\/head&gt;\r\n\t&lt;body&gt;\r\n&lt;form action=\"upload.do\"  method=\"post\" enctype=\"multipart\/form-data\"&gt;\r\n\t\t\t&lt;input type=\"text\" name=\"name\" \/&gt;\r\n\t\t\t&lt;input type=\"file\" name=\"file\" \/&gt;\r\n\t\t\t&lt;input type=\"submit\" \/&gt;\r\n\t\t&lt;\/form&gt;\r\n\t&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<h1>3\u3001load_ok.jsp<\/h1>\n<pre class=\"lang:default decode:true \">&lt;%@ page language=\"java\" import=\"java.util.*\" pageEncoding=\"gbk\"%&gt;\r\n&lt;!DOCTYPE HTML PUBLIC \"-\/\/W3C\/\/DTD HTML 4.01 Transitional\/\/EN\"&gt;\r\n&lt;html&gt;\r\n  &lt;head&gt;\r\n  &lt;\/head&gt;\r\n  &lt;body&gt;\r\n\t   &lt;h1&gt;\u4e0a\u4f20\u6210\u529f\uff01&lt;\/h1&gt;\r\n  &lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<h1>4\u3001upload_error.jsp<\/h1>\n<pre class=\"lang:default decode:true \"> &lt;%@ page language=\"java\" import=\"java.util.*\" pageEncoding=\"gbk\"%&gt;\r\n&lt;!DOCTYPE HTML PUBLIC \"-\/\/W3C\/\/DTD HTML 4.01 Transitional\/\/EN\"&gt;\r\n&lt;html&gt;\r\n  &lt;head&gt;\r\n  &lt;\/head&gt;\r\n  &lt;body&gt;\r\n\t   &lt;h1&gt;\u4e0a\u4f20\u5931\u8d25\uff01&lt;\/h1&gt;\r\n  &lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<h1>5\u3001\u4e3b\u4ee3\u7801<\/h1>\n<pre class=\"\">import java.io.File;\r\nimport java.util.Date;\r\n\r\nimport javax.servlet.ServletContext;\r\n\r\nimport org.springframework.stereotype.Controller;\r\nimport org.springframework.web.bind.annotation.RequestMapping;\r\nimport org.springframework.web.bind.annotation.RequestMethod;\r\nimport org.springframework.web.bind.annotation.RequestParam;\r\nimport org.springframework.web.context.ServletContextAware;\r\nimport org.springframework.web.multipart.commons.CommonsMultipartFile;\r\n\r\n@Controller\r\npublic class FileUploadController implements ServletContextAware {\r\n\r\n\tprivate ServletContext servletContext;\r\n\r\n\tpublic void setServletContext(ServletContext context) {\r\n\t\tthis.servletContext=context;\r\n\t}\r\n\tpublic ServletContext getServletContext() {\r\n\t\treturn servletContext;\r\n\t}\r\n\t\r\n\t@RequestMapping(value=\"\/upload.do\",method=RequestMethod.POST)\r\n\tpublic String handlerUploadDate(String name,@RequestParam(\"file\")CommonsMultipartFile file){\r\n\t\tif(!file.isEmpty()){\r\n\t\t\tString path=this.servletContext.getRealPath(\"\/upload\/\"); \/\/\u83b7\u53d6\u672c\u5730\u5b58\u50a8\u8def\u5f84\r\n\t\t\tString fileName=file.getOriginalFilename();\r\n\t\t\tString fileType=fileName.substring(fileName.lastIndexOf(\".\"));\r\n\t\t\tFile file2=new File(path, new Date().getTime()+fileType); \/\/\u65b0\u5efa\u4e00\u4e2a\u6587\u4ef6\r\n\t\t\tSystem.out.println(fileType);\r\n\t\t\ttry {\r\n\t\t\t\tfile.getFileItem().write(file2);\/\/\u5c06\u4e0a\u4f20\u7684\u6587\u4ef6\u5199\u5165\u65b0\u5efa\u7684\u6587\u4ef6\u4e2d\r\n\t\t\t} catch (Exception e) {\r\n\t\t\t\te.printStackTrace();\r\n\t\t\t}\r\n\t\t\tSystem.out.println(\"\u59d3\u540d\uff1a\"+name+\"\u6587\u4ef6\u540d:\"+fileName+\"\u672c\u5730\u5b58\u50a8\u8def\u5f84\u4e3a:\"+path);\r\n\t\t\treturn \"redirect:upload_ok.jsp\";\r\n\t\t} else{\r\n\t\t\treturn \"redirect:upload_error.jsp\";\r\n\t\t}\r\n\t}\r\n\r\n}<\/pre>\n<!--wp-compress-html no compression--><!--wp-compress-html-->","protected":false},"excerpt":{"rendered":"1\u3001\u6ce8\u89e3 &lt;!&#8211; \u4e0a\u4f20\u6587\u4ef6bean &#8211;&gt; &lt;bean id=&#8221;multipartResolver&#8221; class=&#8221;org.springframework.web.multipar \u00b7\u00b7\u00b7","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[39],"tags":[48],"views":11898,"_links":{"self":[{"href":"https:\/\/qtvz.com\/api\/wp\/v2\/posts\/138"}],"collection":[{"href":"https:\/\/qtvz.com\/api\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qtvz.com\/api\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qtvz.com\/api\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qtvz.com\/api\/wp\/v2\/comments?post=138"}],"version-history":[{"count":1,"href":"https:\/\/qtvz.com\/api\/wp\/v2\/posts\/138\/revisions"}],"predecessor-version":[{"id":139,"href":"https:\/\/qtvz.com\/api\/wp\/v2\/posts\/138\/revisions\/139"}],"wp:attachment":[{"href":"https:\/\/qtvz.com\/api\/wp\/v2\/media?parent=138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qtvz.com\/api\/wp\/v2\/categories?post=138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qtvz.com\/api\/wp\/v2\/tags?post=138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}