.NET Oracle Blob Clob 类型帮助类

/*-------------------------------------------------------------------------
 * 作者:无天
 * 版本号:v1.0
 * 本类主要用途描述及食用方式:
 * 大型文件对象帮助类
 *
 * BLOB:Byte Large Object (比特型)
 * C#参数类型:Byte[] <=> Oracle类型:BLOB
 *
 * CLOB:Character Large Object (字符型)
 * C#参数类型:String <=> Oracle类型:CLOB
 *  -------------------------------------------------------------------------*/

using System;
using System.IO;
using System.Text;
using System.Web;

namespace Common.Data.SystemTools
{
    public static class LargeObjectHelper
    {
        #region BLOB

        /// <summary>
        /// 上传的文件转BLOB二进制(存数据的时候用)
        /// </summary>
        /// <param name="httpPostedFile">上传的HttpPostedFile文件</param>
        /// <returns>对应BLOB类型的二进制</returns>
        public static byte[] HttpPostedFileToBlob(HttpPostedFile httpPostedFile)
        {
            Stream stream = httpPostedFile.InputStream;
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);
            stream.Seek(0, SeekOrigin.Begin);
            return bytes;
        }

        /// <summary>
        /// Oracle的Blob字段转文件流(取数据的时候用)
        /// </summary>
        /// <param name="blobData">DataRow中的Blob列</param>
        /// <returns>Stream</returns>
        public static MemoryStream BlobToStream(object blobData)
        {
            byte[] bytes = blobData as byte[];
            MemoryStream stream = new MemoryStream(bytes);
            return stream;
        }

        #endregion BLOB

        #region Clob

        /// <summary>
        /// 上传的文件转CLOB字符串(存数据的时候用)
        /// </summary>
        /// <param name="httpPostedFile">上传的HttpPostedFile文件</param>
        /// <returns>对应CLOB类型的字符串</returns>
        public static string HttpPostedFileToClob(HttpPostedFile httpPostedFile)
        {
            byte[] fileBytePicture = new byte[httpPostedFile.ContentLength];
            Stream fileStream = httpPostedFile.InputStream;
            fileStream.Read(fileBytePicture, 0, httpPostedFile.ContentLength);

            //文件流转比特(CLOB用)
            StringBuilder strBuilder = new StringBuilder();
            foreach (byte bt in fileBytePicture)
            {
                strBuilder.AppendFormat("{0:X2}", bt);
            }
            string clobString = strBuilder.ToString();

            return clobString;
        }

        /// <summary>
        /// Stream转CLOB字符串(存数据的时候用)
        /// </summary>
        /// <param name="httpPostedFile"></param>
        /// <returns></returns>
        public static string StreamToClob(Stream stream)
        {
            byte[] fileByte = new byte[stream.Length];

            //这行非常重要!可以避免很隐晦的问题。
            //如果之前读取或操作过这个流,那么本次读取的开始位置将会是流的最后一个字节往后,导致取出来的字节都是0;
            stream.Position = 0;

            stream.Read(fileByte, 0, (int)stream.Length);

            //文件流转比特(CLOB用)
            StringBuilder strBuilder = new StringBuilder();
            foreach (byte bt in fileByte)
            {
                strBuilder.AppendFormat("{0:X2}", bt);
            }
            string clobString = strBuilder.ToString();

            return clobString;
        }

        /// <summary>
        /// Oracle的Clob字段转文件流(取数据的时候用)
        /// </summary>
        /// <param name="clobData">DataRow中的Clob列</param>
        /// <returns>Stream</returns>
        public static MemoryStream ClobToStream(object clobData)
        {
            string clobString = clobData.ToString();
            byte[] bytes = new byte[clobString.Length / 2];
            for (int i = 0; i < clobString.Length / 2; i++)
            {
                int btvalue = Convert.ToInt32(clobString.Substring(i * 2, 2), 16);
                bytes[i] = (byte)btvalue;
            }
            MemoryStream stream = new MemoryStream(bytes);
            return stream;
        }

        #endregion Clob
    }
}
posted @ 2021-08-09 16:32  sorachannel  阅读(77)  评论(0)    收藏  举报