java.lang.IllegalStateException错误!!怎么解决?
<%@ page language="java" import="java.util.*,java.io.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'servlet1.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
String name=request.getParameter("filename");
String filename=new String(name.getBytes("ISO-8859-1"),"GBK");
//获得服务器上下载文件的物理路径 *注意如传参带中文要改tomcat里的server文件的配置
String filePath=application.getRealPath("/load")+"/"+filename;
//生成文件对象
File file=new File(filePath);
long fileLength=file.length();//获得文件长度
//设定下载文件长度
response.setContentLength((int)fileLength);
//设定文件MIME类型为application/x-msdownload下载类型
response.setContentType("application/x-msdownload");
//设定下载文件名
response.setHeader("Content-Disposition","attachment;filename="+new String (filename.getBytes("GBK"),"ISO-8859-1"));
FileInputStream fis=new FileInputStream(file);
BufferedInputStream bis=new BufferedInputStream(fis);
//使用缓冲写,提高速度
byte[] buff=new byte[2048];
OutputStream os=response.getOutputStream();//获得输出流
int k=0;
while((k=bis.read(buff,0,buff.length))!=-1){
os.write(buff,0,k);
}
os.close();
%>
</body>
</html>
用个JSP页写的下载:
找了一些资料说是,在程序中添加:
out.clear();
out = pageContext.pushBody();
可是这2行代码添加在什么位置呀?