String file_path="文件路径"
File f;
if (file_path != "")
 {
         f = new File(file_path);
if (!f.exists()) {
return (mapping.findForward("nofile"));
  }
BufferedInputStream br = new BufferedInputStream(
new FileInputStream(f));
byte[] buf = new byte[1024];
int len = 0;
response.reset();
response.setContentType("application/x-msdownload;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment; filename=文件名称.exe"); OutputStream out = response.getOutputStream();
while ((len = br.read(buf)) > 0)
out.write(buf, 0, len);
br.close();
out.close();

解决方案 »

  1.   

    最简单是, 把文件生成在服务器端, 然后给个URL连接给客户, 让客户去下载.
      

  2.   

    不好,那么样的无法下载 Doc ,xls ,txt等文件,会直接打开的
      

  3.   

    response.setContentType("application/x-msdownload;charset=UTF-8");
    这里把头改称一个不认识的
    比如
    response.setContentType("application/download;charset=UTF-8");
    Doc ,xls ,txt等文件,就不会直接打开的
      

  4.   

    看看这个,一个html文件,内嵌了一个excel表格:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <TITLE> New Document </TITLE>
    <META NAME="Generator" CONTENT="EditPlus">
    <META NAME="Author" CONTENT="">
    <META NAME="Keywords" CONTENT="">
    <META NAME="Description" CONTENT="">
    </HEAD><body style="font-size:10pt;font-family:arial;" background="../resources/images/mis_images/titleDH.gif">
            &micro;&frac14;&sup3;&ouml;&sup3;&Eacute;excel&cedil;&ntilde;&Ecirc;&frac12;&pound;&ordm;
            <p>
               <OBJECT classid="clsid:0002E510-0000-0000-C000-000000000046" height=180 id=Spreadsheet1
    style="LEFT: 0px; TOP: 0px" width="100%" VIEWASTEXT>
                <!--PARAM NAME="HTMLURL" VALUE="ini.htm"-->
                <PARAM NAME="DataType" VALUE="HTMLURL">
                <PARAM NAME="AutoFit" VALUE="0">
                <PARAM NAME="DisplayColHeaders" VALUE="-1">
                <PARAM NAME="DisplayGridlines" VALUE="-1">
                <PARAM NAME="DisplayHorizontalScrollBar" VALUE="-1">
                <PARAM NAME="DisplayRowHeaders" VALUE="-1">
                <PARAM NAME="DisplayTitleBar" VALUE="0">
                <PARAM NAME="DisplayToolbar" VALUE="-1">
                <PARAM NAME="DisplayVerticalScrollBar" VALUE="-1">
                <PARAM NAME="EnableAutoCalculate" VALUE="-1">
                <PARAM NAME="EnableEvents" VALUE="-1">
                <PARAM NAME="MoveAfterReturn" VALUE="-1">
                <PARAM NAME="MoveAfterReturnDirection" VALUE="0">
                <PARAM NAME="RightToLeft" VALUE="0">
                <PARAM NAME="ViewableRange" VALUE="1:65536">
              </OBJECT>
            </p>
        </body>
    </HTML>