Spring MVC uploads files

Source: Internet
Author: User

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 import javax.servlet.http.httpservletrequest;13 Import ORG.SPRINGFRAMEWORK.STEREOTYPE.CONTROLLER;15 Import org.springframework.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; Import org.springframework.web.multipart.multiparthttpservletrequest;20 Import ORG.SPRINGFRAMEWORK.WEB.MULTIPART.COMMONS.COMMONSMULTIPARTFILE;21 Import Org.springframework.web.multipart.commons.commonsmultipartresolver;22 @Controller24 public class         uploadfileaction {@RequestMapping ("UploadFile"), Gofile () {29return "UploadFile"; 30}31 32/*33 * Use spring to upload a file method */35 @RequestMapping ("Springupload") 36 @ResponseBody37 public String springupload (HttpServletRequest request) throws IllegalStateException , IOException {startTime = System.currenttimemillis (); 40//Initializes the current context to the Commonsmutipartresolver (multi-part Sub-parser) commonsmultipartresolver multipartresolver = new Commonsmultipartresolver (Request.getse Ssion (). Getservletcontext ()); 43//Check if the form has enctype= "Multipart/form-data" if (multipartresolver.ismulti Part (Request)) {45//change request to multipart request46 multiparthttpservletrequest multirequest = (Multipart HttpServletRequest) request;47//Get all the filenames in multirequest Iterator iter = Multirequest.getfilenam Es (); Iter.hasnext () {50//traverse all files at once multipartfile file = Multireq Uest.getfile (Iter.next (). toString ()); if (file! = null) {si-String path = "E:/spr Ingupload "+ file.getoriginalfilename (); 56//Upload/F Ile.transferto (path),}59}61}63 long endTime = System.curr Enttimemillis (); System.out.println ("Method run Time:" + string.valueof (endtime-starttime) + "MS"); 6          6 return "Success"; 67}68}69
2017-04-10

Spring MVC uploads files

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.