Pre-conditions
Note: The bean ID name cannot be changed
<!--upload file interception, set maximum upload file size 10m=10*1024*1024 (B) =10485760 bytes-- <bean id= "Multipartresolver" class= " Org.springframework.web.multipart.commons.CommonsMultipartResolver "> <property name=" Maxuploadsize " Value= "10485760"/> <!--<property name= "resolvelazily" value= "true"/>--> </bean>
front page upload.jsp
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >Background Controller CodeThis is submitted using the traditional form form, that is, on the foreground page, click the ' Upload ' button to upload the file form,
and must be specified on the form labelenctype= "Multipart/form-data"
@RequestMapping (value = "Getparamfromfile", method = Requestmethod.post) @ResponseBodypublic map<string, object> Getparamfromfile (@RequestParam ("UploadFile") multipartfile uploadfile,httpservletrequest request, HttpServletResponse response) {map<string, object> parammap = new linkedhashmap<string, object> (); FileInputStream InputStream = null;try{string filename = uploadfile.getname (); String originalfilename = Uploadfile.getoriginalfilename (); SYSTEM.OUT.PRINTLN ("filename =" + filename); System.out.println ("originalfilename =" + originalfilename); InputStream = (FileInputStream) Uploadfile.getinputstream ();} catch (IOException E1) {parammap.put ("message", "Upload file Error"); return parammap;} String FilePath = "e:\\work\\svn\\openeap\\code\\openeap\\src\\main\\webapp\\template\\ Beijing housing Leasing contract 5.xml"; Parammap = Importfile.getparamfromfile (filePath);p arammap = Importfile.getparamfromstream (InputStream); Iterator it = Parammap.entryset (). Iterator (); while (It.haSnext ()) {Map.entry E = (map.entry) it.next (); SYSTEM.OUT.PRINTLN ("parameter key:" + e.getkey () + "; Parameter Value: "+ e.getvalue ()); } System.out.println ("Generated JSON is:" + jsonmapper.getinstance (). ToJson (Parammap)); return parammap;} The use of the JQuery form plug-in background @RequestMapping (value = "Getparamfromfileforajax", method = Requestmethod.post) @ResponseBodyp Ublic map<string, object> getparamfromfileforajax (httpservletrequest request,string fileName) {Map<String, object> Parammap = new linkedhashmap<string, object> (); FileInputStream InputStream = null;//Turn request strong into multipart Request object multiparthttpservletrequest Multiparthttpservletrequest = ( Multiparthttpservletrequest) request;//Gets the file object according to the file name Commonsmultipartfile Commonsmultipartfile = ( Commonsmultipartfile) multiparthttpservletrequest.getfile (filename); try{string filename = Commonsmultipartfile.getname (); String originalfilename = Commonsmultipartfile.getoriginalfilename (); System.out.println("filename =" + filename); System.out.println ("originalfilename =" + originalfilename); InputStream = (FileInputStream) Commonsmultipartfile.getinputstream ();} catch (IOException E1) {parammap.put ("message", "Upload file Error"); return parammap;} String FilePath = "e:\\work\\svn\\openeap\\code\\openeap\\src\\main\\webapp\\template\\ Beijing housing Leasing contract 5.xml"; Parammap = Importfile.getparamfromfile (filePath);p arammap = Importfile.getparamfromstream (InputStream); Iterator it = Parammap.entryset (). Iterator (); while (It.hasnext ()) {Map.entry E = (map.entry) it.next (); SYSTEM.OUT.PRINTLN ("parameter key:" + e.getkey () + "; Parameter Value: "+ e.getvalue ()); } System.out.println ("Generated JSON is:" + jsonmapper.getinstance (). ToJson (Parammap)); return parammap;}
There are two ways of uploading files asynchronously:
- Method One: Use the Formdata object
- Method Two: Use Jquery.form.js
Reference articles:
http://www.cnblogs.com/zhuxiaojie/p/4783939.html#autoid-1-0-0
Springmvc Uploading Files asynchronously