任务:

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. How to install VIB on VMware ESXi

    What is VIB? A vib is  vSphere Installation Bundle. To make it more detailed, one can look at the vS ...

  2. Linux下Nginx使用

    1. 安装 CentOS 7上Nginx的安装和启动方法如下 # yum install nginx # firewall-cmd --permanent --zone=public --add-se ...

  3. Android屏幕元素层次结构

    转自:http://blog.csdn.net/hpoi/article/details/4629717   Android屏幕元素层次结构 android.app.Activity 对于一个Andr ...

  4. 将打开的网页以html格式下载到本地

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. [BZOJ1854][SCOI2010]游戏 二分图最大匹

    1854: [Scoi2010]游戏 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 5316  Solved: 2128[Submit][Status] ...

  6. hihocoder Arithmetic Expression【在线查询】

    Arithmetic Expression   时间限制:2000ms 单点时限:200ms 内存限制:256MB 描述 Given N arithmetic expressions, can you ...

  7. HDU 2016.11.12 做题感想

    细数一下这两天做过的值得总结的一些题Orz...... HDU 2571 简单dp,但是一开始WA了一发.原因很简单:没有考虑仔细. 如果指向该点的所有点权值都为负数,那就错了(我一开始默认初始值为0 ...

  8. Frequency

    题目描述 Snuke loves constructing integer sequences. There are N piles of stones, numbered 1 through N. ...

  9. DML数据操纵语言

    --创建表T_HQ_BM2 --create table t_hq_bm2 as select * from t_hq_bm; commit; --添加行内容 --insert into t_hq_b ...

  10. [NOIP模拟赛][并没有用二分][乱搞AC]

    圆圈舞蹈 [问题描述] 熊大妈的奶牛在时针的带领下,围成了一个圆圈跳舞.由于没有严格的教育,奶牛们之间的间隔不一致. 奶牛想知道两只最远的奶牛到底隔了多远.奶牛A到B的距离为A顺时针走和逆时针走,到达 ...