Java-multipartfile image upload Server, and specify size compression

Source: Internet
Author: User

1  /***2 * Upload images to server and compress3      *4      * @parammyFile File5      * @paramRequest6      * @return7      */8     PrivateBoolean UploadFile (Multipartfile myFile,intWidthintheight, httpservletrequest request) {9Boolean STA =false;TenInputStream is =NULL; OneFileOutputStream fs =NULL; A         /**Temp Folder*/ -String Imgpath = "Xyimages" + file.separator + "Buttonimgtemp" +File.separator; -String TempPath = Servletconstants.web_root_os_path +Imgpath; theSystem.out.println ("old-path-" +TempPath); -String name =myfile.getoriginalfilename (); -File OldFile =NewFile (TempPath); -         if(!oldfile.exists ()) { + oldfile.mkdirs (); -         } +         /**folder after processing*/ AString Newimapath = "Xyimages" + file.separator + "buttonimg" +File.separator; atString NewPath = Servletconstants.web_root_os_path +Newimapath; -System.out.println ("new-path-" +NewPath); -File NewFile =NewFile (NewPath); -         if(!newfile.exists ()) { - newfile.mkdirs (); -         } in         Try { -             /**access the source file first*/ tois =Myfile.getinputstream (); +FS =NewFileOutputStream (TempPath +myfile.getoriginalfilename ()); -             //... the             if(Myfile.getsize () > 0) { *                 byte[] buffer =New byte[1024 * 1024]; $                 intBytesum = 0;Panax Notoginseng                 intByteread = 0; -                  while((Byteread = is.read (buffer))! =-1) { theBytesum + =Byteread; +Fs.write (buffer, 0, byteread); A Fs.flush (); the                 } + fs.close (); - is.close (); $             } $             /**process the source files, compress and place them in the new folder*/ -String OldPath = TempPath +myfile.getoriginalfilename (); -String Copypath = NewPath +myfile.getoriginalfilename (); theBoolean ys =zipwidthheightimagefile (OldPath, Copypath, Width,height, 1f); -             if(YS)WuyiSTA =true; the             Else -STA =false; Wu}Catch(Exception ex) { - ex.printstacktrace (); AboutSTA =false; $         } -         returnSTA; -}
The first step is to save the source image first

1 /***2 * Compress to make size picture3      *4      * @paramoldpath Temporary picture path5      * @paramCopypath Compress picture save path6      * @paramwidth Widths7      * @paramHeight Altitude8      * @paramquality High Definition degree9      * @returnTen      * @throwsException One      */ A     PrivateBoolean zipwidthheightimagefile (String OldPath, String Copypath,intWidthintheight, -                                             floatquality) { -Boolean STA =false; theFile OldFile =NewFile (oldpath); -File NewFile =NewFile (copypath); -         if(OldFile = =NULL) { -             return NULL; +         } -String NewImage =NULL; +         Try { A             /**processing temporary files on the server*/ atImage Srcfile =Imageio.read (oldFile); -             intW = srcfile.getwidth (NULL); - System.out.println (w); -             intH = srcfile.getheight (NULL); - System.out.println (h); -  in             /**wide, high setting*/ -BufferedImage tag =Newbufferedimage (width, height, bufferedimage.type_int_rgb); toTag.getgraphics (). DrawImage (srcfile, 0, 0, width, height,NULL); +             //String Fileprex = oldfile.substring (0, Oldfile.indexof ('. ')); -             /**file name after compression*/ the             //newimage = Fileprex + smallicon+ oldfile.substring (Fileprex.length ()); *  $             /**temporary storage location after compression*/Panax NotoginsengFileOutputStream out =NewFileOutputStream (newFile); -  theJPEGImageEncoder encoder =Jpegcodec.createjpegencoder (out); +JPEGEncodeParam Jep =Jpegcodec.getdefaultjpegencodeparam (tag); A             /**Compression Quality*/ theJep.setquality (Quality,true); + Encoder.encode (tag, Jep); - out.close (); $STA =true; $}Catch(Exception e) { - e.printstacktrace (); -STA =false; the         } -         returnSTA;Wuyi}
Compress Pictures

Java-multipartfile image upload Server, and specify size compression

Related Article

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.