我有以下代码来读取blob数据&将结果显示为图像。但我收到错误java.lang.IllegalStateException: getWriter() has already been called for this response
。
while (rs2.next()) {
Blob image = null;
byte[] imgData = null;
j++;
qPaperOptions = rs2.getString(1);
int qDetailId = rs2.getInt(2);
image = rs2.getBlob(5);
ServletOutputStream sout = response.getOutputStream();
// o.close();
// imgData = image.getBytes(1,(int)image.length());
// Blob cnt_data=rs2.getBlob("cimg.ctn_data");
if (image != null) {
// imgData = image.getBytes(1,(int)image.length());
response.setContentType("image/gif");
InputStream in = image.getBinaryStream();
int length = (int) image.length();
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
while ((length = in.read(buffer)) != -1) {
sout.write(buffer, 0, length);
}
}
}
我该怎么做才能解决这个错误?
答案 0 :(得分:0)
哪一行抛出异常?通常,代码看起来是正确的。只是几个想法: 难道你是在迭代这段代码两次(结果集中有两个结果或更多)?另外,尝试在获取输出流之前设置contentType。