博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JdbcTemplate 操作Oracle Blob
阅读量:4582 次
发布时间:2019-06-09

本文共 2332 字,大约阅读时间需要 7 分钟。

写此文章,是因为我之前也并没有接触过,上网找了许多资料,还是有七七八八的问题,最终解决了,做个记录,免得之后忘记。

首先,先取出Blob字段,将其保存在字节数组中,之后用HTTP协议中下载文件的格式对应,主要只要写两个:

(1)response.setContentType("audio/wav"); 

(2)response.setHeader("Content-disposition","attachment;filename=" +URLEncoder.encode("report.wav", "utf-8"));

如果对Http协议不够熟悉,请查阅http://www.cnblogs.com/quanjia/archive/2010/11/01/1866753.html

由于我做的一个播放控件,需要的是文件的路径,所以我还将其写在一个文件中,代码都很详细。

1.DAO层

/**

     * 获取汇报录音
     */
    @Override
    public void showReport(ComInput cip, ComOutput cop) {
        String id=cip.get("id").toString();
        sql.create();
        sql.append("    SELECT T.audio  FROM biaoming T    ");
        sql.append("     WHERE T.ID = ?    ");
        @SuppressWarnings("unchecked")
        List blobBeans= jdbcTemplate.query(sql.toString(),new Object[]{id},
                 new org.springframework.jdbc.core.RowMapper() {
            public Object mapRow(java.sql.ResultSet rs, int rowNum) throws SQLException {
                byte[] iconByte = null;
                java.sql.Blob blob  = rs.getBlob(1);
                if(blob != null){
                    java.io.InputStream inStream  = blob.getBinaryStream();
                    iconByte = new byte[(int)blob.length()];
                    try {
                        inStream.read(iconByte);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }finally{
                        try {
                            inStream.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
             return iconByte;
           }
        }
);

Action层:

    ComOutput com = custReportService.showReport(cip);

            if(com.getRetCode() == LbscConstant.SUCCESS){
                byte[] recByte = (byte[])com.getRetObj();
                if(recByte != null  && recByte.length > 0){
                    response.setContentType("audio/wav");  
                    try {
                        response.setHeader("Content-disposition",
                                "attachment;filename=" +
                                URLEncoder.encode("report.wav", "utf-8"));
                    } catch (UnsupportedEncodingException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }try {
                        ServletOutputStream op = this.response.getOutputStream();
                        //保证文件名唯一,你可以用主键+时间啊等等方法                 
                        File file = new File("E:/JAVAspj/tomcat-7.0.69/webapps/report/test");  
                        if(!file.exists()){
                            file.mkdirs();
                          }
                        // fileName表示你创建的文件名;
                        String fileName= id+".wav";
                        File f = new File(file,fileName);
                        if(!f.exists()){
                            try {
                                f.createNewFile();
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                        FileOutputStream fout = new FileOutputStream(f);
                        op.write(recByte, 0, recByte.length);
                        fout.write(recByte);  
                        op.close();    
                        op = null;   
                        fout.close();
                        response.flushBuffer();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }   
                }
                return null;
            }

 

转载于:https://www.cnblogs.com/lwkblog/p/6197602.html

你可能感兴趣的文章
js 对url进行某个参数的删除,并返回url
查看>>
Windows7装Linux虚拟机
查看>>
SQL 操作结果集 -并集、差集、交集、结果集排序
查看>>
linux上搭建nginx+php+mysql环境详细讲解
查看>>
RemoveDuplicatesFromSortedArrayI II,移除有序数组里的重复元素以及移除数组里的某个元素...
查看>>
Minimum Depth of Binary Tree,求树的最小深度
查看>>
解决Web部署 svg/woff/woff2字体 404错误
查看>>
fiddler 抓取 nodejs
查看>>
1.Nginx服务应用
查看>>
MySQL基础
查看>>
凹凸贴图与法线贴图
查看>>
sqlserver跨服务器数据库sql语句
查看>>
设计模式-结构型模式,外观模式(6)
查看>>
Trie模版
查看>>
2018HDU多校训练-3-Problem F. Grab The Tree
查看>>
2016012032四则运算网页版结对项目报告
查看>>
淘宝专业版改基础版方法
查看>>
[转]ARM Pipeline
查看>>
[转]Blocking Code Injection on iOS and OS X
查看>>
颜色分类函数
查看>>