java server throws java.lang.IllegalStateException:在提交响应后无法调用sendError()

时间:2014-02-25 08:18:39

标签: illegalstateexception

请帮帮我 java server throws:java.lang.IllegalStateException:在提交响应后无法调用sendError() 我多次重新检查并用Google搜索,但我无法找到问题所在 这是我的代码

    DataOutputStream stream = null;
BufferedInputStream buf = null;
try {
response = ServletActionContext.getResponse();
response.setHeader(Constants.AU_TRST_X_RESULT_CD, "0"); // x-resultCd = 0 is OK
// add RSP_KEY_CODE to header
response.setHeader(Constants.RSP_KEY_CODE, Constants.RSP_SUCCCESS + "");
response.setContentType("application/csv");
//------------ write csv file to body ----------
// get response's outputStream
stream = new DataOutputStream(response.getOutputStream());
// fix file csv
File csvFixFile = new File("E:\\a.xls");   
// buffer to read csv File

File csvResponse = new File(csvFixFile.getPath());
// file Input to read csv File
FileInputStream inputStream = new FileInputStream(csvResponse);
buf = new BufferedInputStream(inputStream);
int readBytes = 0;
while ((readBytes = buf.read()) != -1) {
stream.write(readBytes);
}

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (null != buf)
try {buf.close();} catch (Exception e2) {e2.printStackTrace();}
if (null != stream)
try {stream.close();} catch (Exception e2) {e2.printStackTrace();}  
}

。它看起来没问题,但它没有正常工作,也许问题出在while循环中 请指出给我.....

1 个答案:

答案 0 :(得分:0)

您可以使用apache commons-io中的IOUtils将输入流复制到输出流中(它也使用缓冲区,因此您不需要):

ServletOutputStream outStream = response.getOutputStream();
IOUtils.copy(inputStream, outStream);

IOUtils javadoc