今天写程序,出现了如下错误:
java.lang.IllegalStateException: getOutputStream() has already been calledfor this response
程序名称为:Demo10.jsp 。程序的源代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.io.*"%>
<html>
<head>
<title></title>
</head>
<body>
<%
response.setHeader("Content-Disposition","attachment;filename=12.jpg");
String path= application.getRealPath("/myfile/12.jpg");
InputStream in = new FileInputStream(path);
OutputStream os = response.getOutputStream();
byte[] buf =new byte[1024];
int len=-1;
while((len=in.read(buf))!=-1){
os.write(buf, 0, len);
}
os.close();
in.close();
%>
</body>
</html>
程序需要是实现jsp下载文件,当12.jpg文件下载成功的同时,程序报出了非法状态异常:java.lang.IllegalStateException: getOutputStream() has already beencalled for this response
后来,我查看一下,发现原因是servlet中字节流和字符流不能同时使用。理由如下:
打开D:\apache-tomcat-6.0.16\work\Catalina\localhost\day1213\org\apache\jsp 找到Demo10.jsp 被转换为 Demo10_jsp.java 文件,其中内容如下:
package org.apache.jsp;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.util.*;
import java.io.*;
public final class Demo10_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent {
private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();
private static java.util.List _jspx_dependants;
private javax.el.ExpressionFactory _el_expressionfactory;
private org.apache.AnnotationProcessor _jsp_annotationprocessor;
public Object getDependants() {
return _jspx_dependants;
}
public void _jspInit() {
_el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
_jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor.class.getName());
}
public void _jspDestroy() {
}
public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws java.io.IOException, ServletException {
PageContext pageContext = null;
HttpSession session = null;
ServletContext application = null;
ServletConfig config = null;
JspWriter out = null;
Object page = this;
JspWriter _jspx_out = null;
PageContext _jspx_page_context = null;
try {
response.setContentType("text/html;charset=UTF-8");
pageContext = _jspxFactory.getPageContext(this, request, response,
null, true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;
out.write("\r\n");
out.write("\r\n");
out.write("\r\n");
out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\r\n");
out.write("<html>\r\n");
out.write(" <head>\r\n");
out.write(" \r\n");
out.write(" <title></title>\r\n");
out.write(" \r\n");
out.write("\t<meta http-equiv=\"pragma\" content=\"no-cache\">\r\n");
out.write("\t<meta http-equiv=\"cache-control\" content=\"no-cache\">\r\n");
out.write("\t<meta http-equiv=\"expires\" content=\"0\"> \r\n");
out.write("\t\r\n");
out.write(" \r\n");
out.write("\r\n");
out.write(" </head>\r\n");
out.write(" \r\n");
out.write(" <body><br>\r\n");
out.write(" ");
response.setHeader("Content-Disposition","attachment;filename=12.jpg");
String path= application.getRealPath("/myfile/12.jpg");
InputStream in = new FileInputStream(path);
OutputStream os = response.getOutputStream();
byte[] buf =new byte[1024];
int len=-1;
while((len=in.read(buf))!=-1){
os.write(buf, 0, len);
}
os.close();
in.close();
out.write("\r\n");
out.write(" </body>\r\n");
out.write("</html>\r\n");
} catch (Throwable t) {
if (!(t instanceof SkipPageException)){
out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
try { out.clearBuffer(); } catch (java.io.IOException e) {}
if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
}
} finally {
_jspxFactory.releasePageContext(_jspx_page_context);
}
}
}
从源代码中可以看出:
黄色阴影部分,调用字符流JspWriter对象实例out输出;而红色阴影部分,调用了字节流OutputStream InputStream。用于servlet中字节流和字符流不能同时使用,所以,程序抛异常了。
解决办法,去掉jsp中所有用out输出的内容,包括空格和回车。修好后的Demo10.jsp源代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.io.*"%><%
response.setHeader("Content-Disposition","attachment;filename=12.jpg");
String path= application.getRealPath("/myfile/12.jpg");
InputStream in = new FileInputStream(path);
OutputStream os = response.getOutputStream();
byte[] buf =new byte[1024];
int len=-1;
while((len=in.read(buf))!=-1){
os.write(buf, 0, len);
}
os.close();
in.close();
%>