簡単に登録したチェック画像の設定
1446 ワード
@WebServlet("/image")
public class ImageServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//
//BufferedImage.TYPE_INT_RGB
BufferedImage image=new BufferedImage(80, 40, BufferedImage.TYPE_INT_RGB);
//
Graphics g = image.getGraphics();
//
g.setColor(Color.BLACK);
//
g.fillRect(0, 0, 80, 40);
//
String str="";
//
Random random=new Random();
for(int i=0;i<5;i++){
char c=(char)(random.nextInt(26)+65);// A-Z
str+=c;
}
//
g.setColor(Color.WHITE);
g.setFont(new Font(null, Font.ITALIC , 20));
g.drawString(str, 5, 20);
// “png"
ImageIO.write(image, "png", response.getOutputStream());
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!--image servlet -->
<img alt=" " src="image">
</body>
</html>