用JSP下载word文件(不会直接用IE打开)

1@page import="java.util.*"
1@page import="java.io.*"
1@page import="java.net.*"
 1   
 2String filename = "";   
 3if (request.getParameter("file") != null) {   
 4filename = request.getParameter("file");   
 5}   
 6response.setContentType("application/msword");   
 7response.setHeader("Content-disposition","attachment; filename="+filename);   
 8  
 9BufferedInputStream bis = null;   
10BufferedOutputStream bos = null;   
11try {   
12bis = new BufferedInputStream(new FileInputStream(getServletContext().getRealPath("" + filename)));   
13bos = new BufferedOutputStream(response.getOutputStream());   
14  
15byte[] buff = new byte[2048];   
16int bytesRead;   
17  
18while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {   
19bos.write(buff,0,bytesRead);   
20}   
21  
22} catch(final IOException e) {   
23System.out.println ( "出现IOException." + e );   
24} finally {   
25if (bis != null)   
26bis.close();   
27if (bos != null)   
28bos.close();   
29}   
30return;   
Published At
Categories with Web编程
Tagged with
comments powered by Disqus