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 飛豬一號 閱讀(1682) 評論(0)  編輯  收藏


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


          網站導航:
           

          導航

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

          統計

          常用鏈接

          留言簿

          隨筆檔案

          友情鏈接

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 昌都县| 黄陵县| 寿阳县| 乃东县| 吉木萨尔县| 麻栗坡县| 通榆县| 阿勒泰市| 纳雍县| 禹城市| 德州市| 株洲县| 那坡县| 磐石市| 钟祥市| 广宗县| 且末县| 临潭县| 新河县| 海兴县| 香河县| 丽水市| 静宁县| 西贡区| 宣威市| 长治县| 穆棱市| 扶沟县| 兴海县| 连州市| 海南省| 垦利县| 九寨沟县| 张家界市| 白玉县| 清远市| 顺义区| 辉南县| 禄丰县| 泾阳县| 涡阳县|