package com;

 import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random; import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder; public class IdentityServlet extends HttpServlet {
public static final char[] CHARS = { '2', '3', '4', '5', '6', '7', '8',
'9', '0', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
'L', 'M', 'N', 'O', 'P', 'K', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z' }; public static Random random = new Random(); public static String getRandomString() {// 随机获取6位
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < 6; i++) {
buffer.append(CHARS[random.nextInt(CHARS.length)]);// 随机获取一个字符 }
return buffer.toString();
} public static Color getRandomColor() {
return new Color(random.nextInt(255), random.nextInt(255),
random.nextInt(255));
} public static Color getReverseColor(Color c) {// 取反色
return new Color(255 - c.getRed(), 255 - c.getGreen(),
255 - c.getBlue()); } public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("image/jpeg");
String randomString = getRandomString();
request.getSession(true).setAttribute("randomString", randomString);
int width = 100;
int height = 30;
Color color = getRandomColor();
Color reverseColor = getReverseColor(color);
BufferedImage bImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2d = bImage.createGraphics();
graphics2d.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 16));
graphics2d.setColor(color);
graphics2d.fillRect(0, 0, width, height);
graphics2d.setColor(reverseColor);
graphics2d.drawString(randomString, 18, 20);
for (int i = 0, n = random.nextInt(100); i < n; i++) {
graphics2d.drawRect(random.nextInt(width), random.nextInt(height),
1, 1); }
ServletOutputStream outputStream = response.getOutputStream();
JPEGImageEncoder coder = JPEGCodec.createJPEGEncoder(outputStream);
coder.encode(bImage);
outputStream.flush();
} }

需要注意的是在myEclipse中处理图片,需要引入两个包:
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
报错:
Access restriction: The type JPEGImageEncoder is not accessible due to
restriction on required library C:\Java\jre1.6.0_07\lib\rt.jar

此时解决办法:
MyEclipse默认把这些受访问限制的API设成了ERROR。只要把Windows-Preferences-Java-Complicer-Errors/Warnings里面的Deprecated
 and restricted API中的Forbidden references(access rules)选为Warning就可以编译通过。

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body> <img src = "servlet/IdentityServlet" id = "identity" onload = "btn.disabled=false;"/>
<input type = button value="next" onclick="reloadImage()" id = "btn"/>
</body> <script>
function reloadImage(){
document.getElementById('btn').disabled = true;
document.getElementById('identity').src = 'servlet/IdentityServlet?ts='+new Date().getTime(); }
</script>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>servlet</display-name>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>IdentityServlet</servlet-name>
<servlet-class>com.IdentityServlet</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>IdentityServlet</servlet-name>
<url-pattern>/servlet/IdentityServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>

最新文章

  1. Hadoop中JAVA不经过Catch(Exception e)直接到finally或者退出原因
  2. SQL获取时间段内的所有月份
  3. Step By Step(Lua字符串库) (转)
  4. Oracle中decode方法的作用
  5. 用sinopia搭建npm私服
  6. JqueryEasyUI 增加选项卡
  7. Bootstrap 基础教程
  8. shell字符串
  9. android退出activity的方式总结(一)
  10. actor简介
  11. 【Shell脚本学习指南笔记】重定向文件描述符 2&gt;&amp;1
  12. 利用 DirectShow 开发自己的 Filter
  13. JSP自定义标签必知必会
  14. 《图解HTTP》读书笔记(二:各种协议与HTTP协议之间的关系)
  15. Nginx与PHP-FPM运行原理详解
  16. 转载:深入浅出Zookeeper
  17. mac 本上对 rar 压缩包解压
  18. Maven依赖中的scope详解,在eclipse里面用maven install可以编程成功,到服务器上用命令执行报VM crash错误
  19. oracle listagg within group
  20. xilinx IP核配置,一步一步验证Xilinx Serdes GTX最高8.0Gbps

热门文章

  1. ACdream1187-Rational Number Tree-模拟/找规律
  2. python -- 面向对象三大特性
  3. SpringCloud基础篇AOP之拦截优先级详解
  4. Hdoj 1009.FatMouse&#39; Trade 题解
  5. Leetcode 125.验证回文串 By Python
  6. 【BZOJ3668】【NOI2014】起床困难综合症(贪心)
  7. 用DotNetOpenAuth实现基于OAuth 2.0的web api授权 (一)Getting Start
  8. 8bit数据 转换为 16bit数据的四种方法
  9. 洛谷 P4378 [USACO18OPEN]Out of Sorts S(树状数组求冒泡排序循环次数)
  10. 在VS2010中使用Git【图文】转