一段生成验证码图片的程序
<div style="padding-right: 5.4pt; padding-left: 5.4pt; background: #e6e6e6; padding-bottom: 4px; width: 95%; padding-top: 4px;">http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifpackage test;http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport java.awt.Color;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport java.awt.Font;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport java.awt.Graphics;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport java.awt.image.BufferedImage;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport java.io.OutputStream;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport java.util.Random;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport javax.imageio.ImageIO;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport javax.servlet.http.HttpServlet;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport javax.servlet.http.HttpServletRequest;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport javax.servlet.http.HttpServletResponse;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gifpublic class ImageGenerator extends HttpServlet ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif /** *//**
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif *
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif */
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif private static final long serialVersionUID = -3938318741402322164L;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif private static Color getRandColor(int fc, int bc) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif Random random = new Random();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif if (fc > 255)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif fc = 255;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif if (bc > 255)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif bc = 255;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif int r = fc + random.nextInt(bc - fc);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif int g = fc + random.nextInt(bc - fc);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif int b = fc + random.nextInt(bc - fc);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif return new Color(r, g, b);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif private static String charsLong = "23456789abcdefghjklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif private static String charsShort = "0123456789";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif private static String chars = charsLong;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif @Override
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif public void doGet(HttpServletRequest request, HttpServletResponse response) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif try ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif int charsLength = chars.length();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif response.setHeader("Pragma", "No-cache");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif response.setHeader("Cache-Control", "no-cache");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif response.setDateHeader("Expires", 0);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif int width = 70, height = 20;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif Graphics g = image.getGraphics();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif Random random = new Random();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif g.setColor(getRandColor(200, 250));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif g.fillRect(0, 0, width, height);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif g.setFont(new Font("Times New Roman", Font.ITALIC, height));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif g.setColor(getRandColor(160, 200));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif for (int i = 0; i < 35; i++) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif int x = random.nextInt(width);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif int y = random.nextInt(height);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif int xl = random.nextInt(12);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif int yl = random.nextInt(12);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif g.drawLine(x, y, x + xl, y + yl);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif StringBuilder sRand = new StringBuilder();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif String[] fontNames = ...{ "Times New Roman", "Arial", "Book antiqua", "" };
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif for (int i = 0; i < 4; i++) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif g.setFont(new Font(fontNames3)], Font.ITALIC, height));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif char rand = chars.charAt(random.nextInt(charsLength));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif sRand.append(rand);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif g.drawString(String.valueOf(rand), 16 * i + random.nextInt(6) + 3, height - random.nextInt(4));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif g.setColor(getRandColor(160, 200));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif for (int i = 0; i < 30; i++) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif int x = random.nextInt(width);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif int y = random.nextInt(height);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif int xl = random.nextInt(width);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif int yl = random.nextInt(width);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif g.drawLine(x, y, x + xl, y + yl);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif request.getSession().setAttribute("Login_Image_Code", sRand.toString());
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif g.dispose();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif try ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif Thread.sleep(100);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif } catch (Exception ex) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif OutputStream os = response.getOutputStream();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif ImageIO.write(image, "JPEG", os);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif os.flush();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif os.close();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif } catch (Exception ex) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
页:
[1]