java插入PDF文件流到oracle数据库,和读取数据库文件流
插入:
File file = new File("D://b9ef5e9f2ec04dfd984fa55ae6552ee6-1.pdf"); if (file.exists()) { InputStream fin = new FileInputStream(file); // 插入附件二进制数据 String sql = "insert into 表名 (ID,CS) values('111'," + "?)"; dao.updateBinary(sql,fin); if (fin != null) { fin.close(); } } dao.flush();
读取:
try {
  List<Map> dataListByFullSql = dao.getDataListByFullSql("select * from  表名 where ID='111'");
              //将base64编码的字符串解码成字节数组
            Blob blob =  (Blob) dataListByFullSql.get(0).get("CS");
            InputStream is=blob.getBinaryStream();
              FileOutputStream fos = new FileOutputStream("D://AAA//b9ef5e9f2ec04dfd984fa55ae6552ee6-1.pdf");
            //定义缓冲区
            int length=(int)blob.length();
            byte bt[] = new byte[length];
            try {
                // 向缓冲区中 读取数据
                while ((length = is.read(bt)) != -1) {
                    // 把缓冲区的述据 写出去
                    fos.write(bt);
                }
            } catch (IOException ex) {
            }
            // 关闭输入流
            is.close();
            // 关闭输出流
            fos.close();
            } catch (IOException e) {
              e.printStackTrace();
            }
/** * 根据完整SQL语句一次新查询所有数据,非参数化查询 * */ @SuppressWarnings("rawtypes") public List<Map> getDataListByFullSql(String fulSql) { Query query = this.getCurrentSession().createSQLQuery(fulSql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP); @SuppressWarnings("unchecked") List<Map> list = query.list(); return list; }
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号