2014-02-25 145 views
0

請幫助我 java服務器拋出:java.lang.IllegalStateException:在提交響應後無法調用sendError() 複查多次和google搜索了很多,但我不能發現什麼問題 這裏是我的代碼java服務器拋出java.lang.IllegalStateException:在響應提交後無法調用sendError()

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循環 請指出來給我。 ...

回答

0

你可以使用IOUtils從Apache的百科全書-io的輸入流複製到輸出流(也使用緩衝區,所以你並不需要):

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

IOUtils javadoc

+0

太感謝你了,我會這麼做:-) –

相關問題