java.lang.IllegalStateException: getOutputStream() has already been called for this response每次都报这样的异常,这是我的源代码。请高手指教!
<%@ page contentType="text/html; charset=GBK" import="java.util.ArrayList"%><%@ page import="java.io.*" %>
<%@ page import="javax.servlet.ServletOutputStream" %>
<html>
<head>
</head>
<body>
<%request.setCharacterEncoding("GBK");
response.setCharacterEncoding("GBK");
String excelpath = (String)request.getAttribute("path");
String Path = excelpath.substring(3,excelpath.length());
if(excelpath!=null && !excelpath.equals(""))
{
ServletOutputStream fo = null;
BufferedOutputStream bos = null;
BufferedInputStream bis = null;
File temp = new File(excelpath);
try
{
response.setContentType("application/vnd.ms-execl");
response.setHeader("Content-Disposition","attachment;filename="+Path);
bis = new BufferedInputStream(new FileInputStream(temp));
fo = response.getOutputStream();
bos = new BufferedOutputStream(fo);
byte[] buff = new byte[2048];
int bytesRead;
while(-1 != (bytesRead = bis.read(buff, 0, buff.length)))
{
bos.write(buff,0,bytesRead);
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
if(bos!=null)
bos.close();
if(bis!=null){
bis.close();
}
fo.close();
temp.delete();
}%><% }
else
{
out.println("11111111");
}%>
</body>
</html>
<%@ page contentType="text/html; charset=GBK" import="java.util.ArrayList"%><%@ page import="java.io.*" %>
<%@ page import="javax.servlet.ServletOutputStream" %>
<html>
<head>
</head>
<body>
<%request.setCharacterEncoding("GBK");
response.setCharacterEncoding("GBK");
String excelpath = (String)request.getAttribute("path");
String Path = excelpath.substring(3,excelpath.length());
if(excelpath!=null && !excelpath.equals(""))
{
ServletOutputStream fo = null;
BufferedOutputStream bos = null;
BufferedInputStream bis = null;
File temp = new File(excelpath);
try
{
response.setContentType("application/vnd.ms-execl");
response.setHeader("Content-Disposition","attachment;filename="+Path);
bis = new BufferedInputStream(new FileInputStream(temp));
fo = response.getOutputStream();
bos = new BufferedOutputStream(fo);
byte[] buff = new byte[2048];
int bytesRead;
while(-1 != (bytesRead = bis.read(buff, 0, buff.length)))
{
bos.write(buff,0,bytesRead);
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
if(bos!=null)
bos.close();
if(bis!=null){
bis.close();
}
fo.close();
temp.delete();
}%><% }
else
{
out.println("11111111");
}%>
</body>
</html>
改成这样试试