服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > J2EE > Servlet/Jsp > 查看文档

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

  由于例子比较简单,就不多说了,大家看代码就能明白。

  例子下载:http://www.lionsky.net/mywebsite/downsoft/list.aspx?id=215





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.lionsky.net",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();
    }

%>





扫描关注微信公众号