Upload files using Spring MVC and Spring MVC

Source: Internet
Author: User

Upload files using Spring MVC and Spring MVC

1. applicationContext. xml

1 <bean id="multipartResolver"2         class="org.springframework.web.multipart.commons.CommonsMultipartResolver">3         <property name="maxUploadSize" value="104857600" />4         <property name="maxInMemorySize" value="4096" />5         <property name="defaultEncoding" value="UTF-8"></property>6     </bean>

 

2. uploadFile. jsp

1 <% @ page language = "java" contentType = "text/html; charset = UTF-8" 2 pageEncoding = "UTF-8" %> 3 <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" http://www.w3.org/TR/html4/loose.dtd "> 4 

3. UploadFileAction. java

1 package com. taozhiye. controller; 2 3 import java. io. file; 4 import java. io. fileNotFoundException; 5 import java. io. fileOutputStream; 6 import java. io. IOException; 7 import java. io. inputStream; 8 import java. io. outputStream; 9 import java. util. date; 10 import java. util. iterator; 11 12 import javax. servlet. http. httpServletRequest; 13 14 import org. springframework. stereotype. controller; 15 import org. sp Ringframework. web. bind. annotation. requestMapping; 16 import org. springframework. web. bind. annotation. requestParam; 17 import org. springframework. web. bind. annotation. responseBody; 18 import org. springframework. web. multipart. multipartFile; 19 import org. springframework. web. multipart. multipartHttpServletRequest; 20 import org. springframework. web. multipart. commons. commonsMultipartFile; 21 import org. springfr Amework. web. multipart. commons. commonsMultipartResolver; 22 23 @ Controller24 public class UploadFileAction {25 26 27 @ RequestMapping ("uploadFile") 28 public String goFile () {29 return "uploadFile "; 30} 31 32/* 33 * methods for uploading files using spring 34 */35 @ RequestMapping ("springUpload") 36 @ ResponseBody37 public String springUpload (HttpServletRequest request) 38 throws IllegalStateException, IOException {39 long startT Ime = System. currentTimeMillis (); 40 // initialize the current context to CommonsMutipartResolver (multi-part parser) 41 CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver (42 request. getSession (). getServletContext (); 43 // check whether the form contains enctype = "multipart/form-data" 44 if (multipartResolver. isMultipart (request) {45 // convert the request to a multipart request46 MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) r Equest; 47 // get all file names in multiRequest. 48 Iterator iter = multiRequest. getFileNames (); 49 while (iter. hasNext () {50 // traverse all files at a time 51 MultipartFile file = multiRequest. getFile (iter. next () 52. toString (); 53 if (file! = Null) {54 String path = "E:/springUpload" 55 + file. getOriginalFilename (); 56 // upload 57 files. transferTo (new File (path); 58} 59 60} 61 62} 63 long endTime = System. currentTimeMillis (); 64 System. out. println ("method running time:" + String. valueOf (endTime-startTime) 65 + "ms"); 66 return "success"; 67} 68} 69

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.