CAPTCHA image generation and problem solving can not be displayed

IntelliJ IDEA codes generated page-related modules:

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

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public final class ImageUtil {
    
    private static final char[] chars = { '0', '1', '2', '3', '4', '5', '6',
            '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I' };
    private static final int SIZE = 4;
    private static final int LINES = 5;
    private static final int WIDTH = 80;
    private static final int HEIGHT = 40;
    private static final int FONT_SIZE = 30;

    public static Map<String, BufferedImage> createImage() {
        StringBuffer sb = new StringBuffer();
        BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
                BufferedImage.TYPE_INT_RGB);
        Graphics graphic = image.getGraphics();
        graphic.setColor(Color.LIGHT_GRAY);
        graphic.fillRect(0, 0, WIDTH, HEIGHT);
        Random ran = new Random();
        
        for (int i = 1; i <= SIZE; i++) {
            int r = ran.nextInt(chars.length);
            graphic.setColor(getRandomColor());
            graphic.setFont(new Font(null, Font.BOLD + Font.ITALIC, FONT_SIZE));
            graphic.drawString(chars[r] + "", (i - 1) * WIDTH / SIZE,
                    HEIGHT / 2);
            sb.append(chars[r]);
        }
        
        for (int i = 1; i <= LINES; i++) {
            graphic.setColor(getRandomColor());
            graphic.drawLine(ran.nextInt(WIDTH), ran.nextInt(HEIGHT),
                    ran.nextInt(WIDTH), ran.nextInt(HEIGHT));
        }
        Map<String, BufferedImage> map = new HashMap<String, BufferedImage>();
        map.put(sb.toString(), image);
        return map;
    }

    public static Color getRandomColor() {
        Random ran = new Random();
        Color color = new Color(ran.nextInt(256), ran.nextInt(256),
                ran.nextInt(256));
        return color;
    }

    public static InputStream getInputStream(BufferedImage image)
            throws IOException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
        encoder.encode(image);
        byte[] imageBts = bos.toByteArray();
        InputStream in = new ByteArrayInputStream(imageBts);
        return in;
    }

}
Generating class codes ImageUtill

 

    /**
     * 产生登录页面验证码
     */
    @RequestMapping("/createImage.do")
    public void createImage(
            HttpServletResponse response, HttpSession session)
            throws Exception {
        Map<String, BufferedImage> imageMap = ImageUtil.createImage();
        String code = imageMap.keySet().iterator().next();
        session.setAttribute("imageCode", code);
        
        BufferedImage image = imageMap.get(code);
        
        response.setContentType("image/jpeg");
        OutputStream ops = response.getOutputStream();
        ImageIO.write(image, "jpeg", ops);
        ops.close();
    }

JSP pages call (fragment, into the src ceateImage.do above):

<TR> 
<TD class = "login_info"> codes: </ TD>
<TD class = "width70"> <INPUT name = "code" type = "text" class = "width70" ID = "code" the onfocus = "set_msg ( 'code_msg', '');" /> </ TD>
<TD> <IMG the src = "createImage.do" Alt = "codes" title = "click to replace" id = "code_image" onclick = " Change (); "/> </ TD>
<TD> <span class =" required "ID =" code_msg "> </ span> </ TD>
</ TR>

program determines is correct!

Java programs can be written to verify the image generation:









however: CAPTCHA image can not be displayed in a web page. The error message is not written to the Tomcat temp directory, but the temp directory is certainly there because the temp directory must be written to administrator privileges.
Solution:
enter the tomcat installation directory: C: \ Program Files \ Apache Software Foundation \ Tomcat 8.

Check all the permissions for all users, problem solved! ! !

 

 

 

Guess you like

Origin www.cnblogs.com/yinweifeng/p/11308177.html