急急急!我用servlet來打開一個excel文件,但為何只讓我下載呀,請教啦各位,源碼如下:

bellfeel 2001-12-14 02:08:00
我想用servlet來在ie里打開excel文件,但我一調用此servlet時,它就讓我下載此文件,何解呀,

public void performTask(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
throws ServletException, IOException

{
response.setContentType( "application/vnd.ms-excel" ); // MIME type
String fileURL = "http://david/gmims/chinese/test/book1.xls";
response.setHeader("Content-disposition",
"attachment; filename=book1.xls" );
BufferedInputStream bis = null;
BufferedOutputStream bos = null;

try {
ServletOutputStream out = response.getOutputStream ();

URL url = new URL( "http", "david", 80, fileURL );

// Use Buffered Stream for reading/writing.
bis = new BufferedInputStream(url.openStream());
bos = new BufferedOutputStream(out);

byte[] buff = new byte[2048];
int bytesRead;

// Simple read/write loop.
while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
System.out.println("test excel");

} catch(Exception e) {
System.out.println ( "excel error."+e.toString() );

} finally {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
}
}

...全文
64 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

81,116

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧