错误:

严重: 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. 上传列表集合wsp包

    1. 网站设置--web设计器库--解决方案--上载解决方案 2. 打开SPD,左侧的子网站--新建

  2. D - D 田忌赛马

    D - D    田忌赛马   解题报告 hdu 1052 Tian Ji -- The Horse Racing 链接:http://acm.hust.edu.cn/vjudge/contest/v ...

  3. VC++非MFC项目中如何使用TRACE宏

    记得原来尝试学MFC的时候觉得有一个TRACE可以在Debug时向VS的调试输出窗口输出字串符,用来调试时跟踪变量很方便. 然则如果不是MFC项目或者ATL的项目的话是不能使用这个宏的.这时有一个没有 ...

  4. MD5加密解密

    方法一 首先,先简单介绍一下MD5 MD5的全称是message-digest algorithm 5(信息-摘要算法,在90年代初由mit laboratory for computer scien ...

  5. window.open 使用方法总结

    [1.最基本的弹出窗口代码] <SCRIPT LANGUAGE="javascript">  <!--  window.open ('test.html')  - ...

  6. C 语言统计关键字出现次数

    #include <stdio.h> #include <ctype.h> #include <string.h> #define NKEYS (sizeof ke ...

  7. UINavigationController具体解释(二)

    @UINavigationBar-----(是一个View)基本介绍 1.导航栏,和导航控制器一样,是一个容器用来显示提供的其它对象的内容 2.导航栏显示的内容,通过设置UINavigationIte ...

  8. ThinkPHP - 自动创建 + 自动验证 + 自动完成

    自动创建:创建数据模型. $User->create(); 自动验证:验证提交的表单数据. protected $_validate = array( array('verify','requi ...

  9. DOM操作HTML文档

    概述 之前写过一些关于DOM方法的知识,理论方法的偏多,所以,这篇博客主要是实践方面的Demo,如果,大家觉得理论方面有所欠缺,大家可以看看这几篇博客:JavaScript总结(一.基本概念)和Jav ...

  10. python几个排序函数 sort sorted argsort

    Python中排序常用到的sort .sorted和argsort函数 [摘要:Python中sort 战 sorted函数 一 .先容 sort函数是list列表中的函数,而 sorted能够对li ...