开发中文件上传到服务器,一般将文件保存在Web服务器的某个目录下,除非有特殊要求将文件存到数据库中保存。
本文主要基于学习的目的而作。
测试环境:Hibernate3.6.7,Oracle 10g Express,JDK7,Win7
1,数据库脚本
create table TUser (
ID char(32) not null,
name varchar(10char) not null,
photo blob, --头像
constraint PK_TUser primary key (ID)
);
2,Hibernate配置文件,本文基于传统的hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property name="connection.url">
jdbc:oracle:thin:@localhost:1521:XE
</property>
<property name="connection.username">tanlan</property>
<property name="connection.password">tanlan</property>
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="show_sql">true</property>
<mapping resource="com/tanlan/hibernate/entity/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
3,编写实体类User.java
package com.tanlan.hibernate.entity;
public class User {
private String id;
private String name;
private byte[] photo;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public byte[] getPhoto() {
return photo;
}
public void setPhoto(byte[] photo) {
this.photo = photo;
}
}
4,编写对应的映射文件,User.hbm.xml
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.tanlan.hibernate.entity">
<class name="User" table="TUser">
<id name="id">
<generator class="uuid" />
</id>
<property name="name" />
<property name="photo" type="binary" />
</class>
</hibernate-mapping>
5,测试代码
package com.tanlan.hibernate.test;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.tanlan.hibernate.entity.User;
public class TestUser {
public static void main(String[] args) {
addUser();
//getUserById();
//update();
}
private static void addUser() {
User user = new User();
user.setName("谭岚");
File photo = new File("I:\\1.jpg");
try {
FileInputStream is = new FileInputStream(photo);
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte[] temp = new byte[512];
int i = 0;
while ((i = is.read(temp, 0, temp.length)) != -1) {
os.write(temp, 0, temp.length);
}
os.close();
is.close();
user.setPhoto(os.toByteArray());
} catch (Exception e) {
e.printStackTrace();
}
Session session = openSession();
Transaction transaction = session.beginTransaction();
session.save(user);
transaction.commit();
}
private static void getUserById() {
Session session = openSession();
User user = (User) session.get(User.class,
"402881e432993eae0132993eb7d20000");
byte[] photo = user.getPhoto();
try {
FileOutputStream os = new FileOutputStream("E:\\111.jpg");
os.write(photo);
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private static void update() {
User user = new User();
user.setId("402881e432993eae0132993eb7d20000");
user.setName("新名");
File photo = new File("I:\\2.jpg");
try {
FileInputStream is = new FileInputStream(photo);
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte[] temp = new byte[512];
int i = 0;
while ((i = is.read(temp, 0, temp.length)) != -1) {
os.write(temp, 0, temp.length);
}
os.close();
is.close();
user.setPhoto(os.toByteArray());
} catch (Exception e) {
e.printStackTrace();
}
Session session = openSession();
Transaction transaction = session.beginTransaction();
session.update(user);
transaction.commit();
}
private static Session openSession() {
Configuration cfg = new Configuration().configure();
return cfg.buildSessionFactory().openSession();
}
}
分享到:
相关推荐
JDBC+Hibernate将Blob数据写入Oracle
很多朋友在操作oracle数据库的clob,blob字段时不知道怎么插入和使用,这里提供读写范例。
使用JDBC和Hibernate来写入Blob型数据到Oracle中;使用JDBC和Hibernate来写入Blob型数据到Oracle中
C#访问oracle数据库类 1、执行非查询命令 2、执行查询命令 3、带blob字段读写
oracle中用来存储大数据的BLOB(图片,文本,文件等)的 读写与复制方法
Struts2 Hibernate存取Oracle数据库中的BLOB数据.pdf
jdbc 操作oracle blob数据jdbc 操作oracle blob数据jdbc 操作oracle blob数据jdbc 操作oracle blob数据jdbc 操作oracle blob数据jdbc 操作oracle blob数据jdbc 操作oracle blob数据jdbc 操作oracle blob数据jdbc ...
用hibernate和jdbc读存blob或clob
C# oracle blob类型 读入与保存
Oracle blob字段上传下载 Oracle blob字段上传下载Oracle blob字段上传下载 Oracle blob字段上传下载
批量导出oracle bolb图片到本地文件
oracle Blob转换Clob
hibernate对Blob类型字段进行数据添加.txt
Hibernate读取blob字段
从oracle中的BLOB类型字段中取出照片,转存到mysql数据库中。思路是现将oracle中的照片存储在本地文件夹,在将本地图片上传到mysql中。
只需要安装oracle client在本地,然后配置好tns,输入连接名,用户名,密码,数据表的名称,文件名数据列名以及blob数据列名,就可以批量导出ORACLE数据库BLOB字段生成图片
只需要安装oracle client在本地,然后配置好tns,输入连接名,用户名,密码,数据表的名称,文件名数据列名以及blob数据列名,就可以批量导出ORACLE数据库BLOB字段生成图片
该程序可将ORACLE中BLOB字段导入到SQL SERVER中的IMAGE字段,方便快捷。