Page
<script type= "Text/javascript" src= "js/jquery.form.js" ></script>
<script language= "JavaScript" >
$ (function () {
Submit Form
$ ("#add"). Click (function () {
if ($ (' #addForm '). Form (' Validate ')) {
var options = {
URL: ' ${currentbaseurl}/doadd ',
Type: ' Post ',
Success:function (data) {
if ("OK" ==data.message)
Window.location.href=data.backurl;
else{
$.messager.alert (' hint ', data.message);
$ ("#imageFile"). Val (""). Focus ()
}
}
};
$ (' #addForm '). Ajaxsubmit(options);
}
});
})
</script>
<form method= "POST" class= "ValidForm" id= "AddForm" name= "AddForm" enctype= "Multipart/form-data">
<input type= "File" id= "ImageFile"Name=" imagefile "class=" txt w240 easyui-validatebox "/>
<input type= "button" id= "Add" class= "btn" value= "Commit"/>
</form>
Controller class
@RequestMapping (value = "Doadd", method = {Requestmethod.post})
@ResponseBody
Public httpjsonresult<object> Doadd (multiparthttpservletrequestRequest, HttpServletResponse response, Fbnews News) {
httpjsonresult<object> Jsonresult = new httpjsonresult<object> ();
try {
multipartfile multipartfile = Request.getfile ("ImageFile");
if (null! = Multipartfile && multipartfile.getsize () > 0) {
Extended Name
String extend = Multipartfile.getoriginalfilename (). substring (Multipartfile.getoriginalfilename (). LastIndexOf (".") + 1). toLowerCase ();
if (!isimg (extend)) {
Jsonresult.setmessage ("Please upload pictures!");
return jsonresult;
}
File Tmpfile = new file (Buildimgpath (Request) + "/" + multipartfile.getoriginalfilename ());
if (!multipartfile.isempty ()) {
byte[] bytes = Multipartfile.getbytes ();
Bufferedoutputstream stream = new Bufferedoutputstream (new FileOutputStream (tmpfile));
Stream.Write (bytes);
Stream.Close ();
}
}
/** other code omitted **/
Jsonresult.setmessage ("OK");
Add successful return list page
Jsonresult.setbackurl (domainurlutil.getjm_url_resources () + "/admin/fbfc/news");
return jsonresult;
} catch (Exception e) {
throw new RuntimeException ();
}
}
Whether it is a picture
Private Boolean isimg (String extend) {
list<string> list = new arraylist<string> ();
List.add ("JPG");
List.add ("JPEG");
List.add ("BMP");
List.add ("GIF");
List.add ("PNG");
List.add ("TIF");
return list.contains (Extend);
}
Path to image upload
Private String Buildimgpath (HttpServletRequest request) {
String path = "Upload";
SimpleDateFormat formater = new SimpleDateFormat ("YyyyMMdd");
Path + = "/" + Formater.format (new Date ());
Path = Request.getrealpath (path);
File dir = new file (path);
if (!dir.exists ()) {
try {
Dir.mkdirs ();
} catch (Exception e) {
Log.error ("error", e);
}
}
return path;
}
Uploading a picture form when a form is submitted Ajax submission