Ajax asynchronous upload image &springmvc background code

Source: Internet
Author: User

function Uploadpic () {var options = {url: "/upload/updatepic.action", type: "Post", DataType: "JSON", Success:function (DA TA) {$ ("#allUrl"). attr ("src", Data.url); $ ("#imgUrl"). Val (Data.url);}; $ ("#jvForm"). Ajaxsubmit (options);}

  

 PackageTop.dgoyou.core.controller;ImportJava.io.File;ImportJava.util.UUID;ImportJavax.servlet.http.HttpServletResponse;Importorg.apache.commons.io.FilenameUtils;ImportOrg.json.JSONObject;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.multipart.MultipartFile, @Controller @requestmapping ("/upload") Public classUploadcontroller {//Uploading pictures asynchronously@RequestMapping ("/updatepic")     Public voidUpdatepic (multipartfile pic,httpservletresponse response)throwsexception{//get file Original nameString filename =Pic.getoriginalfilename (); //use random string + source picture extension to make a new picture name to prevent the duplicate imageString Newfilename=uuid.randomuuid (). toString (). ReplaceAll ("-", "") + "." +filenameutils.getextension (filename); //Save the file to the picture server ("E:\testpic" the image virtual path can be converted to fastdfs| later | Cloud)Pic.transferto (NewFile ("E:\testpic")); //Jsonobject is a simple to go JSON tool class, but can only be transferred to string, Double,integer,long and other simple types, can not go Pojo, list, set, map, etc.Jsonobject Jsonobject =NewJsonobject (); Jsonobject.put ("url", "/pic/" +newfilename); Response.setcontenttype ("Application/json;charset=utf-8");
Response to the front page response.getwriter (). Write (jsonobject.tostring ()); }}

Ajax asynchronous upload image &springmvc background code

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.