java 01
响应行
组成:协议/版本 响应状态码 状态码描述
响应状态码:服务器告诉客户端浏览器本次请求和响应的一个状态
态码都是3位数字
分类:
1xx:服务器就收客户端消息,但没有接受完成,等待一段时间后,发送1xx多状态码
2xx:成功。代表:200
3xx:重定向。代表:302(重定向),304(访问缓存)
4xx:客户端错误
404(请求路径没有对应的资源)
405:请求方式没有对应的doXxx方法
5xx:服务器端错误。代表:500(服务器内部出现异常)
1 public static final int WIDTH=120; 2 public static final int HEIGHT=35; 3 public void doGet(HttpServletRequest request, HttpServletResponse response) 4 throws ServletException, IOException { 5 6 BufferedImage image=new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); 7 Graphics g=image.getGraphics(); 8 9 //1 设置背景颜色 10 setBackGround(g); 11 12 //2 设置边框 13 setBorder(g); 14 15 //3 画出干扰线 16 drawRandomLine(g); 17 18 //4 写随机数 19 drawRandomNum((Graphics2D)g); 20 21 //5 图形写给浏览器 22 response.setContentType("image/jpeg"); 23 response.setHeader("Expires", "-1"); 24 response.setHeader("Cache-Control", "no-cache"); 25 response.setHeader("Pragma", "no-cache"); 26 ImageIO.write(image, "jpg", response.getOutputStream()); 27 } 28 29 30 private void drawRandomNum(Graphics2D g) { 31 g.setColor(Color.RED); 32 g.setFont(new Font("宋体",Font.BOLD,20)); 33 String base = "\u7684\u4e00\u4e86\u662f\u6211\u4e0d\u5728\u4eba\u4eec\u6709\u6765\u4ed6\u8fd9\u4e0a\u7740\u4e2a\u5730\u5230\u5927\u91cc\u8bf4\u5c31\u53bb\u5b50\u5f97\u4e5f\u548c\u90a3\u8981\u4e0b\u770b\u5929\u65f6\u8fc7\u51fa\u5c0f\u4e48\u8d77\u4f60\u90fd\u628a\u597d\u8fd8\u591a\u6ca1\u4e3a"; 34 int x=5; 35 for(int i=0;i<4;i++){ 36 int degree=new Random().nextInt()%30; 37 String ch=base.charAt(new Random().nextInt(base.length()))+""; 38 g.rotate(degree*Math.PI/180, x, 20); 39 g.drawString(ch, x, 20); 40 g.rotate(-degree*Math.PI/180, x, 20); 41 x+=30; 42 } 43 } 44 45 46 private void drawRandomLine(Graphics g) { 47 g.setColor(Color.GREEN); 48 for(int i=0;i<5;i++){ 49 int x1=new Random().nextInt(WIDTH); 50 int y1=new Random().nextInt(HEIGHT); 51 52 int x2=new Random().nextInt(WIDTH); 53 int y2=new Random().nextInt(HEIGHT); 54 g.drawLine(x1, y1, x2, y2); 55 } 56 57 } 58 59 60 private void setBorder(Graphics g) { 61 g.setColor(Color.BLUE); 62 g.drawRect(1, 1, WIDTH-2, HEIGHT-2); 63 } 64 65 66 private void setBackGround(Graphics g) { 67 g.setColor(Color.WHITE); 68 g.fillRect(0, 0, WIDTH, HEIGHT); 69 }
                    
                
                
            
        
浙公网安备 33010602011771号