任务:

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. poj 2367 拓扑排序入门

    Description The system of Martians' blood relations is confusing enough. Actually, Martians bud when ...

  2. 汕头市队赛 SRM1X T2 ——扫描线

    绵津见-终 SRM 13 背景 “西瓜也是可以种在海上的!”——绵津见 然而种在海上的西瓜最需要防范的,是时不时会涌向瓜田的阵阵海浪. 幸好,身为海神的绵津见可以释放魔法“水平如镜”来阻止海浪拍打西瓜 ...

  3. 限制MYSQL从服务器为只读状态

    修改全局变量的方法有两种,第一种是修改配置文件,第二种是SQL语句设置全局变量的值.(可以参考:http://www.cnblogs.com/qlqwjy/p/8046592.html) 0.简介: ...

  4. lambda calculus

    ;;;lambda calculus ;;;booleanstrue = \x.\y.xfalse = \x.\y.yif = \v.\t.\f. v t f ;;;exif true M N = M ...

  5. CyanogenMod编译

    1. 介绍 本文介绍了i9100手机CyanogenMod 13系统的编译方法 2. 系统要求 笔者使用的环境为CentOS-7-x86_64, 用来为i9100编译CM 13,之所以选择最新版的CM ...

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

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

  7. Vue.js 2使用中的难点举例--子组件,slot, 动态组件,事件监听

    一例打尽..:) <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...

  8. SqlServer 2014安装指引

    具体步骤看整理的Word文档 链接:https://pan.baidu.com/s/1zOhaFVpro2DNnJlJ6dbSEg 密码:lj4m 具体步这里不介绍了,这里记录下报错信息 这个是说系统 ...

  9. Xamarin XAML语言教程Xamarin.Forms中构建进度条

    Xamarin XAML语言教程Xamarin.Forms中构建进度条 ProgressBar被称为进度条,它类似于没有滑块的滑块控件.进度条总是水平放置的.本节将讲解如何使用进度条. 注意:进度条在 ...

  10. luogu P1195 口袋的天空

    题目背景 小杉坐在教室里,透过口袋一样的窗户看口袋一样的天空. 有很多云飘在那里,看起来很漂亮,小杉想摘下那样美的几朵云,做成棉花糖. 题目描述 给你云朵的个数N,再给你M个关系,表示哪些云朵可以连在 ...