Learn together SpringMVC of file uploads

Outline

In the Web system development process, file upload function is universal, this paper with a simple small example, explain the use of the file upload SpringMVC, only to learn to share, if any inadequacies, please correct me.

Upload file dependencies

As shown below, the package file upload dependent jar two main reasons:

commons-fileupload-1.4.jar
commons-io-2.6.jar

SpringMVC support file upload procedure

1. configuration supports file uploads Bean

Limit the size of the default configuration of the main encoding, and upload files as shown below:

1  <! - File Upload Support -> 
2       < the bean ID = "the MultipartResolver" class = "org.springframework.web.multipart.commons.CommonsMultipartResolver" > 
. 3           < Property name = "defaultEncoding" value = "UTF-. 8" > </ Property > 
. 4           <-! set maximum upload content, in units of byte -> 
. 5           < Property name = "maxUploadSize" value = "1024000" > </ Property > 
. 6       </ the bean >

2. In the processing method, the reception MultipartFile

As follows: MultipartFile indicates a disk is stored in memory or the contents of the temporary file type. You can acquire the corresponding input stream (InputStream), and then acquires the content byte array input stream.

1      / * 
2       * upload page display, GET access
 . 3       * @return 
. 4       * / 
. 5      @RequestMapping (value = "upload", Method = RequestMethod.GET)
 . 6      public ModelAndView upload () {
 . 7          System.out.println ( " It is called the upload " );
 . 8          ModelAndView MAV = new new ModelAndView (" upload " );
 . 9          return MAV;
 10      }
 . 11      
12 is      / ** 
13 is       * used to upload files
 14       * @return 
15       * @throws IOException 
16      */
17     @RequestMapping(value="upload",method=RequestMethod.POST)
18     public ModelAndView upload(String desc,@RequestParam("file") MultipartFile file) throws IOException{
19         System.out.println("upload被调用了2");
20         String fileName=file.getOriginalFilename();
21         System.out.println("上传的源文件名:"+fileName);
22         System.out.println("文件描述:"+desc);
23         InputStream input= file.getInputStream();
24         OutputStream output=new FileOutputStream("D:\\upload\\"+fileName);
25         byte[] bs=new byte[1024];
26         int len=-1;
27         while((len=input.read(bs))!=-1){
28             output.write(bs,0,len);
29         }
30         output.close();
31         input.close();
32         ModelAndView mav=new ModelAndView("success");
33         return mav;
34     }
35     

3. The front end (Jsp) page

As follows: method = "post" enctype = "multipart / form-data" is the key

1 <form action="upload" method="post" enctype="multipart/form-data">
2     文件:<input type="file" name="file"> <br />
3     描述:<input type="text" name="desc" /> <br />
4     <input type="submit" value="上传"> 
5 </form>

4. Run the test

As shown below, the log output as follows:

upload is called the 
upload is called the 2 
uploaded source file name: readme.txt 
File Description: 1235 
upload was called

Remark

We no longer young, parents are old, the children have grown up, eat bitter, carry endless responsibilities. To live yourself driving through, to their hard-earned money.
We no longer young, eat the food, and sleep the sleep of the Laugh, the rest on the rest.

Guess you like

Origin www.cnblogs.com/hsiang/p/11443405.html