任务:

uos.docfile的content字段是longblob类型,通过Web点击链接能下载到存储在这个字段里的文件。Web点击链接类似如下形式:

http://localhost:8080/dld/downloadDocument.html?id=81&&filename=jfreechart-1.0.19.zip

1.控制器代码:

    @RequestMapping("/downloadDocument")
    public ModelAndView downloadDocument(HttpServletRequest request,HttpServletResponse response){
        try {
            // 取参数
            String id=request.getParameter("id");
            String filename=request.getParameter("filename");

            // 设置Resposne
            response.reset();
            response.setHeader("Content-disposition", "attachment; filename="+filename);
            response.setContentType("text/x-plain");

            // 获得输出流
            ServletOutputStream out = response.getOutputStream();

            // 从数据库拷贝输出流
            ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(4096);
            service.copyDocumentOutputStream(id, byteOutputStream);
            logger.info(" call PosService.copyDocumentOutputStream successfully.");

            // 转化
            byte[] bt = null;
            bt = byteOutputStream.toByteArray();               

            // 向客户端写输出
            out.write(bt);
            out.flush();
            out.close();

            return null;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e);

            request.setAttribute("error", e.getClass());
            request.setAttribute("reason", e.getMessage());
            StackTraceElement[] arr=e.getStackTrace();
            request.setAttribute("stackTraceElements", arr);

            return new ModelAndView("pages/error/index.jsp");
        }
    }

2.Service中代码,同样这里也只是中转

public void copyDocumentOutputStream(final String id, final OutputStream os) throws Exception{
        getPosDao().copyDocumentOutputStream(id, os);
    }

3.DAO中代码,这里是实质代码

    public void copyDocumentOutputStream(final String id, final OutputStream os) throws Exception{
        final LobHandler lobHandler=new DefaultLobHandler();

        this.getJdbcTemplate().query("select content from uos.docfile where id=?",new String[] {id},new AbstractLobStreamingResultSetExtractor(){
            protected void streamData(ResultSet rs) throws SQLException,IOException,DataAccessException{
                FileCopyUtils.copy(lobHandler.getBlobAsBinaryStream(rs,1),os);
            }
        });
    }

SpringMVC处理MySQL BLOB字段的下载的更多相关文章

  1. SpringMVC处理MYSQL BLOB字段的上传

    任务: uos.docfile的content字段是longblob类型的,通过页面将文件存储到这个字段里. 页面代码: <div class="box"> <d ...

  2. C#读取Mysql blob字段 (转帖)

    http://blog.csdn.net/config_man/article/details/6123191 开发环境:Windows XP Professional SP3.VS2008.Winf ...

  3. MYSQL BLOB 字段大小以及个数的限制測试。

    測试结论 mysql版本号 5.1     表类型: innodb, row_format=compact (这是默认的行格式)     插入超过10个blob, blob的数据量非常小(<76 ...

  4. mysql BLOB字段转String的方法

    1.通过sql直接转换 select CONVERT(GROUP_CONCAT(XXX) USING utf8 from usertable; 2.通过程序转换(注:本例用的是springmvc包装并 ...

  5. MySQL中TEXT与BLOB字段类型的区别

    这篇文章主要介绍了MySQL中TEXT与BLOB字段类型的区别,本文总结了6大区别,需要的朋友可以参考下   在MySQL中有两个字段类型容易让人感觉混淆,那就是TEXT与BLOB,特别是自己写博客程 ...

  6. Java实现打包下载BLOB字段中的文件

    概述 web项目的文件打包下载实现:servlet接收请求,spring工具类访问数据库及简化大字段内容获取,org.apache.tools.zip打包. 必要提醒:当前总结是继Java实现下载BL ...

  7. Java实现下载BLOB字段中的文件

    概述 web项目的文件下载实现:servlet接收请求,spring工具类访问数据库及简化大字段内容获取. 虽然文章的demo中是以sevlet为平台,想必在spring mvc中也有参考意义. 核心 ...

  8. mybatis查询mysql 数据库中 BLOB字段,结果出现乱码

    起因 mybatis-plus 通过Mapper 查询数据,映射出来的BLOB字段中的yml数据中文是乱码的 --- DefaultValue: '' Formula: '' HintContent: ...

  9. 解决springmvc+mybatis+mysql中文乱码问题【转】

    这篇文章主要介绍了解决java中springmvc+mybatis+mysql中文乱码问题的相关资料,需要的朋友可以参考下 近日使用ajax请求springmvc后台查询mysql数据库,页面显示中文 ...

随机推荐

  1. jQuery UI-Draggable 参数集合

    ·概述    在任何DOM元素启用拖动功能.通过单击鼠标并拖动对象在窗口内的任何地方移动.    官方示例地址:http://jqueryui.com/demos/draggable/      所有 ...

  2. tips 前端 背景与元素的透明和模糊

    碰到好几次这样的情况了: 一个带点儿文艺效果 背景图片模糊 而一行别致的文字清晰的悬浮在背景上(口胡,加点美好的想象,生活会更美好) 第一反应是 this is easy. cause i know ...

  3. hdu 4089 概率dp

    /* 题目大意:注册一款游戏需要排队,一共有四种事件: 1.注册失败,队列不变,概率为p1 2.注册过程中断开连接,正在注册的人排到队列的末尾,概率为p2 3.注册成功,移出队列,概率为p3 4.服务 ...

  4. 道路修建(bzoj 2435)

    Description 在 W 星球上有 n 个国家.为了各自国家的经济发展,他们决定在各个国家之间建设双向道路使得国家之间连通.但是每个国家的国王都很吝啬,他们只愿意修建恰好 n – 1条双向道路. ...

  5. 原生dialog

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. IaaS, PaaS和SaaS

    原文链接:http://www.leiphone.com/news/201406/iaas-paas-and-saas.html 云服务”现在已经快成了一个家喻户晓的词了.如果你不知道PaaS, Ia ...

  7. 俄罗斯方块(NOIP模拟赛)(水·模拟)

    真是一道神奇的题目233~ 原题传送门 迫不得已贴了个题解的链接.. 好吧,这道题就是分情况讨论,纯模拟,, 没有什么难的.. 脑洞要大,四面都要考虑,不能漏! #include<iostrea ...

  8. MyBatis报错 Parameter '0' not found. Available parameters are [arg1, arg0, param1, param2]

    修改 <update id="updateStatusById" parameterType="java.lang.Integer"> update ...

  9. PHP中利用PHPMailer使用QQ邮箱实现邮件发送

    /** * 下订单发送邮件 * @to 收件人 @title 标题 @content 内容 */ function sendMail($to,$title,$content){ //引入PHPMail ...

  10. [BZOJ1260][CQOI2007]涂色paint 区间dp

    1260: [CQOI2007]涂色paint Time Limit: 30 Sec  Memory Limit: 64 MB Submit: 1575  Solved: 955 [Submit][S ...