Perpetual constant opening, adding dependencies
<!--https://mvnrepository.com/artifact/com.sun.jersey/jersey-client -<Dependency> <groupId>Com.sun.jersey</groupId> <Artifactid>Jersey-client</Artifactid> <version>1.19.4</version></Dependency><!--Https://mvnrepository.com/artifact/com.sun.jersey/jersey-core -<Dependency> <groupId>Com.sun.jersey</groupId> <Artifactid>Jersey-core</Artifactid> <version>1.19.4</version></Dependency>
1, add a button to change the event in the added page, because it is uploaded, so the form remembers to add attributes
Enctype= "Multipart/form-data"
<TR> <TDwidth= "20%"class= "Pn-flabel pn-flabel-h"> <spanclass= "Pn-frequired">*</span>Upload a product image (90x150 size):</TD> <TDwidth= "80%"class= "Pn-fcontent">Note: This size picture must be 90x150. </TD> </TR> <TR> <TDwidth= "20%"class= "Pn-flabel pn-flabel-h"></TD> <TDwidth= "80%"class= "Pn-fcontent">
<imgwidth= "+"Height= "+"ID= "Allimgurl"/>
<!--picture save path (just a file name)--<inputtype= "hidden"name= "Imgurl"ID= "path"/>
< add events below!----<inputtype= "File"onchange= "Uploadpic ()"name= "Pic"/> </TD> </TR>
2, the content of the event is, jump to
"/upload/uploadpic.do"
<type= "Text/javascript"> //Upload picture function uploadpic () { alert ("111") //define parameter var options = { URL: "/upload/uploadpic.do", dataType: "JSON", Type: "Post", success:function (data) { ///Get two paths to the database, Id=path is the input hidden field, and when the form commits, the picture name is saved, id= Allimgurl is to set the picture display, when the upload is successful, the picture will be displayed here //url //path $ ("#allImgUrl"). attr ("src", data.url); $ ("#path"). Val (Data.path); } }; Jquery.form Use $ ("#jvForm"). Ajaxsubmit (options); }
3. Write Controller
/*** Upload pictures, merchandise, brand, Product introduction*/@Controller Public classUploadcontroller {/*** Image upload *@parampic*/@RequestMapping ("/upload/uploadpic.do") //Required=false said no upload on the error Public voidUploadpic (@RequestParam (required =false) multipartfile pic, httpservletresponse response) {//Image name generation policyDateFormat DateFormat =NewSimpleDateFormat ("Yyyymmddhhmmssss"); //part of the picture nameString format = Dateformat.format (NewDate ()); //random three-digit numberRandom random =NewRandom (); for(inti = 0; I <3; i++) {format+=random.nextint (10); } //special methods for getting file name extensionsString Extension =filenameutils.getextension (Pic.getoriginalfilename ()); //Instantiate a JerseyClient client =NewClient (); //Save the path and name of the database to be sent to the Add pageString pathsql = format + "." +extension; //What is the request path for the other server? String url = image_url+format+ "." +extension; //Set Request PathWebResource resource =client.resource (URL); //Send to start Try{resource.put (String.class, Pic.getbytes ()); } Catch(IOException e) {e.printstacktrace (); } //returns two pathsJsonobject Jsonobject =NewJsonobject (); Jsonobject.put ("url", URL); Jsonobject.put ("Path", Pathsql); Write out the echo of the path completion picture, because the picture was saved in another Tomcat server Responseutils.renderjson (Response,jsonobject.tostring ()); }}
The code for Responseutils is
/*** Asynchronously returns various formats * JSON * XML * text *@authorLX **/ Public classResponseutils {//Send content Public Static voidrender (HttpServletResponse response,string contenttype,string text) {response.setcontenttype (contentType); Try{response.getwriter (). write (text); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } //the JSON is being sent Public Static voidRenderjson (httpservletresponse response,string text) {render (response,"Application/json;charset=utf-8", text); } //Send XML Public Static voidrenderxml (httpservletresponse response,string text) {render (response,"Text/xml;charset=utf-8", text); } //Send Text Public Static voidRenderText (httpservletresponse response,string text) {render (response,"Text/plain;charset=utf-8", text); }}
4, create a new WebApp project, remember to switch ports, Switch Port Method 1) Modify the Tomcat folder under the Conf folder server.xml three port number (specific Baidu) 2) I use the idea, simply modify the configuration on the line, the server is best to open at any time, used as a picture server
5, in the original project (not the picture Server project) SPRINGMVC configuration support Upload image function
<!-- configure Springmvc picture upload --> < bean Span style= "COLOR: #ff0000" >id = "Multipartresolver" class =" Org.springframework.web.multipart.commons.CommonsMultipartResolver " > < property name =" maxuploadsize " value = "10485760" /> </ bean >
6, in the Tomcat folder under the Conf of Web. XML to remove the read-only
<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> <!--new Section - <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>
At this point, when you click on the form to browse, select the picture, the image can be successfully uploaded and displayed in the form, but also saved in the input file name, but how to list it?
A: Set a full path Image_url Yes
That's your server path.
!!! E-Commerce Project Disable select * FROM,-----The Allurl property is not in the database
Create a new Tomcat server, Spring+jersey complete the upload image implementation