日期:2014-05-16  浏览次数:20766 次

Java输入验证码在linux上不能显示的问题

做了个Java验证码程序,结果发现在Windows上运行一切正常,在Linux下却无法显示图片,在网上搜索,开始一直以为是tomcat\bin\catalina.sh的原因,但一直都不行,单独运行生成验证码的Servlet则抛出异常:

javax.imageio.IIOException: Can't create output stream!,

经查,在将图片输入到客户端时,不能使用ImageIO.write(image, "jpeg", response.getOutputStream());而应该使用如下方式:

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(response.getOutputStream());
encoder.encode(image);

?

原因是是ImageIO.write(image, "jpeg", response.getOutputStream());
需要写缓冲文件,但是写文件不成功。

(一)生成验证码的servlet

?

package com.founder.chat.common;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**
 * 验证码输入校验类。
 * @Project chat
 * @Creator y.zhang
 * @CreatDate 2009-4-11
 */
public class GetValidateImage extends HttpServlet
{
	private static final long serialVersionUID = -8584041841501416165L;

	private static int WIDTH = 60;

	private static int HEIGHT = 20;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		HttpSession session = request.getSession();

		// 设置浏览器不要缓存此图片
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		response.setContentType("image/jpeg");

		// 创建内存图象并获得其图形上下文
		BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
		Graphics g = image.getGraphics();

		// 产生随机的认证码并画图
		char[] rands = this.generateCheckCode();
		this.drawBackground(g);
		this.drawRands(g, rands);

		// 结束图像的绘制过程,完成图像
		g.dispose();

		// 将当前验证码存入到Session中
		session.setAttribute("validateNum", new String(rands));

		// 将图像输出到客户端
		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(response.getOutputStream());
		encoder.encode(image);
	}

	/**
	 * 产生随机数。
	 * @return
	 */
	private char[] generateCheckCode()
	{
		// 定义验证码的字符表
		String chars = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ";
		char[] rands = new char[4];
		for (int i = 0; i < 4; i++)
		{
			int rand = (int) (Math.random() * 28);
			rands[i] = chars.charAt(rand);
		}
		return rands;
	}

	/**
	 * 画随机数。
	 * @param g
	 * @param rands
	 */
	private void drawRands(Graphics g, char[] rands)
	{
		g.setColor(Color.BLACK);
		g.setFont(new Font(null, Font.PLAIN | Font.BOLD, 18));
		// 在不同的高度上输出验证码的每个字符
		g.drawString("" + rands[0], 1, 17);
		g.drawString("" + rands[1], 16, 15);
		g.drawString("" + rands[2], 31, 18);
		g.drawString("" + rands[3], 46, 16);
		System.out.println(rands);
	}

	/**
	 * 画图片背景。
	 * @param g
	 */
	private void drawBackground(Graphics g)
	{
		g.setColor(new Color(0xDCDCDC));
		g.fillRect(0, 0, WIDTH, HEIGHT);
		// 随机产生120个干扰点
		for (int i = 0; i < 120; i++)
		{
			int x = (int) (Math.random() * WIDTH);
			int y = (int) (Math.random() * HEIGHT);
			int red = (int) (Math.random() * 255);
			int green = (int) (Math.random() * 255);
			int blue = (int) (Math.random() * 255);
			g.setColor(new Color(red, green, blue));
			g.drawOval(x, y, 1, 0);
		}
	}
}
?

?