加入收藏 | 设为首页 | 网站地图 | RSS订阅
阅读内容

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

日期:2009-7-3     来源:不详     作者:5i93.com收集

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

上一篇:JSP    下一篇:javascript中的对象、函数和继承

发表评论】【告诉好友】【打印此文】【收藏此文】【关闭窗口

相关新闻
    本文评论
    推荐广告
    推荐文章
    关于本站 | 版权声明 | 免责声明 | 联系我们 | 广告服务 | 加入收藏 | 网站地图 | 网站搜索 | RSS订阅