Web-side image upload

Source: Internet
Author: User

Plugin Required: Ajaxfileupload.js Commons-fileupload-1.3.1.jar
Web-side:
  

<form id="Saveform"Method="Post"Enctype="Multipart/form-data"><label> Advertising Pictures: </label><input type="file"Id="Image1"Name="Image1"data-options="required:true"/> ".. /.. /imgsrc/aaaaaa.jpg"Id="img"Width="100px", height="100px"/> <input type="Button"Id="Sub"Value="Upload"/></form>
<script type="Text/javascript">$ (function () {$ ("#sub"). Click (function () {$.ajaxfileupload ({URL:'upload.do', Secureuri:false, Fileelementid:'Image1',//ID of the file tagDataType:'JSON',//type of return datasuccess:function (data) {$ ("#img"). attr ("src",".. /.. /imgsrc/"+data);    },          }); });});</script>

Java background:
  

@RequestMapping ("Upload") @ResponseBody PublicString Upload (@RequestParam ("Image1") Multipartfile file,httpservletrequest req) {//String Savefilepath = Req.getservletcontext (). Getrealpath ("/web-inf/upload");String savefilepath=Constant.savepath; Try {            if(!File.isempty ()) {                 //get the file name of the pictureString FileName =File.getoriginalfilename (); //get the name extension of a pictureString ExtensionName =fileName. Substring (Filename.lastindexof (".") +1); //new picture file name = Get timestamp + "." Picture extensionString NewFileName = string.valueof (System.currenttimemillis ()) +"."+ExtensionName;                     Imgutils.savefile (newfilename, file, Savefilepath); returnNewFileName; }Else{                 return "Exception"; }    } Catch(Exception e) {e.printstacktrace (); return "Exception"; }     }
     Public Static voidSaveFile (String newfilename, Multipartfile filedata,string savefilepath) {//TODO auto-generated Method Stub//Get server picture storage path based on configuration file        /*Build File directory*/File Filedir=NewFile (Savefilepath); if(!filedir.exists ())        {Filedir.mkdirs (); }        Try{FileOutputStream out=NewFileOutputStream (Savefilepath +"\\"+newfilename); //Write File             out. Write (Filedata.getbytes ());  out. Flush ();  out. Close (); } Catch(Exception e) {e.printstacktrace (); }     }

Tomcat:
  

      "webapps" autodeploy="true" name="  localhost" unpackwars="true">        <context docbase="D:\upload" path="/imgsrc"/>

Web-side image upload

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.