不知道你怎么写的bean,贴出来看看

解决方案 »

  1.   

    就是把上面那断程序,放在一个bean里,在使用jsp页面调用!我的bean还有很多功能,具体的功能就我说的那样!1。使用以下代码,抛出异常java.io.IOException: reading encoded JPEG Stream
       BufferedImage image = new BufferedImage(100,100, BufferedImage.TYPE_INT_RGB);
       ServletOutputStream sos = response.getOutputStream();
       JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(sos);
       encoder.encode(image);
       sos.close();2。使用以下代码,抛出异常ClientAbortException:  java.net.SocketException: Connection reset by peer: socket write error   ServletOutputStream sos = response.getOutputStream();
       ImageIO.write(image, "jpeg", sos);请问怎么能控制这种异常!,请问是不是让程序“异步”!
      

  2.   

    把sos.close();替换成sos.flush ();
    试试行不行?
      

  3.   

    image.jsp:
    <%@ page contextType="img/jpeg" %>
    <%
      out.clear();//一定要这一行
      ServletOutputStream sos = response.getOutputStream();
      //向sos中输出Image,在此之后不能输出任何文本数据
    %>
    使用:
    <img src="image.jsp">
      

  4.   

    没有发生这种情况,不管怎么刷新,可能是楼主在其它地方有错。<%@ page contentType="image/jpeg" 
        import="java.awt.*,
            java.awt.image.*,
    java.awt.Color,
                java.awt.Font,
                java.awt.Graphics,
                java.awt.Image,
                java.awt.image.BufferedImage,
                java.io.IOException,
                java.util.Random,
    com.sun.image.codec.jpeg.*,
    java.util.*"
    %>
    <%
    String sRand = null;
    int width = 80,height = 20;
        BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR); // 在内存中创建图象
        Graphics g = image.getGraphics();// 获取图形上下文
        Random   random = new  Random();//生成随机类  
        int fc = 88,bc = 100;
        if(fc>255) fc=255;
        if(bc>255) bc=255;
        int r=fc+random.nextInt(bc-fc);
        int gr=fc+random.nextInt(bc-fc);
        int b=fc+random.nextInt(bc-fc);
                    
    Color color =  new Color(r,gr,b);
        g.setColor(color);// 初始化背景色
        g.fillRect(0,0,width,height);//初始化
        //g.drawRect(0,0,width-1,height-1);//画边框
        g.setFont(new Font("Times New Roman",Font.PLAIN,18));//设定字体    fc = 100;bc = 200;
                    
        r=fc+random.nextInt(bc-fc);
        gr=fc+random.nextInt(bc-fc);
        b=fc+random.nextInt(bc-fc);    color =  new Color(r,gr,b);
        g.setColor(color);//重新设置干扰线颜色
        random = new  Random();//生成随机类 
        for (int i=0; i<88; i++){
            int x,y;
            int x1,y1;
            x = random.nextInt(width);
            y = random.nextInt(height);
            x1 = random.nextInt(88);
            y1 = random.nextInt(88);
            g.drawLine(x,y,x+x1,y+y1);
        }
        
    for (int i=0;i<4;i++){
    String rand=String.valueOf(random.nextInt(10));
            sRand+=rand;
            // 将认证码显示到图象中
            g.setColor(new Color(5+random.nextInt(50),10+random.nextInt(80),10+random.nextInt(80)));
            //调用函数出来的颜色相同,直接生成
            g.drawString(rand,13*i+6,16);
        }
        
    g.dispose(); ServletOutputStream sos = response.getOutputStream();
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(sos);
    encoder.encode(image);
    %>
      

  5.   

    你写在bean中,就会报错!。
    在jsp页面不会报错!(可能是被jsp 异常给捕获了)
      

  6.   

    "java.io.IOException: reading encoded JPEG Stream"是因为有些资源没有close导致的,不继的去刷新,有没有可能:新创建的资源,没有close掉,又创建一次?