迷途书童

敏感、勤学、多思
随笔 - 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 迷途书童 阅读(1220) 评论(0)  编辑  收藏 所属分类: java应用


只有注册用户登录后才能发表评论。


网站导航: