亲宝软件园·资讯

展开

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

人气:0
想了解用JSP下载word文件(不会直接用IE打开)的相关内容吗,在本文为您仔细讲解的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:|用J|JS|SP|P下|下载|载w|wo|or|rd|d文|文件|件(|(不|不会|会直|直接|接用|用I|IE|E打|打开|开)|)|,下面大家一起来学习吧。

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

加载全部内容

相关教程
猜你喜欢
用户评论