咸阳市委:在eclipse中如何编码实现下载功能?

来源:百度文库 编辑:中科新闻网 时间:2024/05/10 07:44:15
需要下载成excel形式,可是我写的代码只生成了个空的excel,怎样才能把数据写进去呢?请高手指点,越快越好,谢谢啦!
我在jsp页中的代码如下:
<%@page language="java" contentType="application/x-msdownload" import="java.io.*,java.net.*"%>

<% response.reset();
response.setContentType("application/x-download");
System.out.println(this.getClass().getClassLoader().getResource("/").getPath());
String filenamedownload = this.getClass().getClassLoader().getResource("/").getPath() + "/../../111.xls";
String filenamedisplay = "111.xls";
filenamedisplay = URLEncoder.encode(filenamedisplay,"UTF-8");
response.addHeader("Content-Disposition","attachment;filename=" + filenamedisplay);

OutputStream output = null;
FileInputStream fis = null;
try
{
output = response.getOutputStream();
fis = new FileInputStream(filenamedownload);

byte[] b = new byte[1024];
int i = 0;

while((i = fis.read(b)) > 0)
{
output.write(b, 0, i);
}
output.flush();
}
catch(Exception e)
{
System.out.println("Error!");
e.printStackTrace();
}
finally
{
if(fis != null)
{
fis.close();
fis = null;
}
if(output != null)
{
output.close();
output = null;
}
}%>

5月25日 09:28 从新再装一次吧,在试试把jdk换成1.5的试试,eclipse有时候很多问题,新手往往不知道怎么回事,莫名其妙的,再装一次,我当时就装了好几次,呵呵,比较麻烦~:)