无法从地图列表构造图像,每个地图都包含图像名称和Blob内容作为键值对 [英] Unable to construct images from list of maps , each map contains image name and blob content as key value pairs

查看:105
本文介绍了无法从地图列表构造图像,每个地图都包含图像名称和Blob内容作为键值对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring jdbcTemplate并使用queryForList(-)方法,我想通过迭代地图列表来从eachmap构造图像,我已经在Spring cfg文件中确定了数据源和JdbcTemplate以及我的类bean,但是当出现问题时试图从地图内容中构造图像.

i'm using Spring jdbcTemplate and using queryForList(-) method and i want to construct images from eachmap by iterating thourgh list of maps, i've declated datasource and JdbcTemplate and my class beans in spring cfg file but facing issue when trying to construct images form the content in map.

这是我的代码段

@SuppressWarnings("unchecked")
    public void test(){
        try {
            System.out.println("========Started======");
            String query="select image from save_image";
            List<Map<String,Object>> li= jdbcTemplate.queryForList(query);
            for (Map<String, Object> map : li) {
                PreparedStatementCreatorFactory psc = null;
                int[] types = {Types.BLOB};
                psc = new PreparedStatementCreatorFactory("insert into save_image_2(image) values(?)",types);
                jdbcTemplate.update(psc.newPreparedStatementCreator(new Object[]{map.get("image")}));
            }
            System.out.println("========Ended======");
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
    }
    public List<Map<String,Object>> getBlobData(){
        String query="select image from save_image_2";
        return jdbcTemplate.queryForList(query);
    }


public static void main(String[] args) throws Throwable {
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring-datasource.xml");
        TestBlob testBlob = (TestBlob)ctx.getBean("testBlob");
        System.out.println("====== Before =====");
        testBlob.test();
        List<Map<String,Object>> li = testBlob.getBlobData();
        int i=0;
        for (Map<String, Object> map : li) {
            i++;
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("/home/mphs/Softwares/eclipse/Home/Softwares/ews/Platform/src/WhenMyBOss"+i+".gif")));
            bos.write(((Blob)map.get("image")).getBytes(1, (int)((Blob)map.get("image")).length()),0,(int)((Blob)map.get("image")).length());
            //bos.write((map.get("image").toString()).getBytes());
        }
        System.out.println("====== After =====");
    }

堆栈跟踪

INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2f333739: defining beans [dataSource,jdbcTemplate,testBlob]; root of factory hierarchy
Jun 02, 2014 5:33:19 PM org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName
INFO: Loaded JDBC driver: com.mysql.jdbc.Driver
====== Before =====
========Started======
========Ended======
Exception in thread "main" java.lang.ClassCastException: [B cannot be cast to java.sql.Blob
    at com.dao.Example.main(Example.java:24)

推荐答案

您假设JDBC驱动程序中的数据库Blob类型已映射到java.sql.Types.BLOB(因此也映射到了java.sql.Blob),但是它看起来像ResultSet.getObject()为此返回一个字节数组(这意味着blob类型实际上被映射为java.sql.Types.LONGVARBINARY),如所示." [B无法转换为java.sql.Blob"( [B 是字节数组).

You are assuming that the database blob type in your JDBC driver is mapped to java.sql.Types.BLOB (and therefor to a java.sql.Blob), but it looks like ResultSet.getObject() for this returns a byte array (which means that the blob type is actually mapped as a java.sql.Types.LONGVARBINARY) as indicated by "[B cannot be cast to java.sql.Blob" ([B is a byte array).

因此您应该可以使用:

bos.write((byte[]) map.get("image"));

这篇关于无法从地图列表构造图像,每个地图都包含图像名称和Blob内容作为键值对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆