3

我有以下代码来读取 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);
        }
    }
}

我能做些什么来解决这个错误?

4

1 回答 1

0

哪一行抛出异常?一般来说,代码看起来是正确的。只是几个想法:可能是您要迭代此代码两次(结果集中有两个或更多结果)?此外,请尝试在获取输出流之前设置 contentType。

于 2012-12-28T14:08:15.970 回答