当前位置: 首页 > 图文教程 > 网络编程 > JSP > JSP教程:点击链接后下载文件(右键另存)功能

JSP
一、Servlet和JSP概述
二、设置开发、运行环境
三、第一个Servlet
JSP简明教程
十一、JSP及语法概要
jsp源码实例1(输出)
邮件发送简单例子-jsp文件
邮件发送简单例子-html文件
邮件发送简单例子-bean文件
jsp源码实例5(cookie)
jsp源码实例4(搜索引擎)
jsp源码实例2(获取表单参数)
jsp计数器代码
IBM WebSphere源代码暴露漏洞
Sun认为C#不会替代Java
jsp源码实例3(获取jsp各种参数)
IBM HTTP Server 远程溢出漏洞
jsp计数器-jsp文件
JAVA/JSP学习系列之一(JDK安装)
JAVA/JSP学习系列之二(Tomcat安装)

JSP教程:点击链接后下载文件(右键另存)功能


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-28   浏览: 118 ::
收藏到网摘: n/a

/** *//** * 实现文件另存功能 * * @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>