Java changes the picture format, changes the picture size, and implements multi-threaded upload pictures.

Source: Internet
Author: User

 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.