使用ssh向sqlserver2005数据库中保存image类型的二进制图片
2013-10-30 22:52
369 查看
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>
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类型的二进制图片
- 【翻译】C# 使用Image Guid 验证图片类型
- FMDB使用的一点心得:数据库创建、制表、查询等以及image转换成二进制nsdata保存到数据库中
- 用动软生成三层后添加数据库二进制image类型图片时注意点
- Silvelight保存Image图片到SQL数据库的Image类型里面(亲自测试)
- Validate Image Type Using Image GUID in ASP.NET(Asp.Net中使用图片GUID验证图片类型)
- C# 使用Image Guid 验证图片类型 (转载,感谢原作者)
- .net中使用SQL Server的Image字段类型储存图片
- 将Image类型的图片文件保存到Mysql数据库
- C# 使用Image Guid 验证图片类型
- struts2中使用Blob类型处理图片上传保存在数据库中并在JSP页面中显示图片
- 将数据库中以image类型保存的图片另存为jpg文件,并将其路径存入数据库
- 使用gdi+的Image旋转图片并保存
- C#中使用FileUpload上传图片到SQL数据库中以image类型存储并使用Image控件显示注意事项
- Python3 初学实践案例(12)将源目录中的图片根据设定最长边参数保存到目标目录脚本(Image 的使用)
- Delphi 中使用封装的TWICImage加载不同类型的图片
- 保存图片控件上的图片到本地 出现错误:无法将类型为“System.Windows.Media.Imaging.BitmapFrameDecode”的对象强制转换为类型“System.Windows.Media.Imaging.BitmapImage”。
- 在 Oracle 和 PHP 中使用 LOB 附 php 保存图片二进制到oracle数据库
- caffe学习之conver_imageset.bin的使用方法,caffe下图片转lmdb格式类型数据总结
- .Net中保存和读取数据库中Image类型(二进制)字段