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'的更多相关文章

  1. WebSocket集成XMPP网页即时通讯1:Java Web Project服务端/客户端Jetty9开发初探

    Web 应用的信息交互过程通常是客户端通过浏览器发出一个请求,服务器端接收和审核完请求后进行处理并返回结果给客户端,然后客户端浏览器将信息呈现出来,这种机制对于信息变化不是特别频繁的应用尚能相安无事, ...

  2. java project转变成java web project

    首先,你的eclipse必须得装有web插件 1.找到项目工作空间目录,打开.project文件,并修改文件, 修改如下:      找到:<natures> </natures&g ...

  3. Java Web Project自定义错误页面,log4j记录日志。

    创建记录日志的文件LoggerHelper.java: package com.wyp.helper; import org.apache.log4j.Logger; public class Log ...

  4. Java Web Project Problems

    A: 项目红叉 1. 检验 Java Builder  Path 2. 检查 Projects Facets 3. 查看 Targets Runtimes B:项目红感叹号 1. 查看问题栏 Prob ...

  5. 在eclpse中 一个web project 引用多个 java project 的方法

    在开发时,我们会遇到一个需求:模块化.它要求我们把 业务组件进行拆分,分组.把一部分业务功能集中处理,以保证 部分功能块的独立,便于 分配任务到个人,确定人员职责,源代码管理,和发布时重组. 我们尝试 ...

  6. java web 学习 --第一天(Java三级考试)

    1.Servlet servlet是运行在web server或 application server端的Java程序,主要用于在服务器端产生动态内容. servlet 在服务器端主要有以下作用 读取 ...

  7. step2-------使用myeclipse创建maven java web项目

    1.文章内容概述: 在对项目需求进行分析之后,决定使用maven对我的java web项目进行管理,这篇文章记录了使用myeclipse创建maven java web项目的过程. 2.开发环境: j ...

  8. 基于Apache axis2开发Java Web服务

    1.安装配置axis2环境 1)下载axis2-1.4.1-war(发布webservice)和axis2-1.4.1-bin.zip(webservice调用使用的各种包) 下载好后把axis2-1 ...

  9. Eclipse创建一个JAVA WEB项目

    继上一篇博客,Eclipse的Tomcat已经配置好了,现在我们开始创建web项目. 1.打开Eclipse,选择菜单栏的file>New>Dynamic Web Project 弹出窗口 ...

随机推荐

  1. axure的一些注意事项

    1. 不要轻易用中继器的 载入时 事件, 感觉存在bug 2. 元件在显示和隐藏的动画过程中,不要去取他的x,y值,有几率会取成0,也不要去获取它的尺寸,只有在动画完成后才能获得 3. 装着一个中继器 ...

  2. 在 Delphi 中判断一个字符是中文的方法

    http://blog.163.com/l1_jun/blog/static/143863882011741124581/ 由于 Delphi2005 支持中文标识符,在编写 PASCAL 词法分析器 ...

  3. jackrabbit学习笔记(1)

    http://dove19900520.iteye.com/blog/1654346 看的这个文章照着来的,遇到了一些问题,记录一下 运行报这个错:NamespaceException: wiki: ...

  4. java微信公众号开发----搭建ngrok环境

    下载ngrok,一个能够在公网安全访问内网Web主机的工具 下载地址:http://download.csdn.net/download/u014252425/9389847,亲测可用 下载完成后,进 ...

  5. "不能在 DropDownList 中选择多个项。"其解决办法及补充

    探讨C#.NET下DropDownList的一个有趣的bug及其解决办法 摘要: 本文就C#.Net 环境下Web开发中经常使用的DropDownList控件的SelectedIndex属性进行了详细 ...

  6. 第二天 ci执行流程

    第二天 ci执行流程 welcome 页面 this this->load 单入口框架index.php 两个文件夹 system application定义 定义常亮路径 载入 codeign ...

  7. 测试...外部指针访问private

    #include<iostream> using namespace std; class A{ public: int* getPointer(){ return &m; } v ...

  8. ASP.NET图形验证码的生成

    效果: 调用方法: int[] r = QAPI.VerifImage.RandomList();//取得随机数种子列 );//产生验证码字符 pictureBox1.Image = QAPI.Ver ...

  9. 使用神经网络来识别手写数字【译】(三)- 用Python代码实现

    实现我们分类数字的网络 好,让我们使用随机梯度下降和 MNIST训练数据来写一个程序来学习怎样识别手写数字. 我们用Python (2.7) 来实现.只有 74 行代码!我们需要的第一个东西是 MNI ...

  10. 在VS2012下静态链接MFC的问题

    1>------ 已启动生成: 项目: MFCApplication1, 配置: Debug Win32 ------1>uafxcwd.lib(afxctrlcontainer2.obj ...