Previously wrote a STRUTS2 implementation of the file upload, this blog post is mainly to summarize the next SPRINGMVC implementation of file upload steps. First take a look at the individual file upload, and then summarize the next multiple file uploads.
1. Environmental Preparedness
SPRINGMVC the ability to upload a file requires two Jar package support (click I download), as follows
2. Uploading of individual files2.1 Front Page
Simply write the foreground page and note that the form does not forget to write the enctype="multipart/form-data" property:
<tr> <TD>Product pictures</td> <TD><c:if Test="${itemscustom.pic!=null}"> <img src="/file/${itemscustom.pic}" width= height = /><br /> </c:if> <input type="file" name="Items_pic"/> </td></tr>
2.2 Multipart parsing of multi-component types
This means that for the above enctype="multipart/form-data" type, SPRINGMVC needs to parse the multipart type of data and configure the multipart type resolver in Springmvc.xml.
<!--file upload, you need to configure the Multipartresolver processor--<bean id= "multipartresolver" class=" Org.springframework.web.multipart.commons.CommonsMultipartResolver "> <!--set the maximum size of the onboard file to 5MB < property name="Maxuploadsize" value="5242880"/ > < property name="defaultencoding" value="Utf-8"/> </Bean>
2.3 Creating a file saved virtual directory
Before uploading a file, the first to create a virtual directory to save the file, the virtual directory will correspond to an actual directory on the disk, in practice there will be a server dedicated to storage resources, here we use local to save the file, and then map a virtual directory, Used to specify the path to get the file in the program (in fact, in the foreground page above, the src= "/file/${itemscustom.pic}" in the/file is the virtual directory).
There are two ways to create one: double-click the Tomcat server in MyEclipse, and then pop up the following box:
After setting up, save, so that the uploaded files will be saved to the directory specified in the document base, equivalent to the virtual map to the directory specified by path, the program to get this file, to be obtained from the virtual directory specified by path, that is, I/file above.
The second method is configured in the Tomcat configuration file, in fact, just in the myeclipse operation has been automatically written to this configuration file, the configuration file location in the Tomcat directory/conf/server.xml, look inside there will be more than one line:
This is just what I configured, it is automatically written to this file, so we can also directly in the file to write, it does not need to be configured in the MyEclipse.
2.4 Writing the backend controller method
Next is the focus, the front-desk file, we need to handle in the controller, and then saved to disk, but also mapped to our configured virtual path, then how to receive it? Look at the following code:
@RequestMapping("/edititemssubmit") PublicStringEdititemssubmit(model model, HttpServletRequest request, Integer ID, @Validated(value = {Validgroup1.class}) Itemscustom Itemscustom, Bindingresult bindingresult, Multipartfile items_pic)throwsException {//processing of uploaded individual imagesString originalfilename = Items_pic.getoriginalfilename ();//Original name //Upload pictures if(Items_pic! =NULL&& OriginalFilename! =NULL&& originalfilename.length () >0) {//Store The physical path of the picture, in fact, to write to the configuration file, cannot write dead in thisString Pic_path ="e:\\github\\develop\\upload\\temp\\";//New picture nameString NewFileName = uuid.randomuuid () + originalfilename.substring (originalfilename . LastIndexOf (".")); File NewFile =NewFile (Pic_path + newfilename);//New pictureItems_pic.transferto (NewFile);//write in-memory data to diskItemscustom.setpic (NewFileName);//write the new picture name into the Itemscustom}Else{//If the user does not select the picture on the upload, but also with the original pictureItemscustom temp = Itemsservice.finditemsbyid (Itemscustom.getid ()); Itemscustom.setpic (Temp.getpic ()); }//Call service to update the product information, the page needs to upload the product information to this methodItemsservice.updateitems (ID, itemscustom);return "/web-inf/jsp/success.jsp";}
First look at the formal parameters, mainly Itemscustom and multipartfile type of items_pic, I upload a picture here is a property of the Itemscustom class, so with this parameter, is to write to the class, In addition to the previous @validated note is I write SPRINGMVC check when used, and here file upload irrelevant. The Springmvc file Upload class is Multipartfile, and the name Items_pic must match the foreground name property.
The logic of uploading the file is to first determine if there is no upload file, if uploaded, then rename the file and write it to disk. If you do not upload the file, then I should still use the original file (image), because I wrote this example is to update the product information, the file upload there is no non-null validation, so wrote here else.
So the file is uploaded, this is a single file upload.
3. Uploading Multiple Files
Multiple file uploads and a single file upload the same principle, but in the details will be a little different, to me a summary. The first thing to note on the foreground page is that the name attribute must be the same, namely:
type="file" name="items_pic"type="file" name="items_pic"/>
Then the background receives the formal parameter also to change, as follows:
@RequestMapping("/edititemssubmit") PublicStringEdititemssubmit(model model, HttpServletRequest request, Integer ID, @Validated(value = {Validgroup1.class}) Itemscustom Itemscustom, Bindingresult Bindingresult,@RequestParamMultipartfile[] items_pic)throwsException {//multiple pictures, do not save the database, print a bit here for(Multipartfile myfile:items_pic) {if(Myfile.isempty ()) {System.out.println ("File not uploaded"); }Else{System.out.println ("File length:"+ myfile.getsize ()); System.out.println ("File type:"+ Myfile.getcontenttype ()); System.out.println ("file name:"+ Myfile.getname ()); System.out.println ("file formerly known as:"+ Myfile.getoriginalfilename ()); System.out.println ("========================================");//write to disk, identical to the single file upload aboveString originalfilename = Myfile.getoriginalfilename (); String Pic_path ="e:\\github\\develop\\upload\\temp\\"; String NewFileName = uuid.randomuuid () + originalfilename.substring (originalfilename . LastIndexOf (".")); File NewFile =NewFile (Pic_path + newfilename); Myfile.transferto (NewFile); } }return "/web-inf/jsp/success.jsp";}
As above, the parameter becomes an array type, and the @requestparam annotation is preceded by the line. And then, it is to iterate over the array, which is exactly the same as uploading the individual files inside the loop. Look at the print results:
You can see that two files have been successfully received, to this point, the multi-file upload success. The file upload function about SPRINGMVC is summarized here.
Related reading: http://blog.csdn.net/column/details/spring-mvc.html
Learning Note Source: Https://github.com/eson15/SpringMVC_Study
-Willing to share and progress together!
--My Blog home: http://blog.csdn.net/eson_15
"Springmvc Learning 08" springmvc Implement File Upload