eclipse导入rt包问题_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > eclipse导入rt包问题

eclipse导入rt包问题

 2013/10/6 3:44:16  eric2500  程序员俱乐部  我要评论(0)
  • 摘要:copy了一份别人写的生成验证码的servlet,却发现编译时不能通过,提示找不到sun.image.codec.jpeg.JPEGCodec和com.sun.image.codec.jpeg.JPEGImageEncoder。其servlet如下。最后,通过将jrelib目录下的rt.jar拷贝到工程,然后引入,得以通过。packagecom.jesse.onlineshop.servlet;importjava.awt.Color;importjava.awt.Font
  • 标签:Eclipse 问题
copy了一份别人写的生成验证码的servlet,却发现编译时不能通过,提示找不到sun.image.codec.jpeg.JPEGCodec和com.sun.image.codec.jpeg.JPEGImageEncoder。其servlet如下。最后,通过将jre lib目录下的rt.jar拷贝到工程,然后引入,得以通过。
class="java">
package com.jesse.onlineshop.servlet;

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.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class GenerateIdentifyCodeServlet extends HttpServlet {

	private static final long serialVersionUID = -5083098939508150302L;

	private static Random random = new Random();
	private static final char[] stores = { '1', '2', '3', '4', '5', '6', '7',
			'8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
			'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
			'Z' };

	public String getRandonString() {
		StringBuffer buff = new StringBuffer();
		for (int i = 0; i < getLength(); i++) {
			buff.append(stores[random.nextInt(stores.length)]);
		}
		return buff.toString();
	}

	public Color getRandomColor() {
		return new Color(random.nextInt(255), random.nextInt(255),
				random.nextInt(255));
	}

	public Color getReverseColor(Color color) {
		return new Color(255 - color.getRed(), 255 - color.getGreen(),
				255 - color.getBlue());
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		int width = 100;
		int height = 30;
		resp.setContentType("image/jpeg");
		String randomString = getRandonString();
		req.getSession(true).setAttribute("identifyCode", randomString);
		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(width); i < n; i++) {
			g.drawRect(random.nextInt(width), random.nextInt(height), 1, 1);
		}
		ServletOutputStream out = resp.getOutputStream();
		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
		encoder.encode(bi);
		out.flush();
		out.close();
	}

	public int getLength() {
		return Integer.parseInt(getInitParameter("codelength"));
	}

}
发表评论
用户名: 匿名