自学JSP的九大内置对象的response,以下代码为响应生成PNG


<%@ page language="java"  pageEncoding="UTF-8" contentType="image/png"%>

<%@ page import="java.awt.image.*,javax.imageio.*,java.io.*,java.awt.*" %>
<%
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>IMG</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>
    <%  //创建BufferedImage
    BufferedImage image = new BufferedImage(340,160,BufferedImage.TYPE_INT_RGB);
    //获取image 对象Graphics
    Graphics g = image.getGraphics();
    //使用Graphics画图, 图像出现在image对象
    g.fillRect(0, 0, 400, 400);
    //画 红弧
    g.setColor(new Color(255,0,0));
    g.fillArc(20, 20, 100, 100, 30, 120);
    //绿
     g.setColor(new Color(0,255,0));
    g.fillArc(20, 20, 100, 100, 150, 120);
    //蓝
     g.setColor(new Color(0,0,255));
    g.fillArc(20, 20, 100, 100, 270, 120);
     
    //写字
    g.setColor(new Color(0,0,0));
    g.setFont(new Font("Arial Black",Font.PLAIN,16));
    //
    g.drawString("Red: climb", 200, 60);
    g.drawString("Green: Running", 200, 100);
    g.drawString("Blue: Swimming", 200, 140);
    g.dispose();
     
    //将图像输出到页面
    ImageIO.write(image, "PNG", response.getOutputStream());
     
     
     %>
  </body>
</html>
Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐