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. CVE-2010-3654分析及利用

    三年前分析的一个漏洞,最近又温习一遍,这个flash中混淆漏洞的鼻祖,10年最经典的漏洞. 漏洞触发原因 该漏洞主要因为avm对返回的类没有进行校验,通过修改swf文件,实现Ref类和Origin类的 ...

  2. cookies插件,记住cookies

    今天同事交给了我一个记住cookies插件,首先先去网上下载一个jquery.cookie.js文件文件下载 <!DOCTYPE html PUBLIC "-//W3C//DTD HT ...

  3. Content Negotiation in ASP.NET Web API

    本文描述Web API实现内容协商(content negotiation). The HTTP specification (RFC 2616) defines content negotiatio ...

  4. HDU5909 Tree Cutting(树形DP + FWT)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5909 Description Byteasar has a tree T with n ve ...

  5. Unity3D 脚本编译器无法关联VisualStudio2012解决办法

    开发环境:Win8 + Unity 4.34f1 +Visual Studio2012 旗舰版 解决办法:   1.创建一个start.bat文件,内容为:[start "" %* ...

  6. TDD测试驱动开发

    TDD测试驱动开发 一.概念 TDD故名思意就是用测试的方法驱动开发,简单说就是先写测试代码,再写开发代码.传统的方式是先写代码,再测试,它的开发方式与之正好相反. TDD是极限编程的一个最重要的设计 ...

  7. git flow工作流实际项目实践

    公司项目的开发流程主要是这样 代码分为 develop分支 master分支 平时我开发的时候,主要在develop分支上改动 一般来讲,有以下几种改动方式 1.直接在develop上修改代码 这种一 ...

  8. myEclipse Could not create the view: An unexpected exception was thrown.

    myEclipse 非正常关闭,打开后 service Explorer or Package Explorer 视图显示不出来.报“Could not create the view: An une ...

  9. FTP命令 - Size的问题

    今天发现一个服务从某一个外接系统(Linux)FTP取到的文件大小和下载后的文件大小总是不一致. 开始以为是程序那里出错了.但是找来找去发现不了原因.后来用FTP工具上去执行SIZE命令,终于发现返回 ...

  10. Spark优化之三:Kryo序列化

    Spark默认采用Java的序列化器,这里建议采用Kryo序列化提高性能.实测性能最高甚至提高一倍. Spark之所以不默认使用Kryo序列化,可能的原因是需要对类进行注册. Java程序中注册很简单 ...