首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Exception >

java.lang.IllegalStateException: getWriter() has already been called for this re

2014-01-26 
代码: import java.io.IOException import java.io.InputStream import java.io.PrintWriter import jav

代码:
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ShowPicture extends HttpServlet {

protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.doPost(req, resp);
}

protected void doPost(HttpServletRequest req, HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding("utf-8"); 
response.setContentType("text/html; charset=utf-8");
PrintWriter out = response.getWriter();
try {
ConnectBean c = new ConnectBean();
Connection conn = c.getConnect();
Statement stmt = conn.createStatement();
String sql = "select * from t_book";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
out.println("编号:  " + rs.getString(1) + " <br/>");
out.println("书名:  " + rs.getString(2) + " <br/>");
out.println("作者:  " + rs.getString(3) + " <br/>");
out.println("出版社:  " + rs.getString(4) + " <br/>");
out.println("价钱:  " + rs.getString(5) + " <br/>");
out.println("折扣:  " + rs.getString(6) + " <br/>");
out.println("出版日期:" + rs.getString(7) + " <br/>");
out.println("页数:  " + rs.getString(8) + " <br/>");
out.println("开本:  " + rs.getString(9) + " <br/>");
out.println("内容简介:" + rs.getString(10) + " <br/>");
out.println("添加时间:" + rs.getString(11) + " <br/>");
out.println("类型:  " + rs.getString(12) + " <br/>");
out.println("书本图片: ");
InputStream in = rs.getBinaryStream(13);
//response.reset();
ServletOutputStream sos = response.getOutputStream();
if(in != null) {
byte[] b = new byte[1024*1024];

int len=in.read(b);
while (len!=-1){
sos.write(b, 0, len);
System.out.println(len);
len=in.read(b);
}
sos.flush();
sos.close();
in.close();
}
out.println(" <br/> <br/>");
}
out.close();
rs.close();
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}

public void destroy() {
super.destroy();
}

public void init() throws ServletException {
super.init();
}

}

注: response.reset();使用这句话可以显示后面的图片,但前面的内容不显示!注释起来就显示了前面的内容,后台报错
java.lang.IllegalStateException: getWriter() has already been called for this response网上搜了一下说是response.getOutputStream(),response.getWriter();只可以用其中一个!页面上可以用这个,out.clear();
pageContext.pushBody();

怎样既可以输出上面的所有内容?

------解决方法--------------------------------------------------------

那就统一用  response.getOutputStream();
来输出内容吧,将前面的内容转为byte输出吧
------解决方法--------------------------------------------------------
我觉得应该用JFrameChar去实现。它是一个java中画图的工具。很好很方便。去网站上自己搜下吧。
------解决方法--------------------------------------------------------
好像以前碰过这个问题,你在dopost的最後加上return null;试一下
------解决方法--------------------------------------------------------
输出图片一般的都是在jsp页面中引入img标签,src="一个连接生成图片"

        

热点排行