使用ssh向sqlserver2005数据库中保存image类型的二进制图片
来源:互联网 发布:一元一级域名 编辑:程序博客网 时间:2024/06/06 19:21
1.首先设计数据库表,其中photo、photo2字段均为image类型的。
2.建立实体bean对象,设置两个字段为byte[]如: private byte[] photo; private byte[] photo2;
3.建立hibernate与数据库的映射文件hbm.xml,其中photo、photo2字段的映射如下:
<property generated="never" lazy="false" name="photo" type="binary">
<column name="photo"/>
</property>
<property generated="never" lazy="false" name="photo2" type="binary">
<column name="photo2"/>
</property>
4.建立前台上传选择文件的页面:
<form id="inputForm" action="./saveStudent.action" method="post" enctype="multipart/form-data">
<tr>
<th class="tdtitle">上传图片:</th>
<td class="tdcell"><input type="file" id="file" name="file" value="浏览"/></td>
</tr>
<tr>
<th class="tdtitle">上传图片:</th>
<td class="tdcell"><input type="file" id="file2" name="file2" value="浏览"/></td>
</tr>
</form>
5.在后台action中创建File对象file,并写set、get方法。在save方法中作如下处理(将输入流转换为byte数组):
InputStream inputStream=null;
inputStream = new FileInputStream(file);
byte bytes[]=new byte[inputStream.available()];
inputStream.read(bytes);
inputStream.close();
inputStream=new FileInputStream(file2);
byte bytes2[]=new byte[inputStream.available()];
inputStream.read(bytes2);
inputStream.close();
price.setPhoto(bytes);
price.setPhoto2(bytes2);
在显示的函数中做如下处理:(从数据库中取得二进制数据,转换为输出流)
byte [] buf=null;
if(pro.equals("1")){
buf=list.get(0).getPhoto();
}else{
buf=list.get(0).getPhoto2();
}
response.setContentType("image/jpg");
OutputStream outputStream=response.getOutputStream();
outputStream.write(buf);
outputStream.flush();
在显示的前台页面作如下处理:
<tr><td>图片1:</td><td><img src="<%=basePath %>getImageStudent.action?no=<s:property value="student.no"/>&&kind=1"/></td></tr>
<tr><td>图片2:</td><td><img src="<%=basePath %>getImageStudent.action?no=<s:property value="student.no"/>&&kind=2"/></td></tr>
- 使用ssh向sqlserver2005数据库中保存image类型的二进制图片
- .Net中保存和读取数据库中Image类型(二进制)字段
- jdbc向数据库中保存图片
- 如何在网页上显示数据库中保存的图片
- Tapestry 怎样从数据库中保存的图片显示出来。
- asp中读取数据库中保存的图片存入硬盘
- Vb.net向sql server数据库中保存图片及图片管理
- 图片二进制存数据库
- Vb.net向sql server数据库中保存图片(一)
- Vb.net向sql server数据库中保存图片(二)
- Vb.net向sql server数据库中保存图片(一)
- 在数据库中保存和读取图片
- 二进制与文件的互相转换,以及在数据库中保存
- 如何在SQLite数据库中保存image数据
- Android 中保存图片的代码
- java中保存图片的缩略图
- Android中保存图片的方式
- matlab中保存图片的方法
- struts2 + JSON
- [易飞]用户密码加密解密
- 10723Cyborg Genes (LCS + 记忆化搜索)
- Spring 接口根据MAP值动态选择实现类
- 开源 免费 java CMS - FreeCMS1.5-数据对象-info
- 使用ssh向sqlserver2005数据库中保存image类型的二进制图片
- MSSQL还原数据库失败
- linux 判断文件是否存在的access()方法与示例
- 网络挂载
- JS和CSS的多浏览器兼容总结
- http://ffmpeg-windows.googlecode.com/svn/trunk/
- Top 8 Diagrams for Understanding Java
- Leetcode: Permutations and Permutations II
- 大学目标