jialisoftw

          如何讓頁面一打開就遠程自動下載文件

          在頁面或者后臺response,直接用文件頭加你的文件,文件流寫出。
          比如在頁面這樣寫,當然,最好在后臺寫:
          <%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
          <%
          String path = request.getContextPath();
          String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
          %>
          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
          <html>
            <head>
              <base href="<%=basePath%>">
              
              <title>My JSP 'test.jsp' starting page</title>
              
           <meta http-equiv="pragma" content="no-cache">
           <meta http-equiv="cache-control" content="no-cache">
           <meta http-equiv="expires" content="0">    
           <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
           <meta http-equiv="description" content="This is my page">
           <!--
           <link rel="stylesheet" type="text/css" href="styles.css">
           -->
            </head>
            
            <body>
              <% 
          // 得到文件名字和路徑 
          String filename = "Example.zip"; 
          String filepath = "D:\\";
          // 設置響應頭和下載保存的文件名 
          response.setContentType("APPLICATION/OCTET-STREAM"); 
          response.setHeader("Content-Disposition", 
          "attachment; filename=\"" + filename + "\"");
          // 打開指定文件的流信息 
          java.io.FileInputStream fileInputStream = 
          new java.io.FileInputStream(filepath + filename);
          // 寫出流信息 
          int i; 
          while ((i=fileInputStream.read()) != -1) { 
          out.write(i); 
          fileInputStream.close(); 
          out.close();
          %>
            </body>
          </html>

          posted on 2013-01-16 12:40 飛豬一號 閱讀(1230) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          <2013年1月>
          303112345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          統計

          常用鏈接

          留言簿

          隨筆檔案

          友情鏈接

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 阜平县| 彩票| 甘谷县| 常熟市| 垣曲县| 克东县| 华亭县| 泰兴市| 即墨市| 项城市| 乐平市| 益阳市| 织金县| 周至县| 城固县| 金门县| 公主岭市| 逊克县| 沂源县| 和林格尔县| 永定县| 讷河市| 绥德县| 武穴市| 黄山市| 聂荣县| 安丘市| 西昌市| 如东县| 蒙山县| 永清县| 定南县| 昆明市| 景谷| 抚州市| 洪湖市| 阳江市| 宜君县| 香格里拉县| 商都县| 慈溪市|