This article describes the SSM (Spring+springmvc+mybatis) Implementation of the upload feature.
As a case of adding a user (mostly uploading files).
I. Introduction of REQUIREMENTS
We want to implement adding users when uploading images (in fact any file can).
file name: Named after the day of the month and minute of the blog name + Date
such as Words superciliousness blog 2017082516403213.png
path : Upload to the Uploads folder and generate the corresponding year and confinement folder
such as uploads/2017/8/words superciliousness blog 2017082516403213.png
database : Store "year/month/" + File name in data table
such as 2017/8/words superciliousness blog 20170825164809907.jpg
Second, import Jar package
The upload feature requires an additional two jar packages, as follows
In the import environment
I use here is Maven, add dependency
1 <!--file Upload- 2 <dependency> 3 <groupid>commons-fileupload</ Groupid> 4 <artifactId>commons-fileupload</artifactId> 5 <version >1.2.2</version> 6 </dependency> 7 <dependency> 8 < Groupid>commons-io</groupid> 9 <artifactId>commons-io</artifactId>10 <version>2.4</version> </dependency>
Third, the code structure
Files uploaded to uploads, if you and the blogger are also using Maven, the files are actually uploaded to
Forestblog\target\forestblog\resource\uploads\2017\8 inside, this has no effect.
But remember to copy the \target\forestblog\resource\uploads file to
In Src\main\forestblog\resource\uploads
Iv. implementation of the Code
We mainly look at the upload part of the code, the other configuration files are also affixed to it
1. SPRINGMVC Configuration (springmvc.xml part code)
1 <!--file Upload--2class= " Org.springframework.web.multipart.commons.CommonsMultipartResolver ">3 <!--set upload maximum size is 5mb-- >4 <property name= "Maxuploadsizeperfile" value= "5242880"/>5 <property name= "Defaultencoding" value= "UTF-8"/>6 <property name= "resolvelazily" value= "true"/>7 </bean>
2. JSP page (createuser.jsp part code)
1 <form action= "${pagecontext.request.contextpath}/createusersubmit.action"2 method= " Post "enctype=" Multipart/form-data ">3 <input type=" file "name=" Upload_avatar ">4 <input type= "Submit" value= "Submission" >5 </form>
3. Controller code (Add user class in Usercontroller.java)
//Add User Submissions@RequestMapping (value = "/createusersubmit", method =requestmethod.post) PublicString createusersubmit (usercustom usercustom,multipartfile Upload_avatar)throwsException {//Upload Image if(Upload_avatar.getsize ()!=0) {String NewFileName=Functions.uploadfile (Request,upload_avatar); Usercustom.setavatar (NewFileName); } usercustom.setlastloginip (FUNCTIONS.GETIPADDR (request)); Userservice.createuser (Usercustom); return"Redirect:userList.action"; }
4, upload the file code (Functions.java remember to inject)
1 //Uploading Files2 PublicString uploadfile (httpservletrequest request,multipartfile uploadfile)throwsIOException {3SimpleDateFormat SDF =NewSimpleDateFormat ("Yyyymmddhhmmssss");4String res = Sdf.format (NewDate ());5 //Uploads folder location6String RootPath =request.getservletcontext (). Getrealpath ("/resource/uploads/");7 //Original name8String OriginalFilename =uploadfile.getoriginalfilename ();9 //the new file nameTenString NewFileName = "Words superciliousness Blog" +res+originalfilename.substring (Originalfilename.lastindexof ("."))); One //Create Date folder ACalendar date =calendar.getinstance (); -File Datedirs =NewFile (Date.get (calendar.year) -+ File.separator + (Date.get (calendar.month) +1)); the //New File -File NewFile =NewFile (rootpath+file.separator+datedirs+file.separator+newfilename); - //determine if the directory in which the target file resides exists - if(!Newfile.getparentfile (). exists ()) { + //if the directory where the destination file is located does not exist, the parent directory is created - newfile.getparentfile (). Mkdirs (); + } A System.out.println (newFile); at //write in-memory data to disk - Uploadfile.transferto (newFile); - //the full URL -String fileUrl = Date.get (calendar.year) + "/" + (Date.get (calendar.month) +1) + "/" +NewFileName; - returnFileUrl; -}
Focus on uploading the file method, attribute injection here will not repeat the
The written steps are complete and clear.
Https://liuyanzhao.com/5989.html
SSM Integration-image upload function (go)