Content from: Spring mvc upload Download
The following example:
Page:
Xml:
<?XML version= "1.0" encoding= "UTF-8"?><Web-appversion= "3.0"xmlns= "Http://java.sun.com/xml/ns/javaee"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <servlet> <Servlet-name>Etoak</Servlet-name> <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class> </servlet> <servlet-mapping> <Servlet-name>Etoak</Servlet-name> <Url-pattern>*.do</Url-pattern> </servlet-mapping> <Display-name></Display-name> <welcome-file-list> <Welcome-file>index.jsp</Welcome-file> </welcome-file-list></Web-app>
Etoakk-servlet.xml:
<Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.2.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/con Text/spring-context-3.2.xsd "> <!--Request Resolver character request resolver handlermapping byte request resolver Multipartresolver Commonsmultipartreso Lver when the server side uses Commons-fileupload to process the upload request, the parser is used Standardservletmultipartresolver When using Smartupload to process upload requests, use this parser note: When registering the upload Request parser, the name (ID) value of the parser must be: Multipartresolver Interface name-First letter lowercase - <BeanID= "Multipartresolver"class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver"></Bean> <Context:component-scanBase-package= "com"/></Beans>
Filecontroller.java:
@Controller Public classFilecontroller {@RequestMapping ("/upload") PublicString Upload (user user, httpservletrequest request)throwsexception{multipartfile myfile=User.getmyfile (); String filename=Myfile.getoriginalfilename (); String ContentType=Myfile.getcontenttype (); LongSize =myfile.getsize (); //get an input stream from the upload fileInputStream is =Myfile.getinputstream (); //Navigate to file directory Request.session.ServletContext.getRealPath ("/file")String path = Request.getsession (). Getservletcontext (). Getrealpath ("/file"); String NewFileName=NewUuidgenerator (). Generate (). toString () +filename.substring (Filename.lastindexof (".")); File File=NewFile (path+ "/" +newfilename); OutputStream OS=Newfileoutputstream (file); intLen; byte[] data =New byte[1024]; while(Len=is.read (data))!=-1) os.write (data,0, Len); Is.close (); Os.close (); return"Redirect:success.jsp"; } @RequestMapping ("/download") Public voidDownload (String filename,httpservletrequest request,httpservletresponse response)throwsexception{System.out.println ("File name" +filename); String Path= Request.getsession (). Getservletcontext (). Getrealpath ("/file"); File File=NewFile (path+ "/" +filename); Response.setcontenttype ("Multipart/form-data"); Response.setheader ("Content-disposition", "attachment;filename=" +filename); InputStream is=Newfileinputstream (file); OutputStream OS=Response.getoutputstream (); intLen; byte[] data =New byte[1024]; while(Len=is.read (data))!=-1) os.write (data,0, Len); Is.close (); Os.close (); }}
User.java:
PackageCom.etoak.bean;ImportOrg.springframework.web.multipart.MultipartFile; Public classUser {/**myfile File * Spring-mvc How to encapsulate a file type Object * * struts1-formfile * struts2-file string String * Spring -mvc-multipartfile interface, Commonsmultipartfile*/ PrivateMultipartfile myfile; Publicmultipartfile Getmyfile () {returnmyfile; } Public voidsetmyfile (Multipartfile myfile) { This. MyFile =myfile; }}
Spring Framework Learning (8) Spring MVC upload Download