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();
映射文件:
<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();
相关文章推荐
- java hibernate 映射和注解oracle含有blob字段的数据表的pojo源码
- Java实体添加字段 值没有进入数据库 hibernate配置文件正确 java代码正确
- JDBC+Hibernate将Blob数据写入Oracle
- JDBC+Hibernate将Blob数据写入Oracle
- Java 通过 BufferReader 实现 文件 写入读取 示例
- JDBC+Hibernate将Blob数据写入Oracle
- JDBC+Hibernate将Blob数据写入Oracle
- Hadoop学习笔记——1.java读取Oracle中表的数据,创建新文件写入Hdfs
- 利用Java代码把oracle的blob字段内容解析成XML格式的字符串
- 将oracle中blob数据写入文件,再把文件读取插入数据库
- JDBC+Hibernate将Blob数据写入Oracle
- JDBC+Hibernate将Blob数据写入Oracle
- Hibernate操作Blob类型字段出现java.lang.AbstractMethodError: oracle.jdbc.driver.OracleResultSetImpl.getBlob(Ljava/lang/String;)Ljava/sql/
- 用java代码实现从excel表格读取数据然后写入生成国际化配置文件properties
- Oracle读取Blob数据-通过hibernate
- Java将数据写入Oracle的Clob字段中
- [转贴]Java中操作数据库的Blob字段,将文件写入数据库!!!
- JDBC+Hibernate将Blob数据写入Oracle
- 使用JDBC和Hibernate在oracle中插入和读取Blob数据的图片的java程序
- JDBC+Hibernate将Blob数据写入Oracle