⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 getrandomimageservlet.java

📁 一个仿造淘宝的jsp网站。功能比较完善
💻 JAVA
字号:
package com.jc.taobao.gjj.action;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class GetRandomImageServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public GetRandomImageServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	Color getRandColor(int fc,int bc){
	       Random r=new Random();
	       if(fc>255)
	       fc=255;
	       if(bc>255)
	       bc=255;
	       int red=fc+r.nextInt(bc-fc);
	       int green=fc+r.nextInt(bc-fc);
	       int blue=fc+r.nextInt(bc-fc);
	       return new Color(red,green,blue);
	     }
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("image/jpeg");
		 response.setHeader("Pragma","NO-cache");
	      response.setHeader("Cache-Control","no-cache");
	      response.setDateHeader("Expires",0);
	      Random r=new Random();
	      int width=60,height=20;
	      BufferedImage pic=new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);
	      Graphics gc=pic.getGraphics();
	      gc.setColor(getRandColor(200,250));
	      gc.fillRect(0,0,width,height);
	      gc.setFont(new Font("Times New Roman",Font.PLAIN,18));
	      gc.setColor(getRandColor(160,200));
	      for(int i=0;i<200;i++)
	      {
	        int x1=r.nextInt(width);
	        int y1=r.nextInt(height);
	        int x2=r.nextInt(15);
	        int y2=r.nextInt(15);
	        gc.drawLine(x1,y1,x1+x2,y1+y2);
	      }
	      gc.setColor(getRandColor(120,240));
	      for(int i=0;i<100;i++)
	      {
	      int x=r.nextInt(width);
	      int y=r.nextInt(height);
	      gc.drawOval(x,y,0,0);
	      }
	      String RS="";
	      String rn="";
	      for(int i=0;i<4;i++)
	      {
	      rn=String.valueOf(r.nextInt(10));
	      RS+=rn;
	      gc.setColor(new Color(20+r.nextInt(110),20+r.nextInt(110),20+r.nextInt(110)));
	      gc.drawString(rn,13*i+6,16);
	      }
	      gc.dispose();
	      request.getSession().setAttribute("random",RS);
	      ImageIO.write(pic,"JPEG",response.getOutputStream());
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occure
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -