隨筆 - 1, 文章 - 14, 評論 - 3, 引用 - 0
          數據加載中……

          jsp頁面中的下載功能實現

          <%

          String filename="test.jpg";
          String dirName=application.getRealPath("/WEB-INF/upload");
          java.io.File ff=null;
          String dd=dirName+System.getProperties().getProperty("file.separator")+filename;
          try{
          ff=new java.io.File(dd);
          }
          catch(Exception e){
          e.printStackTrace();
          }
          if (ff!=null&&ff.exists()&&ff.isFile())
          {
          long filelength = ff.length();
          InputStream inStream=new FileInputStream(dd);
          //設置輸出的格式
          response.reset();
          response.setContentType("application/x-msdownload");
          response.setContentLength((int)filelength);
          response.addHeader("Content-Disposition","attachment; filename=\"" + toUtf8String(filename) + "\"");
          //循環取出流中的數據
          byte[] b = new byte[100];
          int len;
          while((len=inStream.read(b)) >0)
          response.getOutputStream().write(b,0,len);
          inStream.close();

          }
          %>

          posted on 2006-04-03 10:02 zeroone0 閱讀(575) 評論(0)  編輯  收藏 所屬分類: 編程

          主站蜘蛛池模板: 清徐县| 清河县| 长春市| 万载县| 兰溪市| 广平县| 时尚| 中方县| 宝兴县| 佛山市| 集安市| 肥东县| 儋州市| 西贡区| 宁化县| 奉新县| 集安市| 安国市| 长垣县| 凤庆县| 浠水县| 奉新县| 长治市| 天峨县| 嵊州市| 景宁| 吐鲁番市| 普定县| 宿迁市| 临夏市| 浦北县| 临猗县| 通许县| 阿合奇县| 辉南县| 郧西县| 柳江县| 万州区| 深州市| 郎溪县| 青岛市|