solve the problem of 'java web project cannot display verification code'
my java code of the function:
package com.util; import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random; import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder; /**
* @ClassName: CaptchaUtil
* @Description: 关于验证码的工具类
* @author 无名
* @date 2016-5-7 上午8:33:08
* @version 1.0
*/
public final class CaptchaUtil
{
private CaptchaUtil(){} /*
* 随机字符字典
*/
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' }; /*
* 随机数
*/
private static Random random = new Random(); /*
* 获取6位随机数
*/
private static String getRandomString()
{
StringBuffer buffer = new StringBuffer();
for(int i = 0; i < 6; i++)
{
buffer.append(CHARS[random.nextInt(CHARS.length)]);
}
return buffer.toString();
} /*
* 获取随机数颜色
*/
private static Color getRandomColor()
{
return new Color(random.nextInt(255),random.nextInt(255),
random.nextInt(255));
} /*
* 返回某颜色的反色
*/
private static Color getReverseColor(Color c)
{
return new Color(255 - c.getRed(), 255 - c.getGreen(),
- c.getBlue());
} public static void outputCaptcha(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{ response.setContentType("image/jpeg"); String randomString = getRandomString();
request.getSession(true).setAttribute("randomString", randomString); int width = 100;
int height = 30; Color color = getRandomColor();
Color reverse = getReverseColor(color); BufferedImage bi = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = bi.createGraphics();
g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 16));
g.setColor(color);
g.fillRect(0, 0, width, height);
g.setColor(reverse);
g.drawString(randomString, 18, 20);
for (int i = 0, n = random.nextInt(100); i < n; i++)
{
g.drawRect(random.nextInt(width), random.nextInt(height), 1, 1);
} // 转成JPEG格式
ServletOutputStream out = response.getOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(bi);
out.flush();
}
}
First, I found that the awt library of java cannot be used on Linux.

So, must add parameters of jvm to run it.
-Djava.awt.headless=true \
As for the tomcat environment, u should add this line '-Djava.awt.headless=true \' to the file of 'catalina.sh' for 8 times.
If it not works. u can try these commands:
yum install libXp.so.
yum install dejagnu.noarch :1.4.-.el6
yum install dejavu-sans-mono-fonts.noarch 2.33-.el6
yum install dejavu-serif-fonts.noarch 2.33-.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 that the 1.8 jdk has abandoned the class of com.sun.image.codec.jpeg.JPEGCodec & com.sun.image.codec.jpeg.JPEGImageEncoder
So I change the code from
BufferedImage bi = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
...........
// 转成JPEG格式
ServletOutputStream out = response.getOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(bi);
to
// 转成JPEG格式
ServletOutputStream out = response.getOutputStream();
ImageIO.write(bi, "JPEG", out);
And it worked:

solve the problem of 'java web project cannot display verification code'的更多相关文章
- WebSocket集成XMPP网页即时通讯1:Java Web Project服务端/客户端Jetty9开发初探
Web 应用的信息交互过程通常是客户端通过浏览器发出一个请求,服务器端接收和审核完请求后进行处理并返回结果给客户端,然后客户端浏览器将信息呈现出来,这种机制对于信息变化不是特别频繁的应用尚能相安无事, ...
- java project转变成java web project
首先,你的eclipse必须得装有web插件 1.找到项目工作空间目录,打开.project文件,并修改文件, 修改如下: 找到:<natures> </natures&g ...
- Java Web Project自定义错误页面,log4j记录日志。
创建记录日志的文件LoggerHelper.java: package com.wyp.helper; import org.apache.log4j.Logger; public class Log ...
- Java Web Project Problems
A: 项目红叉 1. 检验 Java Builder Path 2. 检查 Projects Facets 3. 查看 Targets Runtimes B:项目红感叹号 1. 查看问题栏 Prob ...
- 在eclpse中 一个web project 引用多个 java project 的方法
在开发时,我们会遇到一个需求:模块化.它要求我们把 业务组件进行拆分,分组.把一部分业务功能集中处理,以保证 部分功能块的独立,便于 分配任务到个人,确定人员职责,源代码管理,和发布时重组. 我们尝试 ...
- java web 学习 --第一天(Java三级考试)
1.Servlet servlet是运行在web server或 application server端的Java程序,主要用于在服务器端产生动态内容. servlet 在服务器端主要有以下作用 读取 ...
- step2-------使用myeclipse创建maven java web项目
1.文章内容概述: 在对项目需求进行分析之后,决定使用maven对我的java web项目进行管理,这篇文章记录了使用myeclipse创建maven java web项目的过程. 2.开发环境: j ...
- 基于Apache axis2开发Java Web服务
1.安装配置axis2环境 1)下载axis2-1.4.1-war(发布webservice)和axis2-1.4.1-bin.zip(webservice调用使用的各种包) 下载好后把axis2-1 ...
- Eclipse创建一个JAVA WEB项目
继上一篇博客,Eclipse的Tomcat已经配置好了,现在我们开始创建web项目. 1.打开Eclipse,选择菜单栏的file>New>Dynamic Web Project 弹出窗口 ...
随机推荐
- winform进程、线程、TreeView递归加载
进程: 一般来说,一个程序就是一个进程,不过也有一个程序需要多个进程支持的情况. 进程所使用的类:Process 所需命名空间:System.Diagnostics; 可以通过进行来开启计算机上现有的 ...
- AsyncTask异步上传文本到服务器
服务器代码:用于接收客户端信息 package ches; import java.io.IOException; import java.io.PrintWriter; import javax.s ...
- 编写高质量的 Java 代码
代码质量概述 代码质量所涉及的5个方面,编码标准.代码重复.代码覆盖率.依赖项分析.复杂度分析.这5方面很大程序上决定了一份代码的质量高低. 我们分别来看一下这5方面:编码标准:这个想必都很清楚,每个 ...
- Linux SHELL 命令入门题目答案(一)
1.如何使用shell 打印 “Hello World!” (1)如果你希望打印 !,那就不要将其放入双引号中,或者你可以通过转义字符转义(2)echo 'hello world!' 使用单引号ech ...
- bind模拟
if (!Function.prototype.bind) { Function.prototype.bind = function(oThis) { if (typeof this !== 'fun ...
- javascript常识
substring和substr的区别 substring() 方法用于提取字符串中介于两个指定下标之间的字符. stringObject.substring(start,stop)例子: <s ...
- Code HighLight
#!/bin/sh BEG=`date --date '-7 days' +%Y%m%d` END=`date --date '-1 days' +%Y%m%d` #BEG='20140509' #E ...
- mac osx Forbidden You don't have permission to access / on this server解决方法
(1)首先查看*.conf 是否有读写权限,如果没有要将文件赋予读写权限,比如 localhost.conf (2)再查看/Users/username/Sites/localhost/文件夹是否有i ...
- PHP获取二维数组中的指定若干列【同array_column】
PHP5.3以上 用到了array_map 使用匿名函数进行处理 代码: <?php function array_col($arr = array(), $idx = 0, $newidx ...
- Jquery垂直下拉二级菜单
自己做了一个基于Jquery 的垂直下拉二级菜单功能,直接看图: Html的代码如下: <!DOCTYPE html> <html> <head> <meta ...