SSH框架 Blob类型图片处理

来源:互联网 发布:爱普生清零软件中文版 编辑:程序博客网 时间:2024/06/15 13:37


SSH框架下处理Blob类型数据

实体类 省略


在Dao方法里面添加    获取Session在根据ID读取数据库数据方法

public Student getsession(Long id) {
Session session = this.getSession();
Student st = (Student ) session.get(Student .class,id);
return st;

}



Serrvice中处理数据库读取的Blob类型数据


Student st=  this.studentDao.getsession(Long.valueOf(studentId));//调用Dao方法

InputStream in = st.getPicture().getBinaryStream();//获取blob类型数据转换为流

//获取项目目录   设置文件路径和名称类型 
String pt= request.getSession().getServletContext().getRealPath("/")+"\\image\\"+sysToken.getUserId()+"_"+st.getstudentId()+"1.png";
File file = new File(pt);
if(file.exists()) {
file.delete();
}
FileOutputStream fos = new FileOutputStream(file);
byte[] b1 = new byte[1024];  

int nRead = 0;  
while ((nRead = in.read(b1)) != -1) {  写入图片
fos.write(b1, 0, nRead);  
 }  
fos.flush();  
fos.close();  
in.close();


String path= "image/"+sysToken.getUserId()+"_"+ae06.getAe0601()+"1.png";

return path;//返回图片路径





原创粉丝点击