Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1101109
  • 博文数量: 60
  • 博客积分: 10010
  • 博客等级: 上将
  • 技术积分: 2500
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-19 19:47
文章分类

全部博文(60)

文章存档

2009年(6)

2008年(54)

我的朋友

分类: Java

2009-01-06 21:01:27

setContentType设置MIME类型,Acrobat PDF文件为"application/pdf",WORD文件为:"application/msword",EXCEL文件为:"application/vnd.ms-excel"。
setHeader设置打开方式,具体为:inline为在浏览器中打开,attachment单独打开。

详细参考:
如何用 servlet 打开非 HTML 格式的文档http://www-900.ibm.com/developer ... /tip094/index.shtml

以打开EXCEL文件为例,具体代码如下:
代码:
复制内容到剪贴板
代码:

String sFileName = "test.xls";
resp.setStatus(200);
resp.setContentType("application/vnd.ms-excel");
resp.setHeader("Content-disposition", "inline;filename=\"" + sFileName + "\";");
ServletOutputStream sos = resp.getOutputStream();
sFileName = "Files/" + sFileName;
log("sFileName = " + sFileName);
FileInputStream fis = new FileInputStream(sFileName);
BufferedOutputStream bos = new BufferedOutputStream(sos);
byte[] bytes = new byte[8192];
for (int i=fis.read(bytes); i>0; i=fis.read(bytes))
{
  bos.write(bytes, 0, i);
}
fis.close();
sos.close();
bos.close();

阅读(3518) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~