First step: Introduce the required JS
<script src= "/res/common/js/jquery.js" type= "Text/javascript" ></script>
<script src= "/res/common/js/jquery.form.js" type= "Text/javascript" ></script><!---- Component to simulate form form submission Data--->
Part II: Writing HTML pages
<form id= "Jvform" action= "add.do" method= "post" enctype= "Multipart/form-data" >
<table cellspacing= "1" cellpadding= "2" width= "100%" border= "0" class= "pn-ftable" >
<tbody>
<tr>
<TD width= "20%" class= "Pn-flabel pn-flabel-h" >
<span class= "pn-frequired" >*</span>
Brand Name: </td><td width= "80%" class= "Pn-fcontent" >
<input type= "text" class= "required" name= "name" maxlength= "/>"
</td>
</tr>
<tr>
<TD width= "20%" class= "Pn-flabel pn-flabel-h" >
<span class= "pn-frequired" >*</span>
Upload a product image (90x150 size):</td>
<TD width= "80%" class= "Pn-fcontent" >
Note: This size picture must be 90x150.
</td>
</tr>
<tr>
<TD width= "20%" class= "Pn-flabel pn-flabel-h" ></td>
<TD width= "80%" class= "Pn-fcontent" >
<!--used to display the picture on the page after the upload was successful--
<input type= "hidden" name= "Imgurl" id= "path"/><!--need to submit a picture path to the server, because it is an asynchronous upload, so you need to bring the hidden domain of Imgurl--
<input type= "File" onchange= "Uploadpic ()" Name= "pic"/><!--Upload file Control--
</td>
</tr>
<tr>
<TD width= "20%" class= "Pn-flabel pn-flabel-h" >
Brand Description: </td><td width= "80%" class= "Pn-fcontent" >
<input type= "text" class= "required" name= "description" maxlength= "Up" size= "/>"
</td>
</tr>
<tr>
<TD width= "20%" class= "Pn-flabel pn-flabel-h" >
Sort: </td><td width= "80%" class= "Pn-fcontent" >
<input type= "text" class= "required" name= "sort" maxlength= "/>"
</td>
</tr>
<tr>
<TD width= "20%" class= "Pn-flabel pn-flabel-h" >
Available: </td><td width= "80%" class= "Pn-fcontent" >
<input type= "Radio" name= "Isdisplay" value= "1" checked= "Checked"/> Available
<input type= "Radio" name= "Isdisplay" value= "0"/> Not available
</td>
</tr>
</tbody>
<tbody>
<tr>
<TD class= "Pn-fbutton" colspan= "2" >
<input type= "Submit" class= "Submit" value= "Submission"/> <input type= "reset" class= "reset" value= "reset"/>
</td>
</tr>
</tbody>
</table>
</form>
Step three: Write the JS code to simulate the form form to go to bed pictures, files
<script type= "Text/javascript" >
Upload image
function Uploadpic () {
Defining parameters
var options = {
URL: "/upload/uploadpic.do",
DataType: "JSON",
Type: "Post",
Success:function (data) {
Callback of two paths
Url
Path
$ ("#allImgUrl"). attr ("src", data.url);
$ ("#path"). Val (Data.path);
}
};
How to use Jquery.form
$ ("#jvForm"). Ajaxsubmit (options);
}
</script>
Fourth step: Write the background accept the uploaded picture in Springmvc
/**
* Upload Images
* Products
* Brand
* @author LX
*
*/
@Controller
public 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-0-999,
for (int i=0; i<3; i++) {
Format + = R.nextint (ten);
}
/* The following is the use of Jersey to upload the picture to another tomcat, if it is generally saved pictures, you can use the Javaweb in general, because the method parameter pic is the uploaded image or file
Instantiate a Jersey
Client client = new Client ();
Save Database
String Path = "upload/" + Format + "." + ext;
What is the request path for the other server?
String url = constants.image_url + path;
Set Request Path
WebResource resource = Client.resource (URL);
Send Start POST get PUT
try {
Resource.put (String.class, Pic.getbytes ());
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
Returns two paths
Jsonobject Jo = new Jsonobject ();
Jo.put ("url", url);
Jo.put ("path", path);
Responseutils.renderjson (response, jo.tostring ());
}
}
Fifth Step: Configure the parser for uploading files in Spring-mvc.xml
<!--upload Pictures--
<bean id= "Multipartresolver" class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver" >
<!--max upload size B 1M--
<property name= "maxuploadsize" value= "1048576"/>
</bean>
Java Upload images asynchronously