SpringMvc file upload and download, springmvc File Upload
1. First import the jar package:
2. Then, add the uploaded and downloaded configuration files in applicatinContext. xml as follows:
1 <! -- File Upload configuration --> 2 <bean id = "multipartResolver" class = "org. springframework. web. multipart. commons. CommonsMultipartResolver"> 3 <! -- The total size of the uploaded file cannot exceed kb. Note that the limit of the maxUploadSize attribute is not for a single file, but the sum of the capacity of all files --> 4 <property name = "maxUploadSize" value = "200000"/> 5 </bean> 6 7 <! -- This exception is thrown by SpringMVC when checking the information of the uploaded file, and has not yet entered the Controller Method --> 8 <bean id = "exceptionResolver" class = "org. springframework. web. servlet. handler. simpleMappingExceptionResolver "> 9 <property name =" exceptionMappings "> 10 <props> 11 <! -- When an exception occurs in MaxUploadSizeExceededException, the system automatically jumps to the error in the WebContent directory. jsp page --> 12 <prop key = "org. springframework. web. multipart. maxUploadSizeExceededException "> error </prop> 13 </props> 14 </property> 15 </bean>
3. All right, the most basic configuration is all right. The following jsp page is displayed: upload. jsp.
1 <form action = "upload. do "method =" post "enctype =" multipart/form-data "> 2 file 1: <input type = "file" name = "myfiles"/> <br/> 3. file 2: <input type = "file" name = "myfiles"/> <br/> 4. file 3: <input type = "file" name = "myfiles"/> <br/> 5 <input type = "submit" value = "Upload"> 6 </form>
4. The corresponding java code in Controller:
1 @ RequestMapping ("/upload. do ") 2 public String upload (@ RequestParam MultipartFile [] myfiles, HttpServletRequest request) throws IOException {3 for (MultipartFile file: myfiles) {4 // here MultipartFile [] indicates multi-file. if it is a single-file MultipartFile, the line is 5 if (file. isEmpty () {6 System. out. println ("file not uploaded! "); 7} 8 else {9 // get the uploaded file name 10 String fileName = file. getOriginalFilename (); 11 // obtain the address where the server project is published and run 12 String path1 = request. getSession (). getServletContext (). getRealPath ("image") + File. separator; 13 // UUID is not used here to generate a unique identifier. The date is used as the identifier 14 String path = path1 + new SimpleDateFormat ("yyyyMMddHHmmss "). format (new Date () + fileName; 15 // view the File Upload path for convenient search of 16 System. out. println (path); 17 // upload the File to path 18 File localFile = new file (path); 19 File. transferTo (localFile); 20} 21} 22 return "uploadSuccess"; 23}
In this way, you can upload the selected images on the webpage.
Download successful!
5. download. jsp: for testing, I directly pass the user name as a parameter:
<A href = "download. do? Filename=606082312271111111.jpg "> download </a>
6. Controller:
@ RequestMapping ("/download") public String download (String fileName, HttpServletRequest request, HttpServletResponse response) {response. setCharacterEncoding ("UTF-8"); response. setContentType ("multipart/form-data"); response. setHeader ("Content-Disposition", "attachment; fileName =" + fileName); try {String path = request. getSession (). getServletContext (). getRealPath ("image") + File. separator; InputSt Ream inputStream = new FileInputStream (new File (path + fileName); OutputStream OS = response. getOutputStream (); byte [] B = new byte [2048]; int length; while (length = inputStream. read (B)> 0) {OS. write (B, 0, length);} // It is mainly disabled here. OS. close (); inputStream. close ();} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} // The returned value must be noted. Otherwise, the following error occurs! // Java + getOutputStream () has already been called for this response return null ;}
Note: Reference link: http://blog.csdn.net/qq_32953079/article/details/52290208