Not much to say, code:
Spring-config. XML
<!--spring can automatically scan Base-pack The following package or the Java file under the child package, if you scan to a class that has related annotations of spring, Register these classes as Spring bean --<context:component-scan base-package = " Org.fkit.controller "></context:component-scan> <!--View Resolver--<bean id=" Viewresolver "class = "Org.springframework.web.servlet.view.InternalResourceViewResolver" > < Property name= "prefix" value= "/web-inf/content/"/> <property name= "suffix" value= ". jsp"/> </bean> <bean id= "Multipartresolver" class = " Org.springframework.web.multipart.commons.CommonsMultipartResolver "> <!--maximum upload file (bytes)--<prope Rty name= "maxuploadsize" value= "10485760"/> <!--request encoding format--<property name= "Defaultencoding" Val Ue= "UTF-8" ></property> </bean>
PackageOrg.fkit.domain;Importjava.io.Serializable;ImportOrg.springframework.web.multipart.MultipartFile;Domain objects, implementing serialization interfaces, user classes Public classUserImplementsserializable{PrivateString username; Privatemultipartfile image; PublicUser () {Super(); //TODO auto-generated Constructor stub } PublicString GetUserName () {returnusername; } Public voidSetusername (String username) { This. Username =username; } Publicmultipartfile getImage () {returnimage; } Public voidsetimage (multipartfile image) { This. Image =image; }}
PackageOrg.fkit.controller;ImportJava.io.File;Importjava.io.IOException;ImportJava.lang.reflect.Method;Importjavax.servlet.http.HttpServletRequest;Importorg.apache.commons.io.FileUtils;ImportOrg.fkit.domain.User;Importorg.springframework.http.HttpHeaders;ImportOrg.springframework.http.HttpStatus;ImportOrg.springframework.http.MediaType;Importorg.springframework.http.ResponseEntity;ImportOrg.springframework.stereotype.Controller;ImportOrg.springframework.ui.Model;ImportOrg.springframework.web.bind.annotation.ModelAttribute;Importorg.springframework.web.bind.annotation.PathVariable;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestMethod;ImportOrg.springframework.web.bind.annotation.RequestParam;Importorg.springframework.web.multipart.MultipartFile; @Controller Public classfileuploadcontroller{@RequestMapping (value= "/{forname}") Publicstring forname (@PathVariable string forname) {//Dynamic Jump Page returnforname; }@RequestMapping (Value= "/register") PublicString Register (httpservletrequest request, @ModelAttribute User User,model Model)throwsillegalstateexception, ioexception{System.out.println (User.getusername ()); if(!user.getimage (). IsEmpty ()) { //Upload file pathString Path=request.getservletcontext (). Getrealpath ("/images/"); //file nameString filename=user.getimage (). Getoriginalfilename (); File FilePath=NewFile (path,filename); //determine if the path exists, or if it does not exist, create a if(!Filepath.getparentfile (). exists ()) {Filepath.getparentfile (). Mkdirs (); } //Save the uploaded file to a destination folderUser.getimage (). TransferTo (NewFile (path+file.separator+fileName)); //add a user to the modelModel.addattribute ("User", user); return"UserInfo"; }Else{ return"Errors"; }} @RequestMapping (Value= "/download")
Publicresponseentity<byte[]> Download (httpservletrequest request, @RequestParam ("filename") String Filename,model Model)throwsexception{//path to the download fileString Path=request.getservletcontext (). Getrealpath ("/images/"); File File=NewFile (path+file.separator+filename); Httpheaders Headers=Newhttpheaders (); //download file display full name, and solve the problem of Chinese garbledString downloadfilename=NewString (Filename.getbytes ("UTF-8"), "Iso-8859-1"); //notifies the browser to open a picture with attachment (download mode)Headers.setcontentdispositionformdata ("Attachment", DownloadFileName); //Application/octet-streama: Binary stream data (most common file download)Headers.setcontenttype (Mediatype.application_octet_stream); return Newresponseentity<byte[]>(Fileutils.readfiletobytearray (file), headers,httpstatus.created); }}
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding= "UTF-8"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >pageencoding= "UTF-8"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >${requestscope.user.image.originalfilename}</a></body>Springmvc upload download is quite simple, very suitable
Springmvc File Upload Download