迷途書童

          敏感、勤學、多思
          隨筆 - 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應用

          主站蜘蛛池模板: 晋江市| 台北市| 德江县| 巴东县| 中牟县| 城步| 年辖:市辖区| 通榆县| 桃江县| 会同县| 息烽县| 图木舒克市| 恩平市| 溆浦县| 惠东县| 商丘市| 安平县| 十堰市| 荔浦县| 周宁县| 宣恩县| 景洪市| 吴忠市| 绵竹市| 安阳市| 曲周县| 楚雄市| 嫩江县| 肥乡县| 汉寿县| 通海县| 阳西县| 灵石县| 三原县| 鹿邑县| 黑水县| 清远市| 大英县| 承德市| 青阳县| 雷州市|