WEB开发网
开发学院WEB开发Jsp 用JSP下载word文件(不会直接用IE打开) 阅读

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

 2003-01-28 16:28:17 来源:WEB开发网   
核心提示:<%@page import="java.util.*"%><%@page import="java.io.*"%><%@page import="java.net.*"%><%String filename = &q

<%@page import="java.util.*"%>
<%@page import="java.io.*"%>
<%@page import="java.net.*"%>
<%
  String filename = "";
  if (request.getParameter("file") != null) {
    filename =   request.getParameter("file");
  }
  response.setContentType("application/msWord");
  response.setHeader("Content-disposition","attachment; filename="+filename);
  
  BufferedInputStream bis = null;
  BufferedOutputStream bos = null;
  try {
    bis = new BufferedInputStream(new FileInputStream(getServletContext().getRealPath("" + filename)));
    bos = new BufferedOutputStream(response.getOutputStream());

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

    while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
      bos.write(buff,0,bytesRead);
    }

  } catch(final IOException e) {
    System.out.PRintln ( "出现IOException." + e );
  } finally {
    if (bis != null)
      bis.close();
    if (bos != null)
      bos.close();
  }
  return;
%>

Tags:JSP 下载 word

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接