The required jar packages are in: \jdk1.7.0_25\jre\lib\rt.jar
Package Util;import Java.awt.Image; Import Java.awt.image.BufferedImage; Import Java.io.File; Import java.io.FileNotFoundException; Import Java.io.FileOutputStream; Import java.io.IOException; Import Javax.imageio.ImageIO; Import Com.sun.image.codec.jpeg.JPEGCodec; Import Com.sun.image.codec.jpeg.JPEGEncodeParam; Import Com.sun.image.codec.jpeg.JPEGImageEncoder; public class Imageziputil {/** * etc. compress picture file <br> Save original file, then compress, upload * @param oldFile file to be compressed * @param NewFile New File * @param width width//Set width (height in 0, equal scale) * @param height//height (width passed in 0, equal scale) * @param quality Mass * @return Returns the full path of the compressed file */public static String zipimagefile (file oldfile,file newFile, int width, int Heig HT, float quality) {if (oldFile = = null) {return null; try {/** to process temporary files on the server */Image srcfile = Imageio.read (oldFile); int w = srcfile.getwidth (null); System.out.println(w); int h = srcfile.getheight (null); System.out.println (h); Double Bili; if (width>0) {bili=width/(double) W; Height = (int) (H*bili); }else{if (height>0) {bili=height/(double) H; width = (int) (W*bili); }}/** width, height setting */bufferedimage tag = new BufferedImage (width, height, BUFFEREDIMAGE.TYPE_INT_RGB); Tag.getgraphics (). DrawImage (srcfile, 0, 0, width, height, null); String Fileprex = Oldfile.getname (). substring (0, Oldfile.getname (). IndexOf ('. ')); /** Compressed file name *///newimage = Fileprex + smallicon+ oldfile.getname (). substring (Fileprex.length ()); /** after compression temporary storage location */FileOutputStream out = new FileOutputStream (newFile); JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (out); JPEGEncodeParam Jep = Jpegcodec.getdefaultjpegencodeparam (tag); /** Compression Quality */jep.setquality (quality, true); Encoder.encode (tag, Jep); Out.close (); } catch (FileNotFoundException e) {e.printstacktrace (); } catch (IOException e) {e.printstacktrace (); } return Newfile.getabsolutepath (); /** * Compress picture file by width height <br> Save original file, then compress, upload * @param oldFile file full path to be compressed * @param newFile New file * @ Param width * @param height * @param quality mass * @return returns the full path of the compressed file */public static String Zipwidthheightimagefile (File oldfile,file newFile, int width, int height, float quality) {if (oldFile = = null) { return null; } String newimage = null; try {/** handles temporary files on the server */Image srcfile = Imageio.read (oldFile); int w = srcfile.getwidth (null); SysteM.out.println (w); int h = srcfile.getheight (null); System.out.println (h); /** width, height setting */bufferedimage tag = new BufferedImage (width, height,bufferedimage.type_int_rgb); Tag.getgraphics (). DrawImage (srcfile, 0, 0, width, height, null); String Fileprex = oldfile.substring (0, Oldfile.indexof ('. ')); /** Compressed file name *///newimage = Fileprex + smallicon+ oldfile.substring (Fileprex.length ()); /** after compression temporary storage location */FileOutputStream out = new FileOutputStream (newFile); JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (out); JPEGEncodeParam Jep = Jpegcodec.getdefaultjpegencodeparam (tag); /** Compression Quality */jep.setquality (quality, true); Encoder.encode (tag, Jep); Out.close (); } catch (FileNotFoundException e) {e.printstacktrace (); } catch (IOException e) {E.printstackTrace (); } return newimage; } public static void Main (String args[]) throws IOException {System.out.println (IMAGEZIPUTIL.ZIPWIDTHHEIGHTIMAGEF Ile (New file ("e:/picture/1.jpg"), New file ("e:/picture/1-1.jpg"), (3f)); } }
Java Image upload Compression processing