1, if in the SPRINGMVC, will bring the generated Multipartfile file, the Multipartfile converted to file
1 multipartfile file1 =2 commonsmultipartfile cf=3 diskfileitem fi = /c5>4 File f = fi.getstorelocation ();
2, convert to the image class, and get some necessary values
1 Private StaticImage img; 2 Private Static floatwidth; 3 Private Static floatheight;4 5img = Imageio.read (f);//Constructing an Image object6width = float.parsefloat (string.valueof (Img.getwidth (NULL)));//get the source map width7Height = float.parsefloat (string.valueof (Img.getheight (NULL)));//get the source graph length8 9SYSTEM.OUT.PRINTLN ("Input picture size:" +f.length ()/1024+ "KB");
3. Compress pictures
1 //determine if the file can be converted into an image class2 if(Width!=-1){3 //The current example is equal scale to 320px wide4 //Zoom ratio5Float s=width/320;6 //the width to zoom7Integer w= (int) (width/s);8 //to scale the high9Integer h= (int) (height/s);Ten One //Create a template ABufferedImage image =NewBufferedImage (w, H,BUFFEREDIMAGE.TYPE_INT_RGB); - //draw a zoomed-out diagram on a template -Image.getgraphics (). DrawImage (img, 0, 0, W, H,NULL); the}
4. Convert the resulting picture to InputStream
New"gif"new Bytearrayinputstream (Os.tobytearray ());
Compression of Java pictures