错误:

严重: Servlet.service() for servlet jsp threw exception java.lang.IllegalStateException: getOutputStream() has already been called

输出验证码的类:

package com.jxc.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random; import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
/**
* 验证码输出类
* @author Pan
*
*/
public class GraphisVerfCode { private HttpServletResponse response;
private Point point;
private int linesize;
private String code="";
public GraphisVerfCode(HttpServletResponse response,Point p,int linesize){ this.response=response;
this.point=p;
this.linesize=linesize;
code=RandomCode.getRandomString(4);
}
public String getCode() {
return this.code;
}
public void Draw() throws IOException{
//输出类型
response.setContentType("image/jpeg");
response.setHeader("Pragma", "No-cache");//设置响应头信息,告诉浏览器不要缓存此内容
response.setHeader("Cache-Control", "no-cache");
//创建对象
BufferedImage bImage=new BufferedImage(point.x, point.y,BufferedImage.TYPE_INT_RGB);
Graphics2D g=bImage.createGraphics();
//前景色 //填充矩形
g.fillRect(0, 0, point.x, point.y); drowString(g);
//干扰线
drowLine(g,this.linesize);
//输出图片
ServletOutputStream out = response.getOutputStream();
ImageIO.write(bImage, "jpeg", out);
out.flush();
out.close();
}
//干扰线条
private void drowLine(Graphics g,int linesize){
Random random=new Random(System.currentTimeMillis()); for (int i = 0; i < linesize; i++) {
int x = random.nextInt(point.x);
int y = random.nextInt(point.y);
int xl = random.nextInt(13);
int yl = random.nextInt(15);
g.setColor(new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)));
g.drawLine(x, y, x+xl, y+yl);
}
} private void drowString(Graphics g){
g.setFont(new Font("微软雅黑",Font.BOLD,22));
Random random=new Random(System.currentTimeMillis());
//循环绘制每个字的颜色
for(int i=0;i<code.length();i++){
g.setColor(new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)));
g.drawString(code.charAt(i)+"", 20*i, 25);
}
}
}

最开始使用的时候,没有调用   out.flush();这个方法,就一直报错。加上就好了。

严重: Servlet.service() for servlet jsp threw exception java.lang.IllegalStateException: getOutputStream() has already been called的更多相关文章

  1. 严重: Servlet.service() for servlet jsp threw exception java.lang.IllegalStateException: getOutputStream() has already been called for this response

    严重: Servlet.service() for servlet jsp threw exception    java.lang.IllegalStateException: getOutputS ...

  2. 严重: Servlet.service() for servlet [jsp] threw exception java.lang.NullPointerException

    五月 04, 2018 11:53:24 上午 org.apache.catalina.core.ApplicationDispatcher invoke 严重: Servlet.service() ...

  3. 异常-User class threw exception: java.lang.IllegalStateException: Cannot call methods on a stopped SparkContext.

    1 详细信息 User class threw exception: java.lang.IllegalStateException: Cannot call methods on a stopped ...

  4. 报错!!!Servlet.service() for servlet [action] in context with path [/myssh] threw exception [java.lang.NullPointerException] with root cause java.lang.NullPointerException

    这个为什么报错啊~~ at com.hsp.basic.BasicService.executeQuery(BasicService.java:33) 这个对应的语句是   Query query = ...

  5. Servlet.service() for servlet UserServlet threw exception java.lang.NullPointerException 空指针异常

    错误付现: 严重: Servlet.service() for servlet UserServlet threw exceptionjava.lang.NullPointerException at ...

  6. 严重: End event threw exception java.lang.IllegalArgumentException: Can't convert argument: null

    堆栈信息: 2014-6-17 10:33:58 org.apache.tomcat.util.digester.Digester endElement 严重: End event threw exc ...

  7. Request processing failed; nested exception is java.lang.IllegalStateException: getOutputStream() has already been called for this response

    问题分析: 在ServletRequest servletRequest中已经存在一个项目名称,此时,又用项目名称访问 http://localhost:8080/rent/pdf/preview r ...

  8. maven项目跳转页面报空指针错误 Servlet.service() for servlet 异常

    Servlet.service() for servlet jsp threw exceptionjava.lang.NullPointerExceptionat org.jaronsource.ms ...

  9. Caused by java.lang.IllegalStateException Not allowed to start service Intent { cmp=com.x.x.x/.x.x.xService }: app is in background uid UidRecord问题原因分析(二)

    应用在适配Android 8.0以上系统时,会发现后台启动不了服务,会报出如下异常,并强退: Fatal Exception: java.lang.IllegalStateException Not ...

随机推荐

  1. backbone HTTP方法中 options参数

    wait: 可以指定是否等待服务端的返回结果再更新model.默认情况下不等待url: 可以覆盖掉backbone默认使用的url格式attrs: 可以指定保存到服务端的字段有哪些,配合options ...

  2. Java代码优化策略

    1.生成对象时,合理分配空间和大小:new ArrayList(100); 2.优化for循环: Vector vect = new Vector(1000); For(int i=0; i<v ...

  3. Centos6.4安装JDK

    链接地址:http://www.iteye.com/topic/1130311 1.先看看OpenJDK的安装包 $ rpm -qa |grep javatzdata-java-2013b-1.el6 ...

  4. [Swust OJ 412]--医院设置(floyd算法)

    题目链接:http://acm.swust.edu.cn/problem/412/ Time limit(ms): 1000 Memory limit(kb): 65535   Description ...

  5. centos安装epel源

    用163的源,但是我发现这个源里面,根本没有libmcrypt libmcrypt-devel这二个包,装php扩展mcrypt时,又要用到这二个包,所以我手动装了libmcrypt包,但是给我的感觉 ...

  6. mac下 配置 Apache Php Mysql

    参考 http://www.guomii.com/posts/30136 参考 http://forums.mysql.com/read.php?11,600754,600754 MacOS 10.8 ...

  7. Python3 官方文档翻译 - 4.7 函数定义

    4.7.1 默认函数定义 最常用的就是为一个或多个参数设定默认值,这让函数可以用比定义时更少的参数来调用,例如: def ask_ok(prompt, retries=4, complaint='Ye ...

  8. Java学习之IO之File类一

    File的操作 package com.gh.file; import java.io.File; import java.io.IOException; /** * File操作 * @author ...

  9. 2013 成都网络赛 1004 Minimum palindrome

    题目大意:用m个字母组成一个长度为N的字符串,使得最长的回文子串 的长度最小. 并且要求字典序最小. 思路:分类模拟. 当M为1 的时候就直接输出N个A 当M大于2的时候就循环ABC 当M等于2的时候 ...

  10. [Swust OJ 648]--简单字典(数位dp)

    题目链接:http://acm.swust.edu.cn/problem/0648/ Time limit(ms): 1000 Memory limit(kb): 65535   有这样一本字典,它每 ...