/** * */package com.up.controller;import java.awt.image;import java.awt.image.bufferedimage;import java.io.File; Import Java.io.fileoutputstream;import java.io.ioexception;import java.io.inputstream;import java.io.PrintWriter; Import Java.util.random;import Javax.imageio.imageio;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.requestparam;import Org.springframework.web.multipart.MultipartFile; Import Com.up.constant.systemconstants;import Com.up.util.filecopy;import Com.up.util.imageutils;import Com.up.util.operateimage;import com.up.util.uploadutil;/** * @author Hu.shiguo * @time 2014-3-14 2:53:15 * @ Description * @version * * * @Controller @requestmapping (value= "Upload") public class Uploadfilecontroller {/** * image upload * @pa RAM Request * @param response * @param myFile */@RequestMapping (value = "UploadFile") public void upload (HttpServletRequest request, httpservletresponse response, @RequestParam Multipartfile myFile) {//the subdirectory where the file is to be deleted string filefolderpath = Request.getparameter ("Filefolderpath"); Project directory String projectpath = Systemconstants.web_root; File files = new file (projectpath);//Upload files Save directory string uploadfilepath = Files.getparent () +file.separator; String Uploadfilepath =files.getpath () +file.separator + "UploadFile"; Upload the actual path string basepath = uploadfilepath+ "UploadFile" +file.separator+ "Up" +file.separator +filefolderpath; Uncompressed pictures uploaded to Temp folder string TempPath = uploadfilepath+ "UploadFile" +file.separator+ "up" +file.separator + Systemconstants.url_temp; InputStream is = null; FileOutputStream OS = null;if (!new file (basepath). Isdirectory ()) {New file (BasePath). Mkdirs ();} try{//gets the old name of the upload file string name = Myfile.getoriginalfilename ();//Gets the suffix string last = name.substring (Name.lastindexof (".") +1); Upload path--String org = "" Before compressing; File File = new file (TempPath, System.currenttimemillis () + new RandOm (50000). Nextint () + "." +last); is = Myfile.getinputstream (); OS = new FileOutputStream (file); Upload Uploadutil.copyfile (is, OS); Get an uncompressed picture after uploading the absolute path = = = In the Temp folder directory of org = File.getabsolutepath (); System.out.println ("org==" +org);//Compressed Picture storage path String dest = System.currenttimemillis () + new Random (50000). Nextint () + "." +last; System.out.println ("dest==" +dest);//system.out.println ("File.getname ():" +file.getname ()); Method One: Compress the Boolean bol1 = Imageutils.resize (org, basepath+file.separator+dest, 200, 200); Method Two: To cut//return the compressed image name to the front-end display//scale first, and then crop the Boolean bol2 = False;if (bol1) {operateimage o = new Operateimage (basepath+ File.separator+dest, 0, 0, 200, 200); O.setsubpath (basepath+file.separator+dest); O.setimagetype (last); Bol2 = O.cut (); } if (bol1| | Bol2) {System.out.println ("---------" +dest); Response.getwriter (). write (dest); }else{filecopy FC = new FileCopy (); Copy uncompressed files from the temporary file to the target path under Fc.domovefile (File.getname (), TempPath, BasePath) because there is no compression ; Response.getwriter (). Write (File.getname ()); }}catch (EXception e) {e.printstacktrace ();} finally{try{if (OS! = null) {Os.close ();} if (is = null) {Is.close ();}} catch (Exception e) {os = Null;is = null;}}} /** * Delete Upload image * @param request * @param response * @param myFile */@RequestMapping (value = "deletefile.html") public void DeleteFile (httpservletrequest request,httpservletresponse response) {String Filefolderpath = Request.getparameter (" Filefolderpath ") +"/"; String fileName = Request.getparameter ("FileName");//Engineering directory String Webpath = systemconstants.web_root; File files = new file (webpath);//directory of uploaded files string uploadfilepath = Files.getparent () +file.separator; String Uploadfilepath =files.getpath () +file.separator + "UploadFile"; String basepath = uploadfilepath+systemconstants.url_uploadfile+filefolderpath+filename; File File = new file (basepath), if (File.isfile () | | file.isdirectory ()) {File.delete ()} String str = "true"; Response.setcontenttype ("Text/xml;charset=utf-8"); Response.setcharacterencoding ("UTF-8"); Response.setheader ("CacHe-control "," No-cache "); PrintWriter out; try {out = Response.getwriter (); Out.print (str);//used to return the object parameter} catch (IOException e) {e.printstacktrace (); }} }
Springmvc upload images and compress and cut demo