Java image read/write

Source: Internet
Author: User
Java photo processing:
 
/*** @ Author YZH photo processing * @ return * @ throws exception */Public String getpersonnelphoto () throws exception {httpservletrequest Req = servletactioncontext. getrequest (); httpservletresponse response = servletactioncontext. getresponse (); PNO = (string) session. get ("no"); string zhigongbhstr = req. getparameter ("bh"); If (zhigongbhstr = NULL) zhigongbhstr = PNO; string xingbiestr = req. getparameter ("xingbie "); If (zhigongbhstr! = NULL & xingbiestr! = NULL) {quapersonphoto pH = personqualityservice. getquapersonphoto (zhigongbhstr); File URL = NULL; If (pH = NULL) {getdefaultpic (xingbiestr, URL, response);} else {If (pH. getphoto () = NULL) {getdefaultpic (xingbiestr, URL, response);} else {byte [] photobytes = Ph. getphoto (); response. setcontenttype ("image/JPEG"); outputstream toclient = response. getoutputstream (); inputstream in = new bytearrayinputst Ream (photobytes); int Len; byte [] Buf = new byte [1024]; while (LEN = in. Read (BUF, 0, 1024 ))! =-1) {toclient. write (BUF, 0, Len);} toclient. close ();/* // method 2 * image = NULL; * inputstream isphoto = new bytearrayinputstream (photobytes); * image = ImageIO. read (isphoto); * response. setcontenttype ("image/JPEG"); * specify imageencoder encoder = * specify codec. createjpegencoder (response. getoutputstream (); * incluencodeparam Param = encoder. getdefajpegincluencodeparam (bufferedimage) image); * Param. setquality (1.0f, true); * encoder. encode (bufferedimage) image, Param); * ImageIO. setusecache (false); ImageIO. write (renderedimage) * image, "jpg", response. getoutputstream (); */} return NULL ;}
/*** @ Author YZH get the default image * @ Param xingbiestr * @ Param URL * @ Param response */private void getdefaultpic (string xingbiestr, File URL, httpservletresponse response) {If (xingbiestr! = NULL) {If ("1 ". equals (xingbiestr) {url = new file (servletactioncontext. getrequest () // female. getsession (). getservletcontext (). getrealpath ("/images/quality/qlovelady.gif");} If ("0 ". equals (xingbiestr) {// male url = new file (servletactioncontext. getrequest (). getsession (). getservletcontext (). getrealpath ("/images/quality/qloveman.gif");} bufferedinputstream Bis = NULL; servletoutputstream sout = NULL; If (UR L! = NULL) {try {Bis = new bufferedinputstream (New fileinputstream (URL); byte [] buffer = new byte [1024*8]; int result =-1; sout = response. getoutputstream (); While (result = bis. read (buffer ))! =-1) {sout. write (buffer, 0, result);} sout. flush ();} catch (exception e) {e. printstacktrace ();} finally {try {bis. close (); sout. close ();} catch (exception ex) {ex. printstacktrace ();}}}}}

// The default image path in the project:


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.