- 浏览: 249974 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (217)
- java 基础 (36)
- flex (1)
- structs 2.0 (5)
- oracle 学习过程 (20)
- j2ee (63)
- c/s java (0)
- javaScript (24)
- jdom (1)
- jquery (1)
- java模式 (1)
- Datebase common konwledge (3)
- JfreeChart (3)
- html (3)
- sql server (6)
- tomcat servlet (4)
- lucene (1)
- Linux (3)
- 常见注意 (6)
- hibernate (2)
- jsp页面 (3)
- effective Java (1)
- spring (2)
- Spark (1)
- json (0)
- JMS (1)
- 电脑故障 (2)
- mysql (1)
- JBPM (2)
- java 线程 (1)
- oracle优化 (1)
- 安全 (1)
- hadoop (0)
- 算法 (1)
- hbase (0)
最新评论
-
mfhappy:
你好,你这个导出的文件中如果有图片你怎么处理??
jsp 导出excel(利用excel本身另存为html) -
丹寺丁:
...
jquery 解析 java 后台的 json数据 -
xurichusheng:
不直接诶生成想要的对象,而是让客户端利用所有必要的参数 ...
重叠构造器、Builder模式 -
wen121221222:
感谢啊!困恼我一个下午的问题终于解决了
java.lang.NoClassDefFoundError: javax/mail/Authenticator异常 -
gaoyanglovejava:
不错。就是这么解决的。多谢
The project is not a myEclipse hibernate project
整个流程分为四步,连接oracle数据库 -> 读取blob图片字段 -> 对图片进行缩放 ->把图片展示在jsp页面上。
下面进行详细描述:
1. java连接Oracle
注:数据库是Oracle10g版本为10.2.0, 在数据库中,图片字段类型为BLOB。
java中通常使用的是通过jdbc驱动来连接数据库,oracle也不例外,因此必须下载一个Oracle驱动的jdbc需要去网上进行下载,名称为 ojdbc14.jar
01.import java.sql.*; 02. 03.import java.io.*; 04. 05. 06. 07.import javax.imageio.ImageIO; 08. 09.import java.awt.image.BufferedImage; 10. 11.import java.awt.image.AffineTransformOp; 12. 13.import java.awt.geom.AffineTransform; 14. 15. 16. 17.public class OracleQueryBean { 18. 19. private final String oracleDriverName = "oracle.jdbc.driver.OracleDriver"; 20. 21. private Connection myConnection = null; 22. 23. /*图片表名*/ 24. 25. private String strTabName; 26. 27. /*图片ID字段名*/ 28. 29. private String strIDName; 30. 31. /*图片字段名*/ 32. 33. private String strImgName; 34. 35. /** 36. 37. * 加载java连接Oracle的jdbc驱动 38. 39. */ 40. 41. public OracleQueryBean(){ 42. 43. try{ 44. 45. Class.forName(oracleDriverName); 46. 47. }catch(ClassNotFoundException ex){ 48. 49. System.out.println("加载jdbc驱动失败,原因:" + ex.getMessage()); 50. 51. } 52. 53. } 54. 55. /** 56. 57. * 获取Oracle连接对象 58. 59. * @return Connection 60. 61. */ 62. 63. public Connection getConnection(){ 64. 65. try{ 66. 67. //用户名+密码; 以下使用的Test就是Oracle里的表空间 68. 69. //从配置文件中读取数据库信息 70. 71. GetPara oGetPara = new GetPara(); 72. 73. String strIP = oGetPara.getPara("serverip"); 74. 75. String strPort = oGetPara.getPara("port"); 76. 77. String strDBName = oGetPara.getPara("dbname"); 78. 79. String strUser = oGetPara.getPara("user"); 80. 81. String strPassword = oGetPara.getPara("password"); 82. 83. 84. 85. this.strTabName = oGetPara.getPara("tablename"); 86. 87. this.strIDName = oGetPara.getPara("imgidname"); 88. 89. this.strImgName = oGetPara.getPara("imgname"); 90. 91. 92. 93. String oracleUrlToConnect ="jdbc:oracle:thin:@"+strIP+":"+strPort+":"+strDBName; 94. 95. this.myConnection = DriverManager.getConnection(oracleUrlToConnect, strUser, strPassword); 96. 97. }catch(Exception ex){ 98. 99. System.out.println("Can not get connection:" + ex.getMessage()); 100. 101. System.out.println("请检测配置文件中的数据库信息是否正确." ); 102. 103. } 104. 105. return this.myConnection; 106. 107. } 108. 109.} 110. 111.2. 读取blob字段 112. 113. 114.在OracleQueryBean类中增加一个函数,来进行读取,具体代码如下: 115. 116./** 117. 118. * 根据图片在数据库中的ID进行读取 119. 120. * @param strID 图片字段ID 121. 122. * @param w 需要缩到的宽度 123. 124. * @param h 需要缩到高度 125. 126. * @return 127. 128. */ 129. 130. public byte[] GetImgByteById(String strID, int w, int h){ 131. 132. //System.out.println("Get img data which id is " + nID); 133. 134. if(myConnection == null) 135. 136. this.getConnection(); 137. 138. byte[] data = null; 139. 140. try { 141. 142. Statement stmt = myConnection.createStatement(); 143. 144. ResultSet myResultSet = stmt.executeQuery("select " + this.strIDName + " from " + this.strTabName + " where " + this.strIDName + "=" + strID); 145. 146. 147. 148. StringBuffer myStringBuffer = new StringBuffer(); 149. 150. if (myResultSet.next()) { 151. 152. java.sql.Blob blob = myResultSet.getBlob(this.strImgName); 153. 154. InputStream inStream = blob.getBinaryStream(); 155. 156. try { 157. 158. long nLen = blob.length(); 159. 160. int nSize = (int) nLen; 161. 162. //System.out.println("img data size is :" + nSize); 163. 164. data = new byte[nSize]; 165. 166. inStream.read(data); 167. 168. inStream.close(); 169. 170. } catch (IOException e) { 171. 172. System.out.println("获取图片数据失败,原因:" + e.getMessage()); 173. 174. } 175. 176. 177. 178. data = ChangeImgSize(data, w, h); 179. 180. } 181. 182. System.out.println(myStringBuffer.toString()); 183. 184. myConnection.commit(); 185. 186. myConnection.close(); 187. 188. } catch (SQLException ex) { 189. 190. System.out.println(ex.getMessage()); 191. 192. } 193. 194. return data; 195. 196.} 197. 198. 199. 200. 201. 202.3. 缩放图片 203.因为图片的大小可能不一致,但是在页面中输出的大小需要统一,所以需要 204. 205.在OracleQueryBean类中增加一个函数,来进行缩放,具体代码如下: 206. 207./** 208. 209. * 缩小或放大图片 210. 211. * @param data 图片的byte数据 212. 213. * @param w 需要缩到的宽度 214. 215. * @param h 需要缩到高度 216. 217. * @return 缩放后的图片的byte数据 218. 219. */ 220. 221. private byte[] ChangeImgSize(byte[] data, int nw, int nh){ 222. 223. byte[] newdata = null; 224. 225. try{ 226. 227. BufferedImage bis = ImageIO.read(new ByteArrayInputStream(data)); 228. 229. int w = bis.getWidth(); 230. 231. int h = bis.getHeight(); 232. 233. double sx = (double) nw / w; 234. 235. double sy = (double) nh / h; 236. 237. AffineTransform transform = new AffineTransform(); 238. 239. transform.setToScale(sx, sy); 240. 241. AffineTransformOp ato = new AffineTransformOp(transform, null); 242. 243. //原始颜色 244. 245. BufferedImage bid = new BufferedImage(nw, nh, BufferedImage.TYPE_3BYTE_BGR); 246. 247. ato.filter(bis, bid); 248. 249. 250. 251. //转换成byte字节 252. 253. ByteArrayOutputStream baos = new ByteArrayOutputStream(); 254. 255. ImageIO.write(bid, "jpeg", baos); 256. 257. newdata = baos.toByteArray(); 258. 259. 260. 261. }catch(IOException e){ 262. 263. e.printStackTrace(); 264. 265. } 266. 267. return newdata; 268. 269.} 270. 271. 272. 273.4. 展示在页面 274.页面使用OracleQueryBean来根据用户提供的图片id进行查询,在读取并进行缩放后,通过jsp页面进行展示,具体代码如下: 275. 276. 277. 278.<%@ page language="java" contentType="text/html;;charset=gbk" %> 279. 280.<jsp:useBean id="OrcleQuery" scope="page" class="HLFtiDemo.OracleQueryBean" /> 281. 282.<% 283. 284. response.setContentType("image/jpeg"); 285. 286. //图片在数据库中的 ID 287. 288. String strID = request.getParameter("id"); 289. 290. //要缩略或放大图片的宽度 291. 292. String strWidth = request.getParameter("w"); 293. 294. //要缩略或放大图片的高度 295. 296. String strHeight = request.getParameter("h"); 297. 298. byte[] data = null; 299. 300. if(strID != null){ 301. 302. int nWith = Integer.parseInt(strWidth); 303. 304. int nHeight = Integer.parseInt(strHeight); 305. 306. //获取图片的byte数据 307. 308. data = OrcleQuery.GetImgByteById(strID, nWith, nHeight); 309. 310. ServletOutputStream op = response.getOutputStream(); 311. 312. op.write(data, 0, data.length); 313. 314. op.close(); 315. 316. op = null; 317. 318. response.flushBuffer(); 319. 320. //清除输出流,防止释放时被捕获异常 321. 322. out.clear(); 323. 324. out = pageContext.pushBody(); 325. 326. } 327. 328.%> 329. 330. 331. 332. 333. 334.5. OracleQueryBean查询类的整体代码 335.OracleQueryBean.java文件代码如下所示: 336. 337.import java.sql.*; 338. 339.import java.io.*; 340. 341. 342. 343.import javax.imageio.ImageIO; 344. 345.import java.awt.image.BufferedImage; 346. 347.import java.awt.image.AffineTransformOp; 348. 349.import java.awt.geom.AffineTransform; 350. 351. 352. 353.public class OracleQueryBean { 354. 355. private final String oracleDriverName = "oracle.jdbc.driver.OracleDriver"; 356. 357. 358. 359. private Connection myConnection = null; 360. 361. 362. 363. /*图片表名*/ 364. 365. private String strTabName; 366. 367. /*图片ID字段名*/ 368. 369. private String strIDName; 370. 371. /*图片字段名*/ 372. 373. private String strImgName; 374. 375. /** 376. 377. * 加载java连接Oracle的jdbc驱动 378. 379. */ 380. 381. public OracleQueryBean(){ 382. 383. try{ 384. 385. Class.forName(oracleDriverName); 386. 387. }catch(ClassNotFoundException ex){ 388. 389. System.out.println("加载jdbc驱动失败,原因:" + ex.getMessage()); 390. 391. } 392. 393. } 394. 395. /** 396. 397. * 获取Oracle连接对象 398. 399. * @return Connection 400. 401. */ 402. 403. public Connection getConnection(){ 404. 405. try{ 406. 407. //用户名+密码; 以下使用的Test就是Oracle里的表空间 408. 409. //从配置文件中读取数据库信息 410. 411. GetPara oGetPara = new GetPara(); 412. 413. String strIP = oGetPara.getPara("serverip"); 414. 415. String strPort = oGetPara.getPara("port"); 416. 417. String strDBName = oGetPara.getPara("dbname"); 418. 419. String strUser = oGetPara.getPara("user"); 420. 421. String strPassword = oGetPara.getPara("password"); 422. 423. 424. 425. this.strTabName = oGetPara.getPara("tablename"); 426. 427. this.strIDName = oGetPara.getPara("imgidname"); 428. 429. this.strImgName = oGetPara.getPara("imgname"); 430. 431. 432. 433. String oracleUrlToConnect ="jdbc:oracle:thin:@"+strIP+":"+strPort+":"+strDBName; 434. 435. this.myConnection = DriverManager.getConnection(oracleUrlToConnect, strUser, strPassword); 436. 437. }catch(Exception ex){ 438. 439. System.out.println("Can not get connection:" + ex.getMessage()); 440. 441. System.out.println("请检测配置文件中的数据库信息是否正确." ); 442. 443. } 444. 445. return this.myConnection; 446. 447. } 448. 449. /** 450. 451. * 根据图片在数据库中的ID进行读取 452. 453. * @param strID 图片字段ID 454. 455. * @param w 需要缩到的宽度 456. 457. * @param h 需要缩到高度 458. 459. * @return 缩放后的图片的byte数据 460. 461. */ 462. 463. public byte[] GetImgByteById(String strID, int w, int h){ 464. 465. //System.out.println("Get img data which id is " + nID); 466. 467. if(myConnection == null) 468. 469. this.getConnection(); 470. 471. byte[] data = null; 472. 473. try { 474. 475. Statement stmt = myConnection.createStatement(); 476. 477. ResultSet myResultSet = stmt.executeQuery("select " + this.strIDName + " from " + this.strTabName + " where " + this.strIDName + "=" + strID); 478. 479. 480. 481. StringBuffer myStringBuffer = new StringBuffer(); 482. 483. if (myResultSet.next()) { 484. 485. java.sql.Blob blob = myResultSet.getBlob(this.strImgName); 486. 487. InputStream inStream = blob.getBinaryStream(); 488. 489. try { 490. 491. long nLen = blob.length(); 492. 493. int nSize = (int) nLen; 494. 495. //System.out.println("img data size is :" + nSize); 496. 497. data = new byte[nSize]; 498. 499. inStream.read(data); 500. 501. inStream.close(); 502. 503. } catch (IOException e) { 504. 505. System.out.println("获取图片数据失败,原因:" + e.getMessage()); 506. 507. } 508. 509. 510. 511. data = ChangeImgSize(data, w, h); 512. 513. } 514. 515. System.out.println(myStringBuffer.toString()); 516. 517. myConnection.commit(); 518. 519. myConnection.close(); 520. 521. } catch (SQLException ex) { 522. 523. System.out.println(ex.getMessage()); 524. 525. } 526. 527. return data; 528. 529. } 530. 531. 532. 533. /** 534. 535. * 根据图片在数据库中的ID进行读取,显示原始大小的图片 536. 537. * @param strID 图片字段ID 538. 539. * @return 读取后的图片byte数据 540. 541. */ 542. 543. public byte[] GetImgByteById(String strID){ 544. 545. //System.out.println("Get img data which id is " + nID); 546. 547. if(myConnection == null) 548. 549. this.getConnection(); 550. 551. byte[] data = null; 552. 553. try { 554. 555. Statement stmt = myConnection.createStatement(); 556. 557. ResultSet myResultSet = stmt.executeQuery("select " + this.strIDName + " from " + this.strTabName + " where " + this.strIDName + "=" + strID); 558. 559. 560. 561. StringBuffer myStringBuffer = new StringBuffer(); 562. 563. if (myResultSet.next()) { 564. 565. java.sql.Blob blob = myResultSet.getBlob(this.strImgName); 566. 567. InputStream inStream = blob.getBinaryStream(); 568. 569. try { 570. 571. long nLen = blob.length(); 572. 573. int nSize = (int) nLen; 574. 575. data = new byte[nSize]; 576. 577. inStream.read(data); 578. 579. inStream.close(); 580. 581. } catch (IOException e) { 582. 583. System.out.println("获取图片数据失败,原因:" + e.getMessage()); 584. 585. } 586. 587. } 588. 589. System.out.println(myStringBuffer.toString()); 590. 591. myConnection.commit(); 592. 593. myConnection.close(); 594. 595. } catch (SQLException ex) { 596. 597. System.out.println(ex.getMessage()); 598. 599. } 600. 601. return data; 602. 603. } 604. 605. 606. 607. /** 608. 609. * 缩小或放大图片 610. 611. * @param data 图片的byte数据 612. 613. * @param w 需要缩到的宽度 614. 615. * @param h 需要缩到高度 616. 617. * @return 618. 619. */ 620. 621. private byte[] ChangeImgSize(byte[] data, int nw, int nh){ 622. 623. byte[] newdata = null; 624. 625. try{ 626. 627. BufferedImage bis = ImageIO.read(new ByteArrayInputStream(data)); 628. 629. int w = bis.getWidth(); 630. 631. int h = bis.getHeight(); 632. 633. double sx = (double) nw / w; 634. 635. double sy = (double) nh / h; 636. 637. AffineTransform transform = new AffineTransform(); 638. 639. transform.setToScale(sx, sy); 640. 641. AffineTransformOp ato = new AffineTransformOp(transform, null); 642. 643. //原始颜色 644. 645. BufferedImage bid = new BufferedImage(nw, nh, BufferedImage.TYPE_3BYTE_BGR); 646. 647. ato.filter(bis, bid); 648. 649. //转换成byte字节 650. 651. ByteArrayOutputStream baos = new ByteArrayOutputStream(); 652. 653. ImageIO.write(bid, "jpeg", baos); 654. 655. newdata = baos.toByteArray(); 656. 657. }catch(IOException e){ 658. 659. e.printStackTrace(); 660. 661. } 662. 663. return newdata; 664. 665. } 666. 667.} 668. 669. 670. 671.本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/super_marioli/archive/2009/12/24/5069023.aspx
发表评论
-
远程调用servlet
2012-09-12 16:26 3959//发起请求、先写发送(out)、然后等待度(in) URL ... -
基于ssh的项目的单元测试
2012-08-01 13:55 956package com.csoft.ta.util; imp ... -
JBPM设计器乱码问题
2012-06-30 13:44 745修改eclipse安装跟目录下的eclipse.ini文件,在 ... -
项目中读取文件
2012-03-13 18:31 787import java.io.BufferedReader;i ... -
项目中配置log4j
2012-03-08 10:26 867<!--如果不定义webAppRootKey参数,那么w ... -
qq album
2012-01-05 23:02 1005此文章完全转自他人博客(用于个人学习记录) 这个功能的实现关 ... -
关于quartz 2.x 一些记录
2011-12-30 14:27 1170功能需要 审计(即对不同规则的定制的东西进行轮巡)如果每一个轮 ... -
JavaScript动态操作表格,添加,删除行、列及单元格
2011-12-27 16:11 764<html> <head> < ... -
jsp 导出excel(利用excel本身另存为html)
2011-12-27 16:06 4464<%@page contentType="ap ... -
jsp 导出excel
2011-11-21 16:09 867<%@page contentType="ap ... -
java mail 应用
2011-09-22 10:02 826以下全部copy from http://www.cnbl ... -
quartz 在 spring静态、动态配置
2011-09-20 14:55 1157项目要求动态改变配置时间,动态改变quartz的 cron表达 ... -
JMX简单示例
2011-09-02 16:40 877以下文章摘自网络仅供学习用 1.编写一个以MBean ... -
cron表达式
2011-08-01 16:59 809以下文章复制自他出: 一个Cron-表达式是一个由六至七个 ... -
不同数据库的数据库类型及对应的jdbc类型
2011-07-27 12:47 1178public static void main(String ... -
Dom4j的使用
2011-06-15 18:08 738Dom4j 使用简介 作 ... -
SVN update时候报错的解决办法。。Working copy not locked; this is probably a bug, please rep
2011-06-01 09:02 5120今天上午CVS服务器突然坏了。。改用svn,安装完插件,连到我 ... -
hibernate 延迟加载
2011-05-12 17:25 667延迟加载: 延迟加 ... -
hibernate 一级缓存和二级缓存
2011-05-12 17:09 867一级缓存 Hibernate框架一级缓存的特点: 1.它是hi ... -
James 一
2011-03-09 10:48 905James 邮件服务器的安装和配置 James 邮件服务 ...
相关推荐
有关Oracle对blob字段的操作读取方法,里面有详细的例子
java中读取ORACLE中BLOB字段存储的图片
可以从数据库中读取blob字段并插入到另一个表中,已经测试通过
delphi读写BLOB字段delphi读写BLOB字段delphi读写BLOB字段delphi读写BLOB字段delphi读写BLOB字段delphi读写BLOB字段delphi读写BLOB字段delphi读写BLOB字段delphi读写BLOB字段delphi读写BLOB字段delphi读写BLOB字段...
Hibernate读取blob字段
只需要安装oracle client在本地,然后配置好tns,输入连接名,用户名,密码,数据表的名称,文件名数据列名以及blob数据列名,就可以批量导出ORACLE数据库BLOB字段生成图片
kettle通过java代码将数据库blob 字段抽取到本地文件
基于JSP访问ORACLE数据库BLOB字段并显示图形的解决方案 基于JSP访问ORACLE数据库BLOB字段并显示图形的解决方案.pdf文档介绍了一种使用JSP技术访问ORACLE数据库BLOB字段并显示图形的方法。该方法使用JSP访问ORACLE...
C#,BLOB读取操作
java读取大字段,介绍java如何读取大字段blob,解决程序员头疼问题
spring+mybatis下BLOB字段的图片存取代码,仅整理了Controller层的代码,service和dao的代码很简单,所以没有整理
C++ Builder XE 读取sqlite图片Blob字段的多种存取方法: 利用TMemoryStream *mem=new TMemoryStream; TBlobField *blob_pic=(TBlobField *)Form1->FDQuery1->Fields->FieldByName("image"); blob_pic->...
java,Blob字段操作,将图片或者文件保存到数据库中.zip
oracle中对blob字段的处理方法,包括增加,删除,修改,更新等操作。
使用PLSQL从数据库中读取BLOB对象
Oracle blob字段上传下载 Oracle blob字段上传下载Oracle blob字段上传下载 Oracle blob字段上传下载
oracle查询blob字段照片(jpeg) 像素大小sql
C#访问oracle数据库类 1、执行非查询命令 2、执行查询命令 3、带blob字段读写
mysql导出工具,可导出无注释插入语句,表中字段有blob类数据,插入语句前还有锁表操作语句,适合多种场景
该程序可将ORACLE中BLOB字段导入到SQL SERVER中的IMAGE字段,方便快捷。