1. Image Server Tomcat settings tomcat/conf/web.xml content changes, Tomcat is set to read and write
<servlet> <servlet-name>default</servlet-name> <servlet-class> org.apache.catalina.servlets.defaultservlet</servlet-class> <init-param> <param-name >debug</param-name> < param-value>0</param-value> </init-param> <init-param> <param-name>readonly</param-name> <param-value>false</param-value> </init-param> <init-param> < param-name>listings</param-name> <param-value>false</param-value> </ Init-param> <load-on-startup>1</load-on-startup> </servlet>
2. The foreground page provides a form for uploading pictures and Ajax-asynchronous logic
<form id= "Jvform" action= "add.do" method= "post" enctype= "Multipart/form-data" ... <input type= "hidden" name= "Imgurl" id= "Imgurl"/> <input type= "file" Name= "Uploadpic" onchange= "UploadP IC () "/></form>
<script type= "Text/javascript" scr= "/res/common/js/jquery.form.js" ></script><script type= "Text/javascript" > //use Jquery.form.js to upload images asynchronously and combine <form> forms function uploadfic () { var options = { //Request Path url = "/upload/uploadpic.do", dataType = "JSON", type = "POST", beforesubmit : function (formdata,jqform,options) { //Judging if it is a picture var f = jqform[0];//turn JqForm into DOM object var v = f.logopic.value;//gets the value of name Logopic in the DOM object //get the extension and turn it into lowercase var ext = v.substring (v.length-3). ToLowerCase (); //extension jpg gif bmp png if (ext != "JPG" && ext != "gif" && ext != "BMP" && ext != "PNG") { &Nbsp; alert ("Only allow images to be uploaded!"); return false; } //Validate submitted Form return true; }, success : function (data) { //Processing Results //set relative paths to hidden fields, at commit $ ("#imgUrl"). Val (Data.path); //set the full path to the IMG tag, display image $ ("#allImgUrl). attr (" src ", Data.url); } } $ ("#jvForm"). Ajaxsubmit (Options); }</script>
3.springmvc-back.xml Configuring picture Converters
<!--upload picture converter--><bean id= "Multipartresolver" class= " Org.springframework.web.multipart.commons.CommonsMultipartResolver > <!--set the maximum size of the upload file to 1MB-<p Roperty name= "maxuploadsize" value= "1048576"/></bean>
4.UploadController uploading images to another server
The required jar packages are as follows
Commons-io-1.3.2.jar
Jersey-client-1.18.1.jar
Jersey-core-1.18.1.jar
import java.text.simpledateformat;import java.util.date;import java.util.random;import javax.servlet.http.httpservletresponse;import org.apache.commons.io.filenameutils;import org.json.jsonobject;import org.springframework.stereotype.controller;import org.springframework.web.bind.annotation.requestmapping;import org.springframework.web.bind.annotation.requestparam;import org.springframework.web.multipart.multipartfile;import cn.itcast.core.web.constants;import cn.itcast.core.web.responseutils;import com.sun.jersey.api.client.client;import Com.sun.jersey.api.client.WebResource, @Controllerpublic class uploadcontroller {@RequestMapping (value= "/upload/uploadpic.do") Public void uploadpic (@RequestParam (required=false) multipartfile Pic,httpservletresponse response) {//@RequestParam (Required=false) multipartfile pic receive foreground form Name= Pic File, Required=false indicates that the file can be empty//response represent SPRINGMVC placeThe return value of the asynchronous upload//Set the picture to the specified server//file extension string ext = filenameutils.getextension (Pic.getoriginalfilename ()) ;//Picture name = date + random number + Picture original name String format = new simpledateformat ("Yyyymmddhhmmsssss"). Format (new date ()); Random r = new random (); for (int i=0;i<3;i++) {format+=r.nextint (10);} Jersey Toolkit sends pictures to the specified server//instantiate jerseyclient client = new client ();//Save Database Pathstring path = "/upload/" +format+ "." +ext;//picture on the image server request address String url = constants.image_url+path;//jersey set the request path webresource Resource = client.resource (URL);//jersey Write Data Try {resource.put (String.class,pic.getbytes ());} catch (exception e) {// TODO Auto-generated catch Blocke.printstacktrace ();} The AJAX callback function receives the data jsonobject jo = new jsonobject (); Jo.put ("url", url); Jo.put ("Path", path); Responseutils.renderjson (Response, jo.tostring ());}
5.ajax Callback Data format
/** * Asynchronously put back data format processing * @author Pengya * */public class Responseutils {//Send content public static void render (HttpServletResponse resp onse,string contenttype,string text) {response.setcontenttype (contentType); try {response.getwriter (). write (text);} catch (IOException e) {e.printstacktrace ();}} Send jsonpublic static void Renderjson (HttpServletResponse response,string text) {Render (response, "Application/json; Charset=utf-8 ", text);} Send xmlpublic static void Renderxml (HttpServletResponse response,string text) {Render (response, "text/xml;charset= UTF-8 ", text);} Send textpublic static void RenderText (HttpServletResponse response,string text) {Render (response, "Text/plain;charset =utf-8 ", text);}}
Build a tomcat, as a picture server, upload images asynchronously