1 Importing related jar packages via commons-fileupload
Commons-fileupload,commons-io
2 Configuring the SPRINGMVC configuration resolver
Mvc:
class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver" > <property name= " Defaultencoding "value=" Utf-8 "></property> <property name=" maxuploadsize "value=" 10485760000 "> </property> <property name= "maxinmemorysize" value= "40960" ></property> </bean>
3 JSP page
<%@ page language= "Java"Import= "java.util.*" pageencoding= "UTF-8"%><%String Path=Request.getcontextpath (); String BasePath= Request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >File:<input type= "File" name= "file"/> <input type= "Submit" value= "Upload"/> </form> </body>4 controller Code
@Controller Public classFileuploadcontroller {@RequestMapping ("/upload") PublicString FileUpload (@RequestParam ("file") Commonsmultipartfile file,httpservletrequest req)throwsioexception{//Get file name//File.getoriginalfilename (); //get the path to the uploaded fileString Path = Req.getrealpath ("/fileupload"); InputStream is=File.getinputstream (); OutputStream OS=NewFileOutputStream (NewFile (Path,file.getoriginalfilename ())); intLen = 0; byte[] buffer =New byte[400]; while(Len=is.read (buffer))!=-1) {os.write (buffer,0, Len); Os.close (); Is.close (); } return"/index.jsp"; }}
Bulk upload of code
@RequestMapping ("/batch") PublicString FileUpload (@RequestParam ("File") Commonsmultipartfile file[], httpservletrequest req)throwsioexception{//Get file name//File.getoriginalfilename (); //get the path to the uploaded fileString Path = Req.getrealpath ("/fileupload"); for(inti = 0; i < file.length; i++) {InputStream is=File[i].getinputstream (); OutputStream OS=NewFileOutputStream (NewFile (Path,file[i].getoriginalfilename ())); intLen = 0; byte[] buffer =New byte[400]; while(Len=is.read (buffer))!=-1) os.write (buffer,0, Len); Os.close (); Is.close (); } return"/index.jsp"; }
Springmvc implementing file Uploads