从数据库读取的二进制的图片,如何画在jsp上,类似于生成验证码那种

解决方案 »

  1.   

    JFreeChart框架中生成饼状图,要画在jsp上,怎么实现啊。。
      

  2.   

    以前写的代码发给你参考,直接通过servlet请求在页面显示图片,页面上通过<img src="getcode.jpg" width="50" height="20" onclick="this.src='getcode.jpg?r='+new Date()" alt="图片看不清?点击换一张"/>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.ServletOutputStream;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;@SuppressWarnings("serial")
    public class RandomIMG extends HttpServlet { private int width=50;//图像宽度
    private int height=20;//图像高度

    /**
     * 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
     */
    public void service(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException { response.setContentType("image/jpeg");
    ServletOutputStream sos=response.getOutputStream();

    Random r=new Random();

    //在内存中创建图像,宽度为width,高度为height
    BufferedImage pic=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
    //获取图形上下文环境
    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));

    //随机产生200条干扰线条,使图像中的认证码不易被其他分析程序探测到
    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);
    }

    //随机产生100个干扰点,使图像中的验证码不易被其他分析程序探测到
    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);
    }

    //随机产生4个数字的验证码
    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+1, 16);
    }
    //释放图形上下文环境
    gc.dispose();

    // if(request.getSession().getAttribute("hk_verify")!=null || request.getSession().getAttribute("hk_verify")!=""){
    // request.getSession().setAttribute("hk_verify", null);
    // request.getSession().removeAttribute("hk_verify");
    // }
    //将认证码rs存放到session中共享
    request.getSession().setAttribute("seccode", rs);

    //输出生成后的验证码图像到页面
    ImageIO.write(pic, "JPEG", sos);
    sos.flush();
    sos.close();
    }

    private 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);
    }}
      

  3.   

    分两步来完成1. 做一个图片Servlet,其响应的Content-type为"image/jpg"之类。图片路径作为该Servlet的请求参数送入,Servlet直接将数据库中对应的字节流输出为Servlet自己的响应体。2. 页面中用正常的HTML <img控件引用该Servlet,src中写Servlet的URL及请求参数。
      

  4.   


    如何Servlet中输出图片呢。
      

  5.   

    LZ明显是不看代码我发的代码那么清楚
    重点在这一段:...
    //输出生成后的验证码图像到页面
            ImageIO.write(pic, "JPEG", sos);
            sos.flush();
            sos.close();
    ...