My Java code of the function:
PackageCom.util;ImportJava.awt.Color;ImportJava.awt.Font;ImportJava.awt.Graphics2D;ImportJava.awt.image.BufferedImage;Importjava.io.IOException;ImportJava.util.Random;Importjavax.servlet.ServletException;ImportJavax.servlet.ServletOutputStream;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportCom.sun.image.codec.jpeg.JPEGCodec;ImportCom.sun.image.codec.jpeg.JPEGImageEncoder;/*** @ClassName: Captchautil * @Description: Tool class for verification code *@authornameless * @date 2016-5-7 a.m. 8:33:08 *@version1.0*/ Public Final classcaptchautil{PrivateCaptchautil () {}/** Random character dictionary*/ Private Static Final Char[] CHARS = {' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' J ', ' K ', ' L ', ' M ', ' N ', ' P ', ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z ' }; /** Random number*/ Private StaticRandom random =NewRandom (); /** Get 6-bit random numbers*/ Private StaticString getrandomstring () {StringBuffer buffer=NewStringBuffer (); for(inti = 0; I < 6; i++) {buffer.append (Chars[random.nextint (chars.length))); } returnbuffer.tostring (); } /** Get random number color*/ Private StaticColor Getrandomcolor () {return NewColor (Random.nextint (255), Random.nextint (255), Random.nextint (255)); } /** Returns the inverse color of a color*/ Private StaticColor Getreversecolor (color c) {return NewColor (255-c.getred (), 255-C.getgreen (),-C.getblue ()); } Public Static voidOutputcaptcha (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {response.setcontenttype ("Image/jpeg"); String RandomString=getrandomstring (); Request.getsession (true). SetAttribute ("RandomString", RandomString); intwidth = 100; intHeight = 30; Color Color=Getrandomcolor (); Color Reverse=getreversecolor (color); BufferedImage Bi=Newbufferedimage (width, height, bufferedimage.type_int_rgb); Graphics2D g=Bi.creategraphics (); G.setfont (NewFont (Font.sans_serif, Font.Bold, 16)); G.setcolor (color); G.fillrect (0, 0, width, height); G.setcolor (reverse); g.DrawString (RandomString,18, 20); for(inti = 0, n = random.nextint (100); I < n; i++) {G.drawrect (Random.nextint (width), random.nextint (height),1, 1); } //turn into JPEG formatServletoutputstream out =Response.getoutputstream (); JPEGImageEncoder Encoder=Jpegcodec.createjpegencoder (out); Encoder.encode (BI); Out.flush (); }}
First, I found that the AWT library of Java cannot is used on Linux.
So, must add parameters of the JVM to run it.
-djava.awt.headless=true \
As for the Tomcat environment, u should add the "-djava.awt.headless=true" to the file of ' catalina.sh ' for 8 Tim Es.
If It is not works. U can try these commands:
Yum Install libxp.so. 6 Yum Install 1:1.4. 4-el6yuminstall2.33-1. El6 Yum Install 2.33-1. el6
Just try, I donot know why~
After doing these things, I still can ' t solve the problem,
The error log is:
It ' s The problem of this Java class.
Then I found this 1.8 jdk has abandoned the class of Com.sun.image.codec.jpeg.JPEGCodec & com.sun.image.c Odec.jpeg.JPEGImageEncoder
The change of the code from
New bufferedimage (width, height, bufferedimage.type_int_rgb); ........... // turn into JPEG format Servletoutputstream out = response.getoutputstream (); = Jpegcodec.createjpegencoder (out); Encoder.encode (BI);
To
// turn into JPEG format Servletoutputstream out = response.getoutputstream ();
And it worked:
Solve the problem of ' Java Web Project cannot display verification code '