08/03/13
BLOB Image Storage with Spring Hibernate and Struts2
Sitemap search skip to content
CiphorBlog
dev life buzz
Home buzz dev life
BLOB Image Storage with Spring Hibernate and Struts2
by kangleay on Jun.11, 2010, under dev In Todays post I wanna talk about storage of images in database with BLOB(Binary Large Object) data type. Though I am still considering the performance issues on BLOB images, yet I am going to share the steps I followed to implement the process. You still have no idea what the BLOB is? its all right.
What is BLOB?
A blob (alternately known as a binary large object, basic large object, BLOB, or BLOb) is a collection of binary data stored as a single entity in a database management system. Blobs are typically images, audio or other multimedia objects, though sometimes binary executable code is stored as a blob. Database support for blobs is not universal. - From Wikipedia. We often store unstructured data into a LOB field in database, and LOB is normally known as BLOB(Binary) and CLOB(Character). The LOB datatypes BLOB, CLOB, NCLOB, and BFILE enable you to store large blocks of unstructured data (such as text, graphic images, video clips, and sound waveforms) up to 4 gigabytes in size. They provide efficient, random, piece-wise access to the data. Oracle Corporation recommends that you always use LOB datatypes over LONG datatypes.- Oracle Docs.
Steps I followed
DefaultLobHandler
First we need to add spring Blob handler into configuration xml. Declare a bean definition and inject it into sessionFactory. 0 <- Srn DfutLBHnlr-> 1 !- pig eal O ade 0 2 <eni=lbade" ba d"oHnlr cas"r.pigrmwr.dcspotlbDfutoHnlr lz-nt"re / ls=ogsrnfaeokjb.upr.o.ealLbade" ayii=tu" > 0 3 0 4 <- HbraeSsinatr -> !- ient esoFcoy 0 5 <eni=ssinatr" ba d"esoFcoy cas"r.pigrmwr.r.ient3antto.noainesoFcoyen> ls=ogsrnfaeokomhbrae.noainAnttoSsinatrBa" 0 6 <rprynm=dtSuc"rf"aaore/ poet ae"aaore e=dtSuc"> 0 7 <rprynm=cniLcto"vle"lspt:ient.f.m"> poet ae"ofgoain au=casahhbraecgxl/ 0 8 <rprynm=hbraerpris> poet ae"ientPoete" 0 9 <au> vle 1 0 hbraedaet$hbraedaet ient.ilc={ient.ilc} 1 1 hbraeqeysbtttostu '' fle'' ient.ur.usiuin=re Y, as N 1 2 hbraecceuescn_ee_ah=re ient.ah.s_eodlvlccetu 1 3 hbraeso_q=re ient.hwsltu 1 4 hbraeccepoie_ls=r.ient.ah.hahPoie ient.ah.rvdrcasoghbraecceECcervdr 1 5 <vle /au> 1 6 <- Tr bthn offrbte errmsae udrPsgeQ -> !- un acig f o etr ro esgs ne otrSL 1 7 <- hbraejb.ac_ie0-> !- ient.dcbthsz= 1 8 <poet> /rpry 1 9 <rprynm=lbade"rf"oHnlr/ poet ae"oHnlr e=lbade">
www.ciphor.com/blog/blob-image-storage-with-spring-hibernate-and-struts2/ 1/6
08/03/13
BLOB Image Storage with Spring Hibernate and Struts2
2 0
<ba> /en
Data Type in Model
And then map the corresponding field of table to a Byte[] property in the model class. 0 piaebt[ iaeaa 1 rvt ye] mgDt; 0 2 0 @ounnm=IAEDT" 3 Clm(ae"MG_AA) 0 4 pbi bt[ gtmgDt( { ulc ye] eIaeaa) 0 5 rtr ti.mgDt; eun hsiaeaa 0 6 } 0 7 0 8 pbi vi stmgDt(ye]iaeaa { ulc od eIaeaabt[ mgDt) 0 9 ti.mgDt =iaeaa hsiaeaa mgDt; 1 0 }
Show Image in Struts Action
In struts, write a separate action for retrieving the image data. As to the action method, it is like the code below: 0 pbi vi gtyaiIae){ 1 ulc od eDnmcmg( 0 2 HtSrltepnersos =nl; tpeveRsos epne ul 0 3 Srltupttemot=nl; eveOtuSra u ul 0 4 ty{ r 0 5 rsos =Srltcinotx.eRsos(; epne eveAtoCnetgtepne) 0 6 rsos.eCnetye"utpr/omdt"; epnestotnTp(mliatfr-aa) 0 7 ot=rsos.eOtuSra(; u epnegtupttem) 0 8 FWrIae fWrIae=fWrIaeMngrgtiaed; saemgs saemg saemgsaae.e(mgI) 0 9 otwiefWrIaegtmgDt() u.rt(saemg.eIaeaa); 1 0 otfuh) u.ls(; 1 1 }cth(Oxeto e { ac IEcpin ) 1 2 epittcTae) .rnSakrc(; 1 3 }cth(xeto e { ac Ecpin ) 1 4 epittcTae) .rnSakrc(; 1 5 }fnly{ ial 1 6 i (u ! nl) f ot = ul 1 7 ty{ r 1 8 otcoe) u.ls(; 1 9 }cth(Oxeto e { ac IEcpin ) 2 0 epittcTae) .rnSakrc(; 2 1 } 2 2 } 2 } 3
Uploading with Struts
For uploading images using struts2 upload machanism you are supposed to follow the rules like: 0 pbi Fl[ gtmg( { 1 ulc ie] eIae) 0 2 rtr iae eun mg; 0 3 } 0 4 0 5 pbi vi stmg(ie]iae { ulc od eIaeFl[ mg) 0 6 ti.mg =iae hsiae mg; 0 7 } 0 8 0 9 pbi Srn[ gtmgCnetye){ ulc tig] eIaeotnTp( 1 0 rtr iaeotnTp; eun mgCnetye 1 1 } 1 2 1 3 pbi vi stmgCnetyeSrn[ iaeotnTp){ ulc od eIaeotnTp(tig] mgCnetye 1 4 ti.mgCnetye=iaeotnTp; hsiaeotnTp mgCnetye 1 5 } 1 6 1 7 pbi Srn[ gtmgFlNm( { ulc tig] eIaeieae) 1 8 rtr iaeieae eun mgFlNm;
www.ciphor.com/blog/blob-image-storage-with-spring-hibernate-and-struts2/ 2/6
08/03/13
BLOB Image Storage with Spring Hibernate and Struts2
1 9 2 0 2 1 2 2 2 3
} pbi vi stmgFlNm(tig]iaeieae { ulc od eIaeieaeSrn[ mgFlNm) ti.mgFlNm =iaeieae hsiaeieae mgFlNm; }
In spring service you need handle the struts input with springs FileCopyUtils class: 0 @rnatoa(edny fle 1 TascinlraOl= as) 0 2 pbi vi svFWrIf(saenofWrIf,Fl[ iae ulc od aesaenoFWrIf saeno ie] mg, 0 3 Srn[ iaeotnTp,Srn[ iaeieae tig] mgCnetye tig] mgFlNm) 0 4 trw IEcpin{ hos Oxeto 0 5 0 6 i (mg.egh! 0| iae! nl){ f iaelnt = | mg = ul 0 7 fr(n i=0 i<iaelnt;i+ { o it ; mg.egh +) 0 8 bt[ flAry=ogsrnfaeokui.ieoytl ye] iera r.pigrmwr.tlFlCpUis 0 9 .oyoyera(mg[]; cpTBtAryiaei) 1 0 FWrIae fWrIae=nwFWrIae(; saemgs saemg e saemgs) 1 1 fWrIaestmgDt(iera) saemg.eIaeaaflAry; 1 2 fWrIaestmgSz(e Ln(iera.egh) saemg.eIaeienw ogflArylnt); 1 3 fWrIaestmgTp(mgCnetyei) saemg.eIaeyeiaeotnTp[]; 1 4 fWrIaestaedfWrIf.eI() saemg.eWrI(saenogtd); 1 5 fWrIf.eFWrIae(.d(saemg) saenogtsaemgs)adfWrIae; 1 6 } 1 7 } 1 8 1 9 fWrIf.eIaeu(e Ln(saenogtsaemgs)sz(); saenostmgNmnw ogfWrIf.eFWrIae(.ie)) 2 0 2 1 sprsv(saeno; ue.aefWrIf) 2 2 }
Finally in JSP
Here goes what the code is like in jsp. 1 <nu tp=fl"i=iae vle" nm=iae> ipt ye"ie d"mg" au=" ae"mg" 2 <m sc"sulato=gtyaiIae> ig r=<:r cin"eDnmcmg" 3 <:aa nm=iaeD vle"d>/:aa> sprm ae"mgI" au=i"<sprm 4 <sul" /:r>>
Demo Snapshot
Here shows a demo snapshot from my previous project with ExtJS organizing the images from database Blobs.
Related posts: 1. How to Build a Dynamic Tree with Dojo,DWR,Spring and Hibernate 2. How to integrate Spring HttpInvoker with Java Applet
www.ciphor.com/blog/blob-image-storage-with-spring-hibernate-and-struts2/ 3/6
08/03/13
BLOB Image Storage with Spring Hibernate and Struts2
3. How to Build Dynamic Tree with ExtJS 4. Howto Enable Lazy Fetch using Hibernate with Spring | 5. Composite Primary Key Mapping with JPA Annotation :applicationContext, blob, hibernate, image, java, spring, struts, struts2, web
3 Comments for this entry
medical assistant
June 25th, 2010 on 9:51 pm
found your site on del.icio.us today and really liked it.. i bookmarked it and will be back to check it out some more later [Reply] Reply
WP Themes
July 4th, 2010 on 5:45 pm
Genial fill someone in on and this fill someone in on helped me alot in my college assignement. Thanks you on your information. [Reply] Reply
Louis
September 14th, 2010 on 2:08 pm
Your site is amazing.I am very impressed to see this,i want to come back for visiting your site.Keep doing Good as well as you can.. [Reply] Reply
3 Trackbacks / Pingbacks for this entry
kangleay
June 11th, 2010 on 8:55 am
BLOB Image Storage with Spring Hibernate and Struts2 http://goo.gl/fb/Asxe1 Johannes Geppert
June 13th, 2010 on 5:25 pm
RT @kangleay: BLOB Image Storage with Spring Hibernate and Struts2 http://goo.gl/fb/Asxe1 Tweets that mention BLOB Image Storage with Spring Hibernate and Struts2 -- Topsy.com
June 14th, 2010 on 1:37 am
[...] This post was mentioned on Twitter by Johannes Geppert, kangleay. kangleay said: BLOB Image Storage with Spring Hibernate and Struts2 http://goo.gl/fb/Asxe1 [...]
Leave a Reply
www.ciphor.com/blog/blob-image-storage-with-spring-hibernate-and-struts2/ 4/6
08/03/13
BLOB Image Storage with Spring Hibernate and Struts2
Name (required) Mail (will not be published) (required) Website
Leave comment
RSS feed for this post (comments)
Hi! Welcome to CiphorBlog!
Thanks for dropping by! Feel free to join the discussion by leaving comments, and stay updated by subscribing to the RSS feed. See ya around!
Where are we?
CiphorWiki
0
Subscribe in a reader
Tag Cloud
buzz code database dbconsole dev dwr eclipse error extjs firefox firefox plugins good man google hardy heron hibernate html install internet ipod touch java javascript junit life tips linux mac mocking network office oracle pdf personal plugins remote spring struts2 technology test tree treepanel ubuntu unit test unix video web
Agile
Recent Posts
ExtJS Tree Recursive Checking Tweak Java Home in Mac for Oracle Habits Subclipse with Eclipse Helios on Snow Leopard Resolve Error Installing MySql Gem in Snow Leopard BLOB Image Storage with Spring Hibernate and Struts2
Recent Comments
kangleay on ExtJS Tree Recursive Checking treelovers Treehuggers on How to Build Dynamic Tree with ExtJS kangleay on Subclipse with Eclipse Helios on Snow Leopard gabri on Subclipse with Eclipse Helios on Snow Leopard Louis on BLOB Image Storage with Spring Hibernate and Struts2
Categories
buzz
www.ciphor.com/blog/blob-image-storage-with-spring-hibernate-and-struts2/ 5/6
08/03/13
BLOB Image Storage with Spring Hibernate and Struts2
dev life
Meta
Log in Valid XHTML
Looking for something?
Use the form below to search the site:
Search keywords Find it
Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!
Blogroll
A few highly recommended websites...
Archives
All entries, chronologically... December 2010 August 2010 July 2010 June 2010 April 2010 March 2009 February 2009 January 2009 November 2008 October 2008 September 2008 August 2008 Powered by WordPress. Theme: Pixel. Sweet icons by famfamfam. Back to top
Bad Behavior has blocked 31 access attempts in the last 7 days.
www.ciphor.com/blog/blob-image-storage-with-spring-hibernate-and-struts2/
6/6