欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-16 16:15:58
<%@page import="java.util.*"%><%@page import="java.io.*"%><%@page impor...

<%@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;
%>

上一篇:

下一篇: