As the project needs in the upload avatar is required to use the asynchronous upload file, in the upload process needs to verify the file: The rules are as follows: Width and height
The degree is greater than 200, the width-height ratio is less than 2, the size is less than 2M.
I'm using ajaxfileuploader this component, and the server uses struts to process files.
Instance:
<form action= "" id= "Imageform" enctype= "Multipart/form-data" method= "POST" >
<input type= "file" Name= " Userphoto "id=" Userphoto ">
<input type=" button "value=" Upload "id=" Shangchuan ">
</form>
Here you need to introduce two JS files: jQuery, ajaxfileupload
<script type= "Text/javascript" src= "${basepath}/resource/js/plugin/jquery-1.6.min.js" ></script>
<script type= "Text/javascript" src= "${basepath}/resource/js/grzx/ajaxfileupload.js" ></script>
JS file:
Upload Avatar $ ("#shangchuan"). Click (function () {var file = $ ("#userPhoto"). Val ();
if (file== "") {alert ("Please choose the Avatar to upload");
Return else{//Determine if the uploaded file is in the correct format var fileType = file.substring (File.lastindexof (".")
+1);
if (filetype!= "PNG" &&filetype!= "jpg") {alert ("Upload file format error");
Return
else{var url = "/symh/user/uploadphoto_uploadphoto.action?nowtime=" +new Date (). GetTime (); $.ajaxfileupload ({url:url, secureuri:false, Fileelementid: "Userphoto",//file ID da Tatype: "Text",//Return data type is Text Success:function (data,status) {if (data== "1") {alert ("
Please upload a picture with a width greater than 200 pixels and a height greater than 200 pixels ");
else if (data== "2") {alert ("Please upload a picture with a height of not more than 2");
else if (data== "3") {alert ("Please upload a picture with a file size not greater than 2M");
} else{$ ("#uploadImage"). Hide ();
$ ("#srcImg"). attr ("src", data);
$ ("#previewImg"). attr ("src", data); $("#cutImage"). Show ();
$ ("#bigImage"). val (data); Cutimage (); Intercept Avatar}}}}})
Background handler: Uploadphotoaction.java
public class Uploadphotoaction {private File Userphoto;
Private String Userphotocontenttype;
Private String Userphotofilename;
Public File Getuserphoto () {return userphoto;
The public void Setuserphoto (File userphoto) {this.userphoto = Userphoto;
Public String Getuserphotocontenttype () {return userphotocontenttype;
} public void Setuserphotocontenttype (String userphotocontenttype) {this.userphotocontenttype = Userphotocontenttype;
Public String Getuserphotofilename () {return userphotofilename;
} public void Setuserphotofilename (String userphotofilename) {this.userphotofilename = Userphotofilename; /** * User Upload image */public void Uploadphoto () {try {httpservletresponse response = (httpservletresponse) Action
Context.getcontext (). get (Servletactioncontext.http_response);
Response.setcharacterencoding ("UTF-8"); FileInputStream fis1 = new FileInputStream (Getuserphoto ()); Save file FileInputStream Fis2 = new FileinputstreaM (Getuserphoto ());
Judge file int i = This.checkimage (FIS2);
if (i==1) {response.getwriter (). Print ("1");
else if (i==2) {response.getwriter (). Print ("2");
else if (i==3) {response.getwriter (). Print ("3");
else {//file correct, upload//get filename String photoname = getphotoname (Getuserphotofilename ());
FileOutputStream fos = new FileOutputStream (Getsavepath () + "\" +photoname);
byte[] buffer = new byte[1024];
int len = 0;
while (len = fis1.read (buffer)) >0) {fos.write (Buffer,0,len);
//process file path to facilitate display of String imagpathstring = Dealpath (Getsavepath () + "\" +photoname) in the foreground;
Response.getwriter (). print (imagpathstring);
} catch (IOException e) {e.printstacktrace (); }/** * Renaming avatar Name: User ID + avatar suffix/public String getphotoname (string photoname) {//Get user HttpServletRequest req
Uest = (httpservletrequest) actioncontext.getcontext (). get (Servletactioncontext.http_request); UserbeAn UserBean = (UserBean) request.getsession (). getattribute ("UserBean");
Gets the suffix of the file string[] strings = Photoname.split ("\.");
String Hz = strings[1]; Build filename String filename = userbean.getuserid () + "."
+hz;
return fileName; /** * Get upload path/public String Getsavepath () {return Servletactioncontext.getservletcontext (). Getrealpath ("Uplo
Ad/photos "); /** * Judge whether the uploaded avatar is legal * rule: Width and height is greater than 200, width-height ratio is less than 2, size is less than 2M * Width or height <200 return 1 * Width-height ratio >2 return 2 * size greater than 2M return 3 * correct return
0 */public int checkimage (FileInputStream fis) {try {bufferedimage image = Imageio.read (FIS);
Double width = image.getwidth ();
Double height = image.getheight (); if (width<200| |
height<200) {return 1;
else if (width/height>2) {return 2;
else if (fis.available ()/(1024*1024) >2) {return 3;
else {return 0;
} catch (IOException e) {e.printstacktrace ();
return 1; /** * Process Avatar Path/public string Dealpath (stringPath) {string[] strings = Path.split ("\\\\");
String string2 = "/";
for (int i = strings.length-4 i < strings.length; i++) {if (i==strings.length-1) {string2 = string2+strings[i];
else {string2 = string2+strings[i]+ "/";
} return string2; }
}
Here's an introduction to using Ajaxfileupload to upload files asynchronously. Below will introduce how to intercept avatar (similar to QQ upload avatar)