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 弹出窗口 ...
随机推荐
- Kafka vs RocketMQ——单机系统可靠性-转自阿里中间件
引言 前几期的评测中,我们对比了Kafka和RocketMQ的吞吐量和稳定性,本期我们要引入一个新的评测标准--软件可靠性. 何为"可靠性"? 先看下面这种情况:有A,B两辆越野汽 ...
- MySQL 数据类型 详解
MySQL 数据类型 详解 MySQL 的数值数据类型可以大致划分为两个类别,一个是整数,另一个是浮点数或小数.许多不同的子类型对这些类别中的每一个都是可用的,每个子类型支持不同大小的数据,并且 My ...
- pyqt的信号槽机制(转)
PySide/PyQt Tutorial: Creating Your Own Signals and Slots This article is part 5 of 8 in the series ...
- 实验二 c语言文法
<程序>-><声明>|<程序><函数> <函数>-><修饰符><标识符><形式参数>< ...
- 【第三课】WEBIX 入门自学-Hello World
在看官网教程时,入门的例子就是dataTable这个空间.So,遵循官网,一起来看一下入门的DataTable组件: WEB使用时固然是先引入相应的库文件: 代码如下 <html> < ...
- 四道简单DP
DP类题目找到子问题(状态),然后找到转移方程,就OK #dp #likes matrixchain #according to two point's distance to recurrence ...
- web前端面试题汇总
1.doctype作用 <!DOCTYPE>声明位于位于HTML文档中的第一行,处于 <html> 标签之前.告知浏览器的解析器用什么文档标准解析这个文档.DOCTYPE不存在 ...
- nodejs的实现原理和搭建服务器(动态)
心得体会 今天是我学习的Node.js的第二天,所谓的node.js其实它是javascript编写的服务器的语言,同时它又是属于后台的框架,是一个开放性的平台. 一.相关理论知识: 我们可以用 ...
- codeforces 360 C
C - NP-Hard Problem Description Recently, Pari and Arya did some research about NP-Hard problems and ...
- ANT_HOME is set incorrectly or ant could not be located .Please set ANT_HOME.
今天配置ant,奶奶的老是报错. 因为环境变量 ANT_HOME 和 JAVA_HOME 设置值不能有“;” ,把分号去掉即可成功了. 神奇!