当前位置: 代码迷 >> Java Web开发 >> reading encoded JPEG Stream ?()
  详细解决方案

reading encoded JPEG Stream ?()

热度:935   发布时间:2016-04-17 10:38:50.0
reading encoded JPEG Stream ?(在线等)
我的一个验证码,在本地测试的时候是没有问题的,但是放到服务器上去之后,会经常报这个错误。
下面是错误化码:
HTML code
ava.io.IOException: reading encoded JPEG Stream     at sun.awt.image.codec.JPEGImageEncoderImpl.writeJPEGStream(Native Method)    at sun.awt.image.codec.JPEGImageEncoderImpl.encode(JPEGImageEncoderImpl.java:472)    at sun.awt.image.codec.JPEGImageEncoderImpl.encode(JPEGImageEncoderImpl.java:228)    at org.apache.jsp.pages.random_jsp._jspService(random_jsp.java:107)

下面是random.jsp的代码:
HTML code
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><%@ page autoFlush="false"  import="java.util.*,java.awt.*,java.awt.image.*,com.sun.image.codec.jpeg.*,java.util.*" %><%//set Chinese Char //Cody by JarryLi@gmail.com;//homepage:jiarry.126.comrequest.setCharacterEncoding("GBK");response.setCharacterEncoding("GBK");response.setContentType("text/html; charset=GBK");%><%String chose="0123456789";//abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZchar display[]={'0',' ','0',' ','0',' ','0'},ran[]={'0','0','0','0'},temp;Random rand=new Random();for(int i=0;i<4;i++){ temp=chose.charAt(rand.nextInt(chose.length())); display[i*2]=temp; ran[i]=temp; } String random=String.valueOf(display);session.setAttribute("random",String.valueOf(ran)); %><%        out.clear();              response.setContentType("image/jpeg");        response.addHeader("pragma","NO-cache");        response.addHeader("Cache-Control","no-cache");        response.addDateHeader("Expries",0);        int width=80, height=30;        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);        Graphics g = image.getGraphics();        //以下填充背景颜色        Color c = new Color(Integer.parseInt("000000",16));        g.setColor(c);        g.fillRect(0, 0, width, height);       //设置字体颜色        g.setColor(Color.WHITE);        Font font=new Font("Arial",Font.PLAIN,20);        g.setFont(font);       // g.drawString(random,5,14);        g.drawString(random,5,20);        g.dispose();        //ServletOutputStream outStream = response.getOutputStream();        JPEGImageEncoder encoder =JPEGCodec.createJPEGEncoder(response.getOutputStream());        encoder.encode(image);//就是报这个错误          //outStream.close();         out.clear();         out = pageContext.pushBody();      %>



------解决方案--------------------
把这些去掉
JPEGImageEncoder encoder =JPEGCodec.createJPEGEncoder(response.getOutputStream());
encoder.encode(image);//就是报这个错误
 
//outStream.close();
out.clear();
out = pageContext.pushBody(); 
用这个试试,效果如何
ImageIO.write(image, "JPEG", response.getOutputStream());
------解决方案--------------------
探讨
把这些去掉
JPEGImageEncoder encoder =JPEGCodec.createJPEGEncoder(response.getOutputStream());
encoder.encode(image);//就是报这个错误

//outStream.close();
out.clear();
out = pageContext.pushBody();
用这个试试,效果如何
ImageIO.write(image, "JPEG", response.getOutputStream());

------解决方案--------------------
Java code
OutputStream os = response.getOutputStream();ImageIO.write(image,"JPEG",os);os.flush();os.close();response.flushBuffer();out.clear();out = pageContext.pushBody();
  相关解决方案