PackageCom.tongyou.util;ImportOrg.slf4j.Logger;Importorg.slf4j.LoggerFactory;Importorg.springframework.util.FileCopyUtils;ImportOrg.springframework.web.multipart.MultipartFile;ImportJava.awt.Color;ImportJava.awt.Graphics2D;ImportJava.awt.image.BufferedImage;ImportJava.io.File;Importjava.io.IOException;ImportJavax.imageio.ImageIO;/*** File Upload Tool *@version1.0.0 * @date 2018/4/2*/ Public classUploadutilImplementsRunnable {Private Static FinalLogger LOG = Loggerfactory.getlogger (uploadutil.class); PrivateMultipartfile Multipartfile; /*** Root Path*/ PrivateString RootPath; /*** Folder full path*/ PrivateString FolderPath; /*** File full path*/ PrivateString FilePath; /*** File access address*/ PrivateString FileUrl; PublicUploadutil (multipartfile multipartfile, String folderName) { This. Multipartfile =Multipartfile; This. RootPath = "V:/xph/image"; String Realfilename=Multipartfile.getoriginalfilename (); String fileextension= Realfilename.substring (Realfilename.lastindexof ("."))); String FileName= Uuidutil.randomuuid () +fileextension; This. FolderPath = This. RootPath +FolderName; This. FilePath = FolderPath + "/" +FileName; String FILEURL= "Http://192.168.0.148:80" + folderName + "/" +FileName; This. FileUrl = fileurl.substring (0, Fileurl.lastindexof ("."))); } Private voidUpload ()throwsException {File Dirpath=NewFile (FolderPath); if(!dirpath.exists ()) {Dirpath.mkdirs (); } File UploadFile=NewFile (FilePath); Filecopyutils.copy (Multipartfile.getbytes (), uploadfile); //save uploaded images in jpg format This. Changjpg (UploadFile); //generate thumbnail images This. Changsmall (UploadFile); } Private voidChangjpg (File uploadfile)throwsIOException {String path=Uploadfile.getpath (); //TODO auto-generated Method StubBufferedImage bufferedimage=Imageio.read (UploadFile); //Create a blank, RGB, same width and height, and a white backgroundBufferedImage Newbufferedimage =NewBufferedImage (Bufferedimage.getwidth (), Bufferedimage.getheight (), BUFFEREDIMAGE.TYPE_INT_RGB); //Type_int_rgb: Creating a RBG image, 24-bit depth, successfully converting 32-bit graph to 24-bitNewbufferedimage.creategraphics (). DrawImage (bufferedimage, 0, 0, Color.White,NULL); //write to JPEG fileString fileName = path.substring (0,path.lastindexof ("."))); Imageio.write (Newbufferedimage,"JPG",NewFile (filename+ ". jpg")); } @Override Public voidrun () {Try{upload (); } Catch(Exception e) {log.error ("File Upload failed:", E); } } PublicString Getfileurl () {returnFileUrl; } /*** Generate thumbnails in the specified position with the specified picture*/ Private voidChangsmall (File uploadfile) {String path=Uploadfile.getpath (); Try{bufferedimage input=Imageio.read (UploadFile); BufferedImage Inputbig=NewBufferedImage (33, 33, BUFFEREDIMAGE.TYPE_INT_BGR); Graphics2D g=(graphics2d) inputbig.getgraphics (); G.drawimage (Input,0, 0,33,33,NULL);//DrawingG.dispose (); Inputbig.flush (); String fname= path.substring (0, Path.lastindexof ("."));//New nameString parent =uploadfile.getparent (); Imageio.write (Inputbig,"JPG",NewFile (fname + "_small.jpg"));//save it under c:/imagesort/targetpic/}Catch(Exception ex) {ex.printstacktrace (); } }}
Java changes the picture format, changes the picture size, and implements multi-threaded upload pictures.