迷途書童

          敏感、勤學、多思
          隨筆 - 77, 文章 - 4, 評論 - 86, 引用 - 0
          數據加載中……

          JSP禁用迅雷等下載工具下載文件,強制使用右鍵另存功能下載文件

          * 實現文件另存功能

          * @param text
          * 文件內容
          * @param fileName
          * 文件名稱
          * @return
          */
          protected String renderFile(String text, String fileName)
          throws IOException
          {
          response.addHeader("Content-Disposition", "attachment; filename="
          + fileName);
          response.setContentType("application/octet-stream");
          response.setCharacterEncoding("GB2312");
          response.getWriter().write(text);
          response.flushBuffer();
          response.getWriter().close();
          return null;
          }

          下載的action:

          /** *//**
          * 提供下載的方法
          * @return
          */
          public String down()
          {
          String dir = getFullPath() + "/upload/file/";
          try
          {
          if (!FileUtils.exists(dir))
          {
          new File(dir).mkdirs();
          }
          Random r = new Random(System.currentTimeMillis());
          Integer randomInt = r.nextInt();
          this.renderFile("test content:" + randomInt,randomInt + ".txt");
          }
          catch (IOException e)
          {
          e.printStackTrace();
          this.renderText(e.getMessage());
          }
          return null;
          }

          頁面鏈接調用:

          <a href="${ctx}/va/va!down.do" >下載</a>

          posted on 2011-03-26 23:22 迷途書童 閱讀(1237) 評論(0)  編輯  收藏 所屬分類: java應用

          主站蜘蛛池模板: 镇雄县| 吴忠市| 荔浦县| 安陆市| 合江县| 祁门县| 阿鲁科尔沁旗| 涪陵区| 永和县| 安西县| 桃江县| 名山县| 晋州市| 东丰县| 泰州市| 平利县| 镇雄县| 黄龙县| 三原县| 迭部县| 毕节市| 龙门县| 防城港市| 永胜县| 沛县| 闸北区| 新平| 信宜市| 修水县| 五原县| 大足县| 盖州市| 莫力| 醴陵市| 繁峙县| 邛崃市| 富宁县| 永济市| 江山市| 邳州市| 通许县|