求助:一个简单的文件下载问题
1.jsp点击xxx进入下载页面 2.jsp
2.jsp代码如下:
<%@ page contentType="text/html;charset=gb2312"%> <%@ page import="java.io.*"%> <html> <body > <% OutputStream o=response.getOutputStream(); byte b[]=new byte[500]; File fileload=new File("f:/myjsp","爱情密码.rar"); response.setHeader("Content-disposition","attachment;filename="+"爱情密码.rar"); response.setContentType("application/x-tar"); long fileLength=fileload.length(); String length=String.valueOf(fileLength); response.setHeader("Content_Length",length); FileInputStream in=new FileInputStream(fileload); int n=0; while((n=in.read(b))!=-1) { o.write(b,0,n); }
%> </body> </html> 异常如下:
java.lang.IllegalStateException: getOutputStream() has already been called for this response org.apache.coyote.tomcat5.CoyoteResponse.getWriter(CoyoteResponse.java:611) org.apache.coyote.tomcat5.CoyoteResponseFacade.getWriter(CoyoteResponseFacade.java:190) org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:167) org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:160) org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:235) org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:160) org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:120) org.apache.jsp.load.love_jsp._jspService(love_jsp.java:69) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133) javax.servlet.http.HttpServlet.service(HttpServlet.java:856) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248) javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
请哪位高手帮忙看看啊。。。