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

JSP
使用Eclipse开发Jsp
Freebsd+Resin成功建立支持jsp平台
入门教程:JSP标准模板库(上)
入门教程:JSP标准模板库(下)
Struts学习傻瓜式入门篇
新手入门经典:Jsp环境配置
Apache服务器之JSP概述篇
配置Eclpise+tomcat并实现JSP的编写与部署
zip版本Tomcat配置新手入门
Windows2000下Apache2.0.46与Tomcat5.0.2整合配置方法
利用JSP 2.0开发Web应用程序
初学Java所需要注意的几点
整合Tomcat5和IIS5 及正常打开jsp
WIN98/2000下的jsp服务器
配置整合Win+Apache+PHP+MySQL+Tcomcat(或Resin)完全手册
Windows2000下整合Mysql4.0.13与Tomcat4.1.24搭建Jsp环境
JSP实践要点
Servlet/JSP配置详解
使用lomboz调试JSP
Win2000安装Apache+ApacheJserv+gnujsp之完全攻略

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-28   浏览: 82 ::
收藏到网摘: 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>