错误:

严重: 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. linux命令: patch

    一. 针对单文件的patch: 我们以mkprj.sh.1和mkprj.sh两个文件为例: [root@localhost tst]# lsmkprj.sh.1  mkprj.sh 看两个文件的差异: ...

  2. Oracle数据库的启动和关闭实例

    在开始了解oracle数据库的命令之前,先来看一个东西:SQL*PLUS(sqlplus) Oracle的sql*plus是与oracle进行交互的客户端工具.在sql*plus中,可以运行sql*p ...

  3. GNU自动补全模块readline解析

    readline模块定义了一系列函数用来读写Python解释器中历史命令,并提供自动补全命令功能.这个模块可以通过relcompleter模块直接调用,模块中的设置会影响解释器中的交互提示,以及内置函 ...

  4. vs 2010 网站项目和asp.net 应用程序引用webservice

    项目需要做对接,对方提供的是一个webservice接口,自己建了个网站项目,然后添加web 引用,一切正常. 当实际编码的时候,发现一些类.方法找不到. 百思不得其解,只得对照对方提供的demo 看 ...

  5. 使用phpQuery实现批量文件处理

    能够将置顶文夹下的指定类型文件进行处理 <?php header('Content-Type:text/html;Charset=utf-8'); include './phpQuery/php ...

  6. java中printf中用法详解

    目前printf支持以下格式: %c 单个字符 %d 十进制整数 %f 十进制浮点数 %o 八进制数 %s 字符串 %u 无符号十进制数 %x 十六进制数 %% 输出百分号% printf的格式控制的 ...

  7. python3.4.3如何获取文件的路径

    #coding:utf-8from tkinter import *from tkinter import filedialogroot = Tk()root.filename = filedialo ...

  8. STL string 模拟

    下面的代码来自c++ primer plus第5版第12章,书中代码写的非常好: // string1.h -- fixed and augmented string class definition ...

  9. 费用流&网络流模版

    费用流模版: #include<cstdio> #include<cstring> #include<queue> using namespace std; ;// ...

  10. Uva 552 Prime Ring Problem(dfs)

    题目链接:Uva 552 思路分析:时间限制为3s,数据较小,使用深度搜索查找所有的解. 代码如下: #include <iostream> #include <string.h&g ...