Using springmvc+spring
Front-end submission of picture files to controller, check the upload image size to meet the requirements
directly on the code.
1. Check the image size
A validation method is provided here, which is used to call in place where validation is required
1 /**2 * Verify picture size3 */4 PrivateMap<string,object>Validate (multipartfile image) {5Map<string,object> Jsonmap =NewHashmap<string,object>();6 //360*240 30k (length * width max)7 if(!Image.isempty ()) {8 Try {9BufferedImage bi =Imageio.read (Image.getinputstream ());TenString standard = constant.thumbnail_size;//constant.thumbnail_size=360,240,30 Onestring[] Stand = Standard.split (","); A if(Bi.getwidth () >NewInteger (stand[0])) { -Jsonmap.put (constant.error_msg, "thumbnail width cannot be greater than" + stand[0] + "px"); - } the if(Bi.getheight () >NewInteger (stand[1])) { -Jsonmap.put (constant.error_msg, "thumbnail height cannot be greater than" + stand[1] + "px"); - } - if(Image.getsize ()/1024x768 >NewInteger (stand[2])) { +Jsonmap.put (constant.error_msg, "thumbnail size cannot be greater than" + stand[2] + "K"); - } + A}Catch(IOException e) { atLogger.error ("IOException exception appears when picture is verified!")); - e.printstacktrace (); - } - } - returnJsonmap; -}
ways to check the size of a picture
2. After passing the calibration, upload the picture to the specified location
Here is the method used in the controller to receive requests, where the method is merged into the controller.
1 /**2 * Upload Images3 * @throwsIOException4 * @throwsillegalstateexception5 */6@RequestMapping ("/uploadimg.do")7 @ResponseBody8 PublicObject uploadimg (multipartfile thumbnail,httpsession session)throwsillegalstateexception, ioexception{9Map<string,object> Jsonmap =NewHashmap<string,object>();Ten OneJsonmap = validate (thumbnail);//Verify that the picture size meets the requirements A - if(Jsonmap.get (constant.error_msg) = =NULL){//picture format meets requirements - the //get directory where files are stored - //String Path = Session.getservletcontext (). Getrealpath ("/upload"); -String Path =Constant.upload_img_path; - //File Upload + if( !Thumbnail.isempty ()) { - //uploaded file +String FileName =thumbnail.getoriginalfilename (); A //restrictions on uploading files, usually preceded by extensions.. jpg,. png at if(Filename.endswith (". jpg") | | filename.endswith (". png") | | filename.endswith (". bmp") | | filename.endswith (". jpeg") | | Filename.endswith (". gif")) { - //can upload - //The file name is processed, and the file name is unique on the server. Generate unique values for methods that use the UUID class -String uuid =Uuidgenerator.getuuid (); - - //Remove the extension of the original file in intpos = Filename.lastindexof (".")); - toString Extname =filename.substring (POS); + - //make a full file name theString NewFileName = uuid +Extname; * $ //Save the file to the serverPanax NotoginsengFile dest =NewFile (path,newfilename); - if(!dest.exists ()) { the dest.mkdirs (); + } A Thumbnail.transferto (dest); the + //File access Path -String urlimg = constant.url_pre_img +NewFileName; $ //successfully uploaded files $Jsonmap.put (Constant.success,true); -Jsonmap.put ("Thumbnail_url", urlimg); - the}Else { -Jsonmap.put (Constant.success,false);WuyiJsonmap.put (constant.error_msg, "picture format only supports Jpg|bmp|gif|jpeg|png"); theLogger.info ("Picture upload failed!!!! "); - } Wu } -}Else{ AboutJsonmap.put (Constant.success,false); $Jsonmap.put (constant.error_msg, "picture size should be 360*240px cannot be greater than 30K"); - } - returnJsonmap; -}
after verifying the image format, upload
Springmvc image upload, check image size