您的位置:首页 > 数据库 > Oracle

JAVA通过Hibernate将文件写入ORACLE数据的BLOB字段 代码示例

2013-04-15 10:26 846 查看
//将文件写入字节数组,可以将BOLB对应的实体类映射为字节数组类型,如下:

映射文件:

<hibernate-mapping>

…………

<property name="fileContent" type="binary">//设置对应的映射type为binary 就可以存储字节数组

<column name="file_content"/>
</property>

…………

</hibernate-mapping>

对应的实体类:

public  class  FileBean  {

…………

private byte[] fileContent; //直接映射为byte[]

…………

}

struts.xml

<action name="uploadFile" class="uploadAction" method="uploadFile">

<interceptor-ref name="fileUploadStack"/>

<interceptor-ref name="defaultStack"/>

<result type="json">

<param name ="includeProperties">success,message</param>

<param name="contentType">text/html</param>

</result>

</action>

JAVA部分代码:

Java.io.File file = fileList.get(x);//获取前台传入的文件

byte[] fileByteArray = new byte[(int)file.length()] //根据文件长度创建字节数组

 FileInputStream in = new FileInputStream(file);//获取流

in.read(fileByteArray,0,fileByteArray.length);//将文件流写入字节数组

//最后将字节数组通过Hibernate设置入库就OK 了

FileBean  bean =  new FileBean();

……

bean.setFileContent();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: