1:在applicationContext-*.xml的配置文件中
<bean id="nativeJdbcExtractor" class="org.springframework.jdbc.support.nativejdbc.SimpleNativeJdbcExtractor" />
<bean id="oracleLobHandler" class="org.springframework.jdbc.support.lob.OracleLobHandler" >
<property name="nativeJdbcExtractor" ref="nativeJdbcExtractor"></property>
</bean>
<bean id="lobHandler" lazy-init="true" class="org.springframework.jdbc.support.lob.DefaultLobHandler"/>
2:在sessionFactory Bean中
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="lobHandler">
<ref bean="oracleLobHandler" />
</property>
</bean>
加入这个属性
<property name="lobHandler">
<ref bean="oracleLobHandler" />
</property>
3:配置实体的hibernate的*.hbm.xml
<property name="person18" type="org.springframework.orm.hibernate3.support.BlobByteArrayType" length="1048576000">
<column name="PERSON18" />
</property>
这个type是关键
在实体的 PERSON18 属性 的类型是byte[] ,配置基本结束
4:怎样存储呢?
这里是struts2+hibernate+spring 架构
在action中 private File[] upload; 这个upload其实就是jsp页面的上传<input type="file" name="upload"
在提交的时候 action中 开始把上传的图片set到实体的相关属性中去
InputStream in = null;
in = new BufferedInputStream(new FileInputStream(this.getUpload()[0]), BUFFER_SIZE);
byte[] buffer = new byte[BUFFER_SIZE];
in.read(buffer);
//把上传的图片set到 实体中去
person.setPerson18(buffer);
最好直接save实体就可以了
5:读取显示在页面
本人最初的想法是在action中把对象查出来,也就可以得到保存图片的byte[] ,然后放到request中,在页面直接读取,结果失败,
最后的解决办法是
jsp页面:
<img id="imgShow" width="100%" height="100%" src="imageAction.action?person01=${mk.person01}" alt="" />
后台action中
public String loadImage() throws Exception{
InputStream fileInput = null;
String person01 = request.getParameter("person01");
File dir = new File(ServletActionContext.getServletContext().getRealPath("")+ "/1.jpg");
Person p = super.getPersonService().getPersonById(Integer.parseInt(person01));
byte[] bs = p.getPerson18();
if(bs == null){
fileInput = new BufferedInputStream(new FileInputStream(dir), 100*1024);
byte[] buffer = new byte[100*1024];
int i = fileInput.read(buffer);
upLoadImg(buffer);
fileInput.close();
}else{
upLoadImg(bs);
}
return null;
/**
* 上传图片
* @param bs
*/
public void upLoadImg(byte[] bs){
ServletOutputStream out = null;
InputStream in = null;
InputStream in2 = null;
try {
//二进制输出流
response.setContentType("image/jpeg");
//得到输出流
out = response.getOutputStream();
in = new java.io.ByteArrayInputStream(bs);
//强制刷新输出流
out.write(bs);
out.flush();
} catch ( IOException e ) {
e.printStackTrace();
} catch ( Exception e ) {
e.printStackTrace();
} finally {
if ( in != null ) {
try {
in.close();
} catch ( IOException e ) {
e.printStackTrace();
}
}
if ( out != null ) try {
out.close();
} catch ( IOException e ) {
e.printStackTrace();
}
}
}
这样就可以了
如有疑问:qq 331887602 最好发邮件哈 有现成的项目实例