关于socket验证码的报错问题
JSP验证码刷新报错:java.net.SocketException: Connection reset by peer: socket write error 浏览次数:2039次悬赏分:30 | 解决时间:2010-1-26 20:52 | 提问者:zhou7530169
估半是我生成验证码图片的JSP文件的问题,我是先写了一个类,然后导入JSP文件中,把图片地址连到这个文件。
我写的工具类代码如下:
public class MakePicNum {
private char[] mapTable = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8',
'9' };
public String getCertPic(int width,int height,OutputStream os){
if(width<=0)width=60;
if(height<=0)height=20;
BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics g=image.getGraphics();
g.setColor(new Color(0xDCDCDC));
g.fillRect(0, 0, width, height);
g.setColor(Color.black);
g.drawRect(0, 0, width-1, height-1);
String strEnsure="";
for(int i=0;i<4;i++){
strEnsure+=mapTable[(int)(mapTable.length*Math.random())];
}
g.setColor(Color.black);
g.setFont(new Font("Atlantic Inline",Font.PLAIN,18));
String str=strEnsure.substring(0, 1);
g.drawString(str, 8, 17);
str=strEnsure.substring(1, 2);
g.drawString(str, 20, 15);
str=strEnsure.substring(2, 3);
g.drawString(str, 35, 18);
str=strEnsure.substring(3, 4);
g.drawString(str, 45, 15);
Random rand=new Random();
for(int i=0;i<10;i++){
int x=rand.nextInt(width);
int y=rand.nextInt(height);
g.drawOval(x, y, 1, 1);
}
g.dispose();
try{
ImageIO.write(image,"JPEG",os);
}catch(Exception e){
e.printStackTrace();
return "";
}
return strEnsure;
}
}
生成图片的JSP代码为:
<%@page contentType="image/jpeg"%>
<jsp:useBean id="image" scope="page" class="util.MakePicNum"/>
<%String str=image.getCertPic(0,0,response.getOutputStream());
session.setAttribute("certnum",str);
out.clear();
response.flushBuffer();
%>
大家帮我看一看是哪的问题,我已经弄了六个小时了,网上找到些答案还是不能解决问题。补充一点,如果不写out.clear();
response.flushBuffer();会多报一个错:getOutputStream() has already been called for this response
最佳答案 太奇怪,我测试了一下你的代码,很好用阿,采用两种方式都没有问题
直接访问img.jsp或者访问html文件的img标签,都没有问题。
补充:
我是在本地访问的,如果你访问其它计算机,那么看看是否是防火墙的问题。代码应该没有问题。
分享给你的朋友吧:
i贴吧
新浪微博
腾讯微博
QQ空间
人人网
豆瓣
MSN
对我有帮助
3回答时间:2010-1-26 11:38 | 我来评论
向TA求助 回答者: zeus3288 | 七级采纳率:50%
擅长领域: JAVA相关 数据库 C#/.NET
参加的活动: 暂时没有参加的活动
提问者对于答案的评价:
劳您费心了,还帮我试了一下。把连接图片改成这样<img src='makePic' id='pic' onclick="chk_image();"/>
相关内容
2011-4-15 ClientAbortException: java.net.SocketException: Connection reset by... 2 2011-3-13 tomcat环境中使用 MultipartRequest 类 后台报错 java.net.SocketExcept... 2009-5-7 java.net.SocketException: Connection reset 15 2011-5-30 启动eclipse —help-eclipse MarkPlace 报java.net.SocketException: Co... 1 2006-8-30 java.sql.SQLException: Connection reset by peer: socket write error 8 更多相关问题>>
查看同主题问题: connection reset 验证码 刷新
等待您来回答1回答5连接池 Connection Pool 问题2回答15JAVA/JSP删除文件语句问题~~!2回答5JSP启动TOMCAT时的错误Catalina.stop:0回答org.apache.jasper.JasperException: /dbselect.jsp(2,17) quote symb...2回答在jsp中将图片存入数据库发生的一些问题更多等待您来回答的问题>>
其他回答 共2条
建议你使用 Kaptcha 验证码插件,详细访问http://code.google.com/p/kaptcha/ 这里,很简单实用。 回答者: sokoo108 | 七级 | 2010-1-26 08:24
生成图片的JSP代码为:
这个页面在String str前加一句话:response.reset();去掉,out.clear。