任务:

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. Data_Structure01-绪论作业!

    一.作业题目 仿照三元组或复数的抽象数据类型写出有理数抽象数据类型的描述 (有理数是其分子.分母均为整数且分母不为零的分数). 有理数基本运算: 构造有理数T,元素e1,e2分别被赋以分子.分母值 销 ...

  2. 重量WeightFormatUtil辅助类

    package com.jlb.scan.util; import java.text.DecimalFormat; public class WeightFormatUtil { public st ...

  3. 解方程(NOIP2014)Warning!(前方高能!!)

    原题传送门 一看这不是水题嘛. 枚举+乱搞..特别容易.... 然后a[i]取值范围出现了 当当当当~:|a[i]|<=10^10000!!!!! 我去,这是什么鬼.. 高精度? 然后默默算了算 ...

  4. How do I list all fields of an object in Objective-C?

    http://stackoverflow.com/questions/1213901/how-do-i-list-all-fields-of-an-object-in-objective-c As m ...

  5. UCRT: VC 2015 Universal CRT, by Microsoft

    https://blogs.msdn.microsoft.com/vcblog/2015/03/03/introducing-the-universal-crt/ App local UCRT DLL ...

  6. C++笔试题目大全(笔试宝典)(不断完善中)

    1.new . delete . malloc . free 关系 delete 会调用对象的析构函数 , 和 new 对应 free 只会释放内存, new 调用构造函数. malloc 与 fre ...

  7. 无类型指针 在delphi中可以直接赋值任何指针类型。

  8. hdu 1217(Floyed)

    Arbitrage Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  9. cl编译C文件的环境变量修改

    添 加环境 变量INCLUDEC:\Program Files (x86)\Windows Kits\10\Include\10.0.10240.0\ucrt;C:\Program Files (x8 ...

  10. Python的功能模块[4] -> pdb/ipdb -> 实现 Python 的单步调试

    pdb / ipdb 模块 / pdb / ipdb Module pdb 和 ipdb 的主要作用是用于 Python 程序的单步调试,Python 的调试可参考链接. 下面是一个简单的使用示例 i ...