关闭

用java生成一个附加码。

500人阅读 评论(0) 收藏 举报

好久没有写个东西了.最近好忙.希望大家能够用的上这个附加码.
/**
*@作者 pxjianke
*@目标  生成附加码,免得别人破解密码
*
*/

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServlet;
import java.io.IOException;
import javax.servlet.ServletOutputStream;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import java.awt.Font;
import java.awt.image.BufferedImage;
import java.awt.Graphics;
import java.awt.Color;
import com.encrypts.StringEncrypt;

public class AuthImgServlet
    extends HttpServlet {

  private static final String CONTENT_TYPE = "text/html; charset=UTF-8";
  private static char numbersAndLetters[] =
      "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
  private Font mFont = new Font("Times New Roman", Font.BOLD, 14);
  public AuthImgServlet() {
  }

  //Initialize global variables
  public void init() throws ServletException {
  }

  public void doGet(HttpServletRequest request, HttpServletResponse response) throws
      ServletException, IOException {
    response.setContentType("image/jpeg");
    ServletOutputStream out = response.getOutputStream();
    BufferedImage image = new BufferedImage(70, 17, BufferedImage.TYPE_INT_RGB);
    Graphics g = image.getGraphics();
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, 70, 17);
    g.setColor(Color.red);
    g.setFont(mFont);
    String temp=getStringCode(6);
    HttpSession hs=request.getSession();
    hs.setAttribute("createImage",(new StringEncrypt(temp.toLowerCase())).toString());
    for(int i=0;i<6;i++){
      g.drawString(temp.substring(i,i+1),i*11+2,14);
    }
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
    encoder.encode(image);
    out.close();
  }

  private String getStringCode(int len) {
    StringBuffer strCode = new StringBuffer();
    int nRand = (int) Math.round(Math.random() * 100D);
    for (int i = 0; i < len; i++) {
      nRand = (int) Math.round(Math.random() * 100D);
      strCode.append(numbersAndLetters[nRand % (numbersAndLetters.length - 1)]);
    }
    return strCode.toString();
  }

  //Process the HTTP Post request
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws
      ServletException, IOException {
    doGet(request, response);
  }

  //Clean up resources
  public void destroy() {
  }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:14733次
    • 积分:290
    • 等级:
    • 排名:千里之外
    • 原创:13篇
    • 转载:6篇
    • 译文:0篇
    • 评论:0条
    文章分类