Springmvc Uploading Files asynchronously

Source: Internet
Author: User
Tags tojson

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 Code
This 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

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.