文件名从 src/java/myUtils/GenerateVcodeServlet.java 修改 |
| | |
| | | package myUtils; |
| | | package my.utils; |
| | | |
| | | import java.awt.Color; |
| | | import java.awt.Font; |
| | |
| | | |
| | | /** |
| | | * Servlet implementation class GenerateVcodeServlet |
| | | * @author |
| | | */ |
| | | @WebServlet("/GenerateVcodeServlet") |
| | | public class GenerateVcodeServlet extends HttpServlet { |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | private static int m155 =155; |
| | | |
| | | private static int m6 =6; |
| | | |
| | | private static int m255 =255; |
| | | |
| | | public GenerateVcodeServlet() { |
| | | } |
| | | |
| | | @Override |
| | | protected void doGet(HttpServletRequest req, HttpServletResponse resp) |
| | | throws ServletException, IOException { |
| | | resp.setContentType("image/jpeg"); |
| | |
| | | g.fillRect(0, 0, width, height); |
| | | g.setFont(new Font("Times New Roman", 0, 18)); |
| | | g.setColor(getRandColor(160, 200)); |
| | | for (int i = 0; i < 155; i++) { |
| | | for (int i = 0; i < m155; i++) { |
| | | int x = random.nextInt(width); |
| | | int y = random.nextInt(height); |
| | | int xl = random.nextInt(12); |
| | |
| | | } |
| | | |
| | | String sRand = ""; |
| | | for (int i = 0; i < 6; i++) { |
| | | for (int i = 0; i < m6; i++) { |
| | | String rand = String.valueOf(random.nextInt(10)); |
| | | sRand = (new StringBuilder(String.valueOf(sRand))).append(rand) |
| | | .toString(); |
| | |
| | | out.flush(); |
| | | } |
| | | |
| | | @Override |
| | | protected void doPost(HttpServletRequest req, HttpServletResponse resp) |
| | | throws ServletException, IOException { |
| | | doGet(req, resp); |
| | |
| | | |
| | | private Color getRandColor(int fc, int bc) { |
| | | Random random = new Random(); |
| | | if (fc > 255) |
| | | fc = 255; |
| | | if (bc > 255) |
| | | bc = 255; |
| | | if (fc > m255) { |
| | | fc = m255; |
| | | } |
| | | if (bc > m255) { |
| | | bc = m255; |
| | | } |
| | | int r = fc + random.nextInt(bc - fc); |
| | | int g = fc + random.nextInt(bc - fc); |
| | | int b = fc + random.nextInt(bc - fc); |