Package Com.foo.controller;import Java.io.file;import Java.io.ioexception;import java.text.simpledateformat;import Java.util.date;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.multipart.multipartfile;import org.springframework.web.multipart.MultipartHttpServletRequest; @Controller @requestmapping (value= "/file") public Class fileupload{@RequestMapping (value= "/gotofilepage") public String Gotofilepage () {System.out.println (" Enter into the Gotofilepage method in file .... "); return" UploadFile ";} @RequestMapping (value= "/fileupload") public void FileUpload (HttpServletRequest request,httpservletresponse response {Multiparthttpservletrequest multipartrequest = (multiparthttpservletrequest) request; SimpleDateFormat DateFormat = new SimpleDateFormat ("yyyy/mm/dd/hh");/** get the true path of the picture save directory **/string Realpath = Request.getsession (). GetservletContext (). Getrealpath ("/");/** build picture saved directory **/string Filedir = "files" + file.separator+ Dateformat.format (new Date ()); String Filelocationdir = Realpath + filedir;/** Creates a directory based on the true path **/file logosavefile = new File (filelocationdir); if (!logosavef Ile.exists ()) logosavefile.mkdirs ();/** the file stream of the page control **/multipartfile multipartfile = multipartrequest.getfile ("file"); * * Get the suffix of the file to prevent others from transmitting executable files **/string suffix = multipartfile.getoriginalfilename (). SUBSTRING ( Multipartfile.getoriginalfilename (). LastIndexOf (".")); String imagename = Multipartfile.getoriginalfilename ();/** to complete file save path Plus file **/string filename = filelocationdir + File.separator + imagename; File File = new file (filename); try {multipartfile.transferto (file);} catch (IllegalStateException e) {E.printstacktrace ();} catch (IOException e) {e.printstacktrace ();}}}
uploadfile.jsp
<body>
Springmvc-servlet.xml<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc= "Http://www.springframework.org/schema/mvc" xmlns: context= "Http://www.springframework.org/schema/context" xmlns:tx= "Http://www.springframework.org/schema/tx" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:task= "Http://www.springframework.org/schema/task" xsi:schemalocation= "Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/ Spring-mvc-3.0.xsd Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context /spring-context-3.0.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ Spring-tx-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/Spring-aop-3.0.xsd Http://www.springframework.org/schema/task http://www.springframework.org/schema/task/ Spring-task-3.0.xsd "><!--@Controller, @Service, @Configuration, etc.--><context:component-scan Base-package= "Com.foo.controller"/><bean class= " Org.springframework.web.servlet.view.InternalResourceViewResolver "><property name=" prefix "value="/web-inf /views/"/><property name=" suffix "value=". jsp "/></bean><!--upload files must be--><bean id=" Multipartresolver "class=" Org.springframework.web.multipart.commons.CommonsMultipartResolver "><!--set the Max upload size100mb--><property name= "Maxuploadsize" ><value>104857600</value></property ><property name= "Maxinmemorysize" ><value>4096</value></property></bean><!-- <task:annotation-driven/><bean id= "Tasktest" class= "Com.htf.task.MyTask" ></bean><task: scheduled-tasks> <task:scheduled ref= "Tasktest" method= "Say" cron= "5/3 * * * *?"/> </task:scheduled-tasks><context:component-scan base-package= "Com.htf.task"/& Gt --></beans>
Jar Required
SPRINGMVC Bag
Commons-io-2.2.jar
Commons-fileupload-1.3.1.jar
File upload using Springmvc