【高分求助】oracle的blob字段存取照片 大家好,我的开发框架是struts2+ibatis2.3+spring,请问如何把图片存到oracle9的blob字段里,如何读取出来显示在jsp页面上,最好是给详细代码和配置文件参考参考啊,不胜感激!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://huazhouji.javaeye.com/blog/111195 这种方式我会用,但是我想用的是把图片直接存到数据库里的这种方式。图片可以存到blob或者clob这种大字段里面 1。首先在Oracle 中创建一个表: create table blobtest( ID int primary key, pic blob); 注意:BLOB类型用 SQL*Plus 无法显示 2。编写BlobTest 类 /** * */ package test; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.sql.Blob; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; /** * @author Administrator * */ public class BlobTest { /** * @param args */ Connection con = null; /* * 向数据库中添加BLOB数据 */ public void addBlob() { // 创建一个PreparedStatement实例 try { PreparedStatement pstmt = con .prepareStatement("insert into blobtest values(1,?)"); File file = new File("c:\\test.jpg"); FileInputStream fis = new FileInputStream(file); // 把输入流设置为预处理语句的对象 pstmt.setBinaryStream(1, fis, (int) file.length()); // 执行更新 pstmt.executeUpdate(); pstmt.close(); fis.close(); } catch (Exception e) { // TODO 自动生成 catch 块 e.printStackTrace(); } } // // 创建一个表 // public void createTable() { // try { // con.createStatement().execute( // "create table blobtest(id int,pic blob," // + "constraint pk_blobtest primary key(id));"); // } catch (Exception e) { // // TODO 自动生成 catch 块 // e.printStackTrace(); // } // } /** * 从数据库中读取BLOB数据 * * @param args */ public void getBlob() { // 创建一个Statement实例 try { Statement stmt = con.createStatement(); ResultSet rst = stmt.executeQuery("select * from blobtest where id=1"); rst.next(); // 获取BLOB数据和它的输入流,然后通过输入流把数据写到文件中 Blob blob = rst.getBlob("pic"); FileOutputStream out = new FileOutputStream(new File("c:\\k.jpg")); InputStream in = blob.getBinaryStream(); int i; while ((i = in.read()) != -1) out.write(i); // 关闭输入、输出流 in.close(); out.close(); } catch (Exception e) { // TODO 自动生成 catch 块 e.printStackTrace(); } } public static void main(String[] args) { // TODO 自动生成方法存根 try { Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); BlobTest test = new BlobTest(); String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl"; String user = "system"; String pwd = "kimmy"; test.con = DriverManager.getConnection(url, user, pwd); //test.createTable(); test.addBlob(); test.getBlob(); } catch (Exception e) { // TODO 自动生成 catch 块 e.printStackTrace(); } } } 转自:http://lidapeng2100.javaeye.com/blog/633679 http://3ccoder.javaeye.com/blog/497253 还是不知道ibatis怎么配,呵呵,谢了,哥们儿 http://huazhouji.javaeye.com/blog/111195 我觉得,不能完全指望别人给你提供的代码能够拿来就用,这个链接有个例子,我觉得可以解决你的问题,struts+ibatis+spring,其实,入blob的问题,和struts与spring都无关,首先搞定ibatis写入blob的问题,其它地方使用即可。http://twit88.com/blog/2008/07/15/java-insert-a-blob-object-using-ibatis/ 简单的很,楼上的已经答复了,我把代码复制出来你看看。。 1. public static void main(String[] args) { 2. //获得Spring的上下文对象 3. ApplicationContext ac = new ClassPathXmlApplicationContext("/applicationContext.xml"); 4. //存图片 5. PresentDao dao = (PresentDao) ac.getBean("presentDao"); 6. 7. FileInputStream fis = null; 8. try { 9. ADPresent p = new ADPresent(); 10. p.setName("世博会吉祥物海宝钥匙扣"); 11. p.setDescription("纪念物品"); 12. p.setPrice(25.5); 13. p.setQtyRemain(100L); 14. Blob photo = null; 15. //将图片读进输入流 16. fis = new FileInputStream("D:\\1.jpg"); 17. // 转成Blob类型 18. photo = Hibernate.createBlob(fis); 19. p.setPhoto(photo); 20. dao.savePresent(p); 21. fis.close(); 22. } catch (Throwable t) { 23. t.printStackTrace(); 24. } 25. } 26. 27. 28. 29. 30. ==============ADPresent的代码============== 31. private Long objectRrn; 32. private String name; 33. private String description; 34. private Double price; 35. private Long qtyRemain; 36. private Blob photo; 37. 38. setter/getter方法 39. 40. 41. ==============PresentDao的代码============== 42. //使用spring的依赖注入获得sessionfactory 43. private SessionFactory sf; 44. 45. public ADPresent savePresent(ADPresent present) throws Throwable{ 46. Session session = null; 47. Transaction tx = null; 48. try { 49. session = sf.openSession(); 50. tx = session.beginTransaction(); 51. //使用session的save方法可以持久化Blob属性 52. session.saveOrUpdate(present); 53. tx.commit(); 54. } catch (Exception e) { 55. if(tx!=null) 56. tx.rollback(); 57. throw e; 58. } finally{ 59. session.close(); 60. } 61. return present; 62. } 页面刷新之后数据重复显示 请教:EJB中createNativeQuery(sql)对于char类型字段的处理 ajax怎么生成1000到9999的随机数? 如何在jsp页面中进行button按钮传参 本群接受正在学习的准IT人士,共同探讨,一起进步 如何才能检测到有数据包传过来? JDBC连接可数据库出错,附原代码和错误报告,高手指教!! select 默认选择的问题! 请问Servlet里如何调用JavaBean? 正则表达式/^[0-9]+$/和^[0-9]+$有什么不同吗,前后的"/"有什么用 方法返回list然后在页面显示列表 紧急求助,JSP网站速度变慢,怎么优化
1。首先在Oracle 中创建一个表:
create table blobtest(
ID int primary key,
pic blob);
注意:BLOB类型用 SQL*Plus 无法显示
2。编写BlobTest 类
/**
*
*/
package test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
/**
* @author Administrator
*
*/
public class BlobTest {
/**
* @param args
*/
Connection con = null;
/*
* 向数据库中添加BLOB数据
*/
public void addBlob() {
// 创建一个PreparedStatement实例
try {
PreparedStatement pstmt = con
.prepareStatement("insert into blobtest values(1,?)");
File file = new File("c:\\test.jpg");
FileInputStream fis = new FileInputStream(file);
// 把输入流设置为预处理语句的对象
pstmt.setBinaryStream(1, fis, (int) file.length());
// 执行更新
pstmt.executeUpdate();
pstmt.close();
fis.close();
} catch (Exception e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
// // 创建一个表
// public void createTable() {
// try {
// con.createStatement().execute(
// "create table blobtest(id int,pic blob,"
// + "constraint pk_blobtest primary key(id));");
// } catch (Exception e) {
// // TODO 自动生成 catch 块
// e.printStackTrace();
// }
// }
/**
* 从数据库中读取BLOB数据
*
* @param args
*/
public void getBlob() {
// 创建一个Statement实例
try {
Statement stmt = con.createStatement();
ResultSet rst = stmt.executeQuery("select * from blobtest where id=1");
rst.next();
// 获取BLOB数据和它的输入流,然后通过输入流把数据写到文件中
Blob blob = rst.getBlob("pic");
FileOutputStream out = new FileOutputStream(new File("c:\\k.jpg"));
InputStream in = blob.getBinaryStream();
int i;
while ((i = in.read()) != -1)
out.write(i);
// 关闭输入、输出流
in.close();
out.close();
} catch (Exception e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO 自动生成方法存根
try {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
BlobTest test = new BlobTest();
String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
String user = "system";
String pwd = "kimmy";
test.con = DriverManager.getConnection(url, user, pwd);
//test.createTable();
test.addBlob();
test.getBlob();
} catch (Exception e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
转自:http://lidapeng2100.javaeye.com/blog/633679
这个链接有个例子,我觉得可以解决你的问题,
struts+ibatis+spring,
其实,入blob的问题,和struts与spring都无关,
首先搞定ibatis写入blob的问题,其它地方使用即可。http://twit88.com/blog/2008/07/15/java-insert-a-blob-object-using-ibatis/
2. //获得Spring的上下文对象
3. ApplicationContext ac = new ClassPathXmlApplicationContext("/applicationContext.xml");
4. //存图片
5. PresentDao dao = (PresentDao) ac.getBean("presentDao");
6.
7. FileInputStream fis = null;
8. try {
9. ADPresent p = new ADPresent();
10. p.setName("世博会吉祥物海宝钥匙扣");
11. p.setDescription("纪念物品");
12. p.setPrice(25.5);
13. p.setQtyRemain(100L);
14. Blob photo = null;
15. //将图片读进输入流
16. fis = new FileInputStream("D:\\1.jpg");
17. // 转成Blob类型
18. photo = Hibernate.createBlob(fis);
19. p.setPhoto(photo);
20. dao.savePresent(p);
21. fis.close();
22. } catch (Throwable t) {
23. t.printStackTrace();
24. }
25. }
26.
27.
28.
29.
30. ==============ADPresent的代码==============
31. private Long objectRrn;
32. private String name;
33. private String description;
34. private Double price;
35. private Long qtyRemain;
36. private Blob photo;
37.
38. setter/getter方法
39.
40.
41. ==============PresentDao的代码==============
42. //使用spring的依赖注入获得sessionfactory
43. private SessionFactory sf;
44.
45. public ADPresent savePresent(ADPresent present) throws Throwable{
46. Session session = null;
47. Transaction tx = null;
48. try {
49. session = sf.openSession();
50. tx = session.beginTransaction();
51. //使用session的save方法可以持久化Blob属性
52. session.saveOrUpdate(present);
53. tx.commit();
54. } catch (Exception e) {
55. if(tx!=null)
56. tx.rollback();
57. throw e;
58. } finally{
59. session.close();
60. }
61. return present;
62. }