zoukankan      html  css  js  c++  java
  • 动态HIBERNATE 保存 照片

    这个题目不知道取什么好。

    其实是两个技术问题。

    1.通过HIBERNATE保存BLOB字段。这个可以参考下面的资料:

    http://edu.codepub.com/2009/0619/6636.php

    2.为了做到通用,需要动态传入PO和照片字段,所以需要设计到反射。

    http://hi.baidu.com/twssy/blog/item/cdff02df0ccf6b1763279898.html

    结合这两个技术解决这个照片动态保存问题:

        public Long saveZp(File zpFile, IXMLResultFilter filter, String zpFieldName)
                throws Exception {
            byte[] buffer = new byte[1];
            Class<?> c = null;  
            c = Class.forName(filter.getPoName());
            Object o = c.newInstance();
            BeanUtils.setProperty(o, "CJSJ", CodeManager.getInstance().getServerStringDate());
            //动态设置照片字段
            Class[] parameterTypes = new Class[1];//这里你要调用的方法只有一个参数
               parameterTypes[0] = Blob.class;//这个参数的类型是String[]
               Method method = o.getClass().getMethod("set" + zpFieldName, parameterTypes);//得到setZP方法
               Object[] objs = new Object[1];
               objs[0] = Hibernate.createBlob(buffer);
               method.invoke(o, objs);

    //        XX_ZP c = new XX_ZP();
    //        c.setCJSJ(CodeManager.getInstance().getServerStringDate());
    //        c.setZP(Hibernate.createBlob(new byte[1]));
            _hibernateDao.saveZp(o, zpFile, zpFieldName);
            return null;
        }

        public Object saveZp(Object obj, File zpFile, String zpFieldName)
                throws Exception {
            this.getHibernateTemplate().save(obj);
            this.getHibernateTemplate().flush();
            this.getHibernateTemplate().refresh(obj, LockMode.UPGRADE);
            //动态生成getZP方法
            Method method = obj.getClass().getMethod("get" + zpFieldName, new Class[]{});//得到getZP方法
            SerializableBlob bb = (SerializableBlob)method.invoke(obj, new Object[]{});
    //        SerializableBlob bb = (SerializableBlob) ((XX_ZP)obj).getZP(); // 先从java.sql.Blob转SerializableBlob   
            java.sql.Blob blob = bb.getWrappedBlob(); //再从通过getWrappedBlob()这个方法又转为java.sql.Blob,这里实在不理解   
            oracle.sql.BLOB ob = (oracle.sql.BLOB)blob;// 从java.sql.Blob转oracle.sql.BLOB   
            OutputStream out = ob.getBinaryOutputStream();   
            FileInputStream fin = new FileInputStream(zpFile);
            byte[] data = new byte[(int)fin.available()];
            fin.read(data);
            out.write(data);
            fin.close();
            out.close();
            this.getHibernateTemplate().flush();
            return obj;
        }

  • 相关阅读:
    C语言遍历文件夹里所有文件【转】
    davinci有用的连接
    VC中关于release和debug的区别【转】
    fprintf、printf、sprintf、fscanf、scanf、sscanf 格式化输入输出【转】
    达芬奇数字媒体片上系统的架构和 Linux 启动过程【转】
    shell执行的特殊变数
    解决ubuntu10插入耳机还有外音的问题
    VMware Workstation安装ubuntu10.10【图解】
    使用 WebPlayer9播放器 使得my_vod_caching文件夹巨大系统空间急剧减少
    使用指针时的“陷阱”
  • 原文地址:https://www.cnblogs.com/barryhong/p/1715295.html
Copyright © 2011-2022 走看看