欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

将JSP在内存生成的图片显示到页面

程序员文章站 2024-02-23 15:52:52
imaging_toweb.jsp<%@ page autoflush="false"  import="java.awt.*,java.awt.image...

imaging_toweb.jsp

<%@ page autoflush="false"  import="java.awt.*,java.awt.image.*,com.sun.image.codec.jpeg.*,java.util.*"%>
<%
    string imagestr = request.getparameter("id_text");
    if(imagestr==null || imagestr.equals(""))
    {
        response.setcontenttype("text/html; charset=gb2312");
 %>
<html>
 <head>
  <title>http://www.lionsky.net</title>
  <meta http-equiv="content-type" content="text/html; charset=gb2312">
 </head>
 <body>
  <form id="form1" method="post">
    <input type="text" id="id_text" name=id_text>
    <input type=submit value="go">
  </form>
 </body>
</html>

<%
       }
       else
       {
        out.clear();
        response.setcontenttype("image/jpeg");
        response.addheader("pragma","no-cache");
        response.addheader("cache-control","no-cache");
        response.adddateheader("expries",0);
        int width=300, height=100;
        bufferedimage image = new bufferedimage(width, height, bufferedimage.type_int_rgb);
        graphics g = image.getgraphics();
        //以下填充背景颜色
        g.setcolor(color.lightgray);
        g.fillrect(0, 0, width, height);
        string random="random";
        //以下设置前景色
        g.setcolor(color.black);
        g.drawstring("http://www.knowsky.com",10,20);
        g.drawstring("author:lion[lion-a@sohu.com]",10,40);
        g.drawline(10,50,290,50);
        g.drawstring(imagestr,10,70);
        g.dispose();
        servletoutputstream outstream = response.getoutputstream();
        jpegimageencoder encoder =jpegcodec.createjpegencoder(outstream);
        encoder.encode(image);
        outstream.close();
    }

%>