1. Foreground Interface Code
<form id= "Jvform" action= "add.do" method= "post" enctype= "Multipart/form-data" ><table><tr><td Width= "20%" class= "Pn-flabel pn-flabel-h" ></td><td width= "80%" class= "pn-fcontent" ><input type=" hidden "name=" Imgurl "id=" path "/><input type=" file " Onchange= "Uploadpic ()" Name= "pic" id= "Logopic"/></td></tr></table></form>
2. Front-desk JS code
<script type= "Text/javascript" >//upload picture function uploadpic () {//define parameters var options = {url : "/upload/uploadpic.do",datatype : "JSON",type : "POST", Beforesubmit : function (formdata,jqform,options) {// determine if the picture// convert Jqform to DOM object var f = jqform[0];// gets the value of name Logopic in the DOM object var v = f.logopic.value;// gets the extension and turns 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") {alert ("Only allow images to be uploaded!"); Return false;} Verify submitted Form return true;},success : function (data) {// echo Picture contents $ ("#allImgUrl"). attr ("src", data.url); $ ("#path"). Val (Data.path);}};/ / jquery.form Use $ ("#jvForm"). Ajaxsubmit (options);} </script>
3. Configure the picture converter in spring
<!--upload picture converter--><bean id= "Multipartresolver" class= " Org.springframework.web.multipart.commons.CommonsMultipartResolver > <!--Setting the maximum size of the uploaded file is 1MB--< Property Name= "Maxuploadsize" value= "1048576"/></bean>
4. Creating Uploadcontroller and Tool classes
package cn.itcast.core.controller.admin;import java.io.ioexception;import java.text.dateformat; 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 com.sun.jersey.api.client.client;import com.sun.jersey.api.client.webresource;import cn.itcast.common.web.responseutils;import Cn.itcast.core.web.Constants, @Controllerpublic class UploadController {// Upload Image @requestmapping (value = "/upload/uploadpic.do") Public void uploadpic (@RequestParam ( Required = false) multipartfile pic, httpservletresponse response) {// extension string ext = filenameutils.getextension ( Pic.getoriginalfilename ());// picture name generation policy Dateformat df = new simpledateformat (" Yyyymmddhhmmsssss ");// Picture name Part String format = df.format (new date ());// Random three-digit random r = new random ();// n 1000 0-999 99for (int i = 0; i < 3; i++) {format += r.nextint (10);} Instantiate a jerseyclient client = new client ();// Save the database string path = "upload/" + format + "." + ext;// What is the request path for the other server? string url = constants.image_url + path;// set the request path webresource resource = client.resource (URL);// send start post get puttry {resource.put (String.class, Pic.getbytes ()); } catch (ioexception e) {e.printstacktrAce ();} returns two paths Jsonobject jsonobject = new jsonobject (); Jsonobject.put ("url", url); Jsonobject.put ("path", path); Responseutils.renderjson (Response, jsonobject.tostring ());}}
package cn.itcast.core.web;public interface constants {/** * Image Server */public static final String IMAGE_URL = http ://localhost:8088/babasportproject_imageservice/";}
package cn.itcast.common.web;import java.io.ioexception;import javax.servlet.http.httpservletresponse;public class responseutils {// Send content public Static void render (httpservletresponse response, string contenttype, string text) {response.setcontenttype (ContentType); Try {response.getwriter (). write (text);} catch (ioexception e) {e.printstacktrace ();}} is sending 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);}}
5. Build another tomcat as a picture server
Picture server tomcat settings Tomcat/conf/web.xml content changes
<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>
Create Javaweb project on 6.Eclipse, name Babasprotproject_imageservice
7. Modify the Tomcat port number of the image server
8. Create the Upload folder under the WebApp directory of the Babasprotproject_imageservice Project
======================
Whether the server test was started successfully
======================
This article is from the Java Development Engineer blog, so be sure to keep this source http://robertohuang.blog.51cto.com/10343725/1846554
Using jersey to build picture server has echo picture function