java - Extract Blob value and pass to Controller in Spring MVC (without using hibernate) -
is there way extract blob file (an oracle) db , pass controller in spring mvc without using hibernate ?
the following table structure:
desc project_storage; project_id number(38) file_name varchar2(20) documents blob alias varchar2(50) file_type varchar2(50)
my code of follows :
@requestmapping(value = "/downloadfile.htm", method = requestmethod.get) @responsebody public void downloadfile(modelmap model,httpservletrequest request,httpservletresponse response,@requestparam map<string,string> parameters) { object objarray2[]=new objec map<string,object> file=user.getbyid(objarray2, "select * project_storage project_id = 63 "); log.info("the record retrieved : "+file); //the record retrieved : {project_id=63, file_name=mat.png, documents=[b@1edef76, alias=null, file_type=image/png} try {file file2=(file)file.get("documents"); //classcastexception occurs here response.setheader("content-disposition", "attachment;filename=\"test\""); response.setcontenttype("image/*"); response.setheader("cache-control", "cache, must-revalidate"); response.setheader("cache-control", "no-cache"); response.setheader("pragma", "public"); response.setheader("content-transfer-encoding", "binary"); fileinputstream inputfile = new fileinputstream(file2); system.out.println("the file : "+file2.tostring()); objectinputstream inputostream = new objectinputstream(inputfile); //byte barray[]= filecopyutils.copy(inputostream, response.getoutputstream()); // imageio.write(bi, "png", out); } catch (exception e) { e.printstacktrace(); } }
stacktrace :
[6/25/15 17:52:53:824 ist] 00000036 systemout o dbutility || getrow || query : select * project_storage project_id = 63 || object : [ljava.lang.object;@20f2141 [6/25/15 17:52:53:893 ist] 00000036 systemerr r java.lang.classcastexception: [b incompatible java.io.file [6/25/15 17:52:53:906 ist] 00000036 systemerr r @ org.game.prev.gamer.controller.admincontroller.downloadfile(admincontroller.java:199) [6/25/15 17:52:53:906 ist] 00000036 systemerr r @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) [6/25/15 17:52:53:906 ist] 00000036 systemerr r @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:60) [6/25/15 17:52:53:906 ist] 00000036 systemerr r @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:37) [6/25/15 17:52:53:911 ist] 00000036 systemerr r @ java.lang.reflect.method.invoke(method.java:611) [6/25/15 17:52:53:912 ist] 00000036 systemerr r @ org.springframework.web.method.support.invocablehandlermethod.invoke(invocablehandlermethod.java:213) [6/25/15 17:52:53:912 ist] 00000036 systemerr r @ org.springframework.web.method.support.invocablehandlermethod.invokeforrequest(invocablehandlermethod.java:126) [6/25/15 17:52:53:912 ist] 00000036 systemerr r @ org.springframework.web.servlet.mvc.method.annotation.servletinvocablehandlermethod.invokeandhandle(servletinvocablehandlermethod.java:96) [6/25/15 17:52:53:918 ist] 00000036 systemerr r @ org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter.invokehandlermethod(requestmappinghandleradapter.java:617) [6/25/15 17:52:53:918 ist] 00000036 systemerr r @ org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter.handleinternal(requestmappinghandleradapter.java:578) [6/25/15 17:52:53:918 ist] 00000036 systemerr r @ org.springframework.web.servlet.mvc.method.abstracthandlermethodadapter.handle(abstracthandlermethodadapter.java:80) [6/25/15 17:52:53:919 ist] 00000036 systemerr r @ org.springframework.web.servlet.dispatcherservlet.dodispatch(dispatcherservlet.java:923) [6/25/15 17:52:53:925 ist] 00000036 systemerr r @ org.springframework.web.servlet.dispatcherservlet.doservice(dispatcherservlet.java:852) [6/25/15 17:52:53:925 ist] 00000036 systemerr r @ org.springframework.web.servlet.frameworkservlet.processrequest(frameworkservlet.java:882) [6/25/15 17:52:53:925 ist] 00000036 systemerr r @ org.springframework.web.servlet.frameworkservlet.doget(frameworkservlet.java:778) [6/25/15 17:52:53:925 ist] 00000036 systemerr r @ javax.servlet.http.httpservlet.service(httpservlet.java:575) [6/25/15 17:52:53:925 ist] 00000036 systemerr r @ javax.servlet.http.httpservlet.service(httpservlet.java:668) [6/25/15 17:52:53:925 ist] 00000036 systemerr r @ com.game.ws.webcontainer.servlet.servletwrapper.service(servletwrapper.java:1214) [6/25/15 17:52:53:925 ist] 00000036 systemerr r @ com.game.ws.webcontainer.servlet.servletwrapper.handlerequest(servletwrapper.java:774) [6/25/15 17:52:53:925 ist] 00000036 systemerr r @ com.game.ws.webcontainer.servlet.servletwrapper.handlerequest(servletwrapper.java:456) [6/25/15 17:52:53:925 ist] 00000036 systemerr r @ com.game.ws.webcontainer.servlet.servletwrapperimpl.handlerequest(servletwrapperimpl.java:178) [6/25/15 17:52:53:925 ist] 00000036 systemerr r @ com.game.ws.webcontainer.filter.webappfilterchain.invoketarget(webappfilterchain.java:125) [6/25/15 17:52:53:926 ist] 00000036 systemerr r @ com.game.ws.webcontainer.filter.webappfilterchain.dofilter(webappfilterchain.java:92) [6/25/15 17:52:53:926 ist] 00000036 systemerr r @ org.game.prev.gamer.filters.sessionfilter.dofilter(sessionfilter.java:104) [6/25/15 17:52:53:926 ist] 00000036 systemerr r @ com.game.ws.webcontainer.filter.filterinstancewrapper.dofilter(filterinstancewrapper.java:192) [6/25/15 17:52:53:926 ist] 00000036 systemerr r @ com.game.ws.webcontainer.filter.webappfilterchain.dofilter(webappfilterchain.java:89) [6/25/15 17:52:53:926 ist] 00000036 systemerr r @ com.game.ws.webcontainer.filter.webappfiltermanager.dofilter(webappfiltermanager.java:926) [6/25/15 17:52:53:926 ist] 00000036 systemerr r @ com.game.ws.webcontainer.filter.webappfiltermanager.invokefilters(webappfiltermanager.java:1023) [6/25/15 17:52:53:926 ist] 00000036 systemerr r @ com.game.ws.webcontainer.webapp.webapp.handlerequest(webapp.java:3703) [6/25/15 17:52:53:926 ist] 00000036 systemerr r @ com.game.ws.webcontainer.webapp.webgroup.handlerequest(webgroup.java:304) [6/25/15 17:52:53:926 ist] 00000036 systemerr r @ com.game.ws.webcontainer.webcontainer.handlerequest(webcontainer.java:962) [6/25/15 17:52:53:926 ist] 00000036 systemerr r @ com.game.ws.webcontainer.wswebcontainer.handlerequest(wswebcontainer.java:1662) [6/25/15 17:52:53:926 ist] 00000036 systemerr r @ com.game.ws.webcontainer.channel.wcchannellink.ready(wcchannellink.java:195) [6/25/15 17:52:53:926 ist] 00000036 systemerr r @ com.game.ws.http.channel.inbound.impl.httpinboundlink.handlediscrimination(httpinboundlink.java:452) [6/25/15 17:52:53:927 ist] 00000036 systemerr r @ com.game.ws.http.channel.inbound.impl.httpinboundlink.handlenewrequest(httpinboundlink.java:511) [6/25/15 17:52:53:927 ist] 00000036 systemerr r @ com.game.ws.http.channel.inbound.impl.httpinboundlink.processrequest(httpinboundlink.java:305) [6/25/15 17:52:53:928 ist] 00000036 systemerr r @ com.game.ws.http.channel.inbound.impl.httpiclreadcallback.complete(httpiclreadcallback.java:83) [6/25/15 17:52:53:928 ist] 00000036 systemerr r @ com.game.ws.tcp.channel.impl.aioreadcompletionlistener.futurecompleted(aioreadcompletionlistener.java:165) [6/25/15 17:52:53:928 ist] 00000036 systemerr r @ com.game.io.async.abstractasyncfuture.invokecallback(abstractasyncfuture.java:217) [6/25/15 17:52:53:928 ist] 00000036 systemerr r @ com.game.io.async.asyncchannelfuture.firecompletionactions(asyncchannelfuture.java:161) [6/25/15 17:52:53:928 ist] 00000036 systemerr r @ com.game.io.async.asyncfuture.completed(asyncfuture.java:138) [6/25/15 17:52:53:928 ist] 00000036 systemerr r @ com.game.io.async.resulthandler.complete(resulthandler.java:204) [6/25/15 17:52:53:928 ist] 00000036 systemerr r @ com.game.io.async.resulthandler.runeventprocessingloop(resulthandler.java:775) [6/25/15 17:52:53:928 ist] 00000036 systemerr r @ com.game.io.async.resulthandler$2.run(resulthandler.java:905) [6/25/15 17:52:53:928 ist] 00000036 systemerr r @ com.game.ws.util.threadpool$worker.run(threadpool.java:1659)
the log file after execution of select statement shows :
[info]-the record retrieved : {project_id=63, file_name=mat.png, documents=[b@1edef76, alias=null, file_type=image/png}
this shows value being fetched blob field "documents", not able retrieve properly.
i tried section of code, leads /downloadfile.htm page, got 0 byte file downloaded output :
@requestmapping(value = "/downloadfile.htm", method = requestmethod.get) @responsebody public void downloadfile(modelmap model,httpservletrequest request,httpservletresponse response,@requestparam map<string,string> parameters) { object objarray2[]=new object[]{}; map<string,object> file=user.getbyid(objarray2, "select * project_storage project_id = 63 "); object file2=file.get("documents"); //previously file file2=(file)file.get("documents"); response.setheader("content-disposition", "attachment;filename=\"test\""); response.setcontenttype("image/*"); response.setheader("cache-control", "cache, must-revalidate"); response.setheader("cache-control", "no-cache"); response.setheader("pragma", "public"); response.setheader("content-transfer-encoding", "binary"); try { fileinputstream inputfile = new fileinputstream(file2.tostring()); //previously fileinputstream inputfile = new fileinputstream(file2); filenotfound exception occurs here system.out.println("the file : "+file2.tostring()); objectinputstream inputostream = new objectinputstream(inputfile); filecopyutils.copy(inputostream, response.getoutputstream()); } catch (exception e) { e.printstacktrace(); } } gives filenotfound exception. here stacktrace : [6/25/15 18:06:39:825 ist] 00000037 systemerr r java.io.filenotfoundexception: [b@22b6d45 (the system cannot find file specified.) [6/25/15 18:06:39:826 ist] 00000037 systemerr r @ java.io.fileinputstream.open(native method) [6/25/15 18:06:39:826 ist] 00000037 systemerr r @ java.io.fileinputstream.<init>(fileinputstream.java:123) [6/25/15 18:06:39:826 ist] 00000037 systemerr r @ java.io.fileinputstream.<init>(fileinputstream.java:83) [6/25/15 18:06:39:826 ist] 00000037 systemerr r @ org.game.prev.gamer.controller.admincontroller.downloadfile(admincontroller.java:210) [6/25/15 18:06:39:826 ist] 00000037 systemerr r @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) [6/25/15 18:06:39:826 ist] 00000037 systemerr r @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:60) [6/25/15 18:06:39:827 ist] 00000037 systemerr r @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:37) [6/25/15 18:06:39:827 ist] 00000037 systemerr r @ java.lang.reflect.method.invoke(method.java:611) [6/25/15 18:06:39:827 ist] 00000037 systemerr r @ org.springframework.web.method.support.invocablehandlermethod.invoke(invocablehandlermethod.java:213) [6/25/15 18:06:39:827 ist] 00000037 systemerr r @ org.springframework.web.method.support.invocablehandlermethod.invokeforrequest(invocablehandlermethod.java:126) [6/25/15 18:06:39:827 ist] 00000037 systemerr r @ org.springframework.web.servlet.mvc.method.annotation.servletinvocablehandlermethod.invokeandhandle(servletinvocablehandlermethod.java:96) [6/25/15 18:06:39:827 ist] 00000037 systemerr r @ org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter.invokehandlermethod(requestmappinghandleradapter.java:617) [6/25/15 18:06:39:827 ist] 00000037 systemerr r @ org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter.handleinternal(requestmappinghandleradapter.java:578) [6/25/15 18:06:39:827 ist] 00000037 systemerr r @ org.springframework.web.servlet.mvc.method.abstracthandlermethodadapter.handle(abstracthandlermethodadapter.java:80) [6/25/15 18:06:39:827 ist] 00000037 systemerr r @ org.springframework.web.servlet.dispatcherservlet.dodispatch(dispatcherservlet.java:923) [6/25/15 18:06:39:827 ist] 00000037 systemerr r @ org.springframework.web.servlet.dispatcherservlet.doservice(dispatcherservlet.java:852) [6/25/15 18:06:39:827 ist] 00000037 systemerr r @ org.springframework.web.servlet.frameworkservlet.processrequest(frameworkservlet.java:882) [6/25/15 18:06:39:828 ist] 00000037 systemerr r @ org.springframework.web.servlet.frameworkservlet.doget(frameworkservlet.java:778) [6/25/15 18:06:39:828 ist] 00000037 systemerr r @ javax.servlet.http.httpservlet.service(httpservlet.java:575) [6/25/15 18:06:39:828 ist] 00000037 systemerr r @ javax.servlet.http.httpservlet.service(httpservlet.java:668) [6/25/15 18:06:39:828 ist] 00000037 systemerr r @ com.game.ws.webcontainer.servlet.servletwrapper.service(servletwrapper.java:1214) [6/25/15 18:06:39:828 ist] 00000037 systemerr r @ com.game.ws.webcontainer.servlet.servletwrapper.handlerequest(servletwrapper.java:774) [6/25/15 18:06:39:828 ist] 00000037 systemerr r @ com.game.ws.webcontainer.servlet.servletwrapper.handlerequest(servletwrapper.java:456) [6/25/15 18:06:39:828 ist] 00000037 systemerr r @ com.game.ws.webcontainer.servlet.servletwrapperimpl.handlerequest(servletwrapperimpl.java:178) [6/25/15 18:06:39:828 ist] 00000037 systemerr r @ com.game.ws.webcontainer.filter.webappfilterchain.invoketarget(webappfilterchain.java:125) [6/25/15 18:06:39:828 ist] 00000037 systemerr r @ com.game.ws.webcontainer.filter.webappfilterchain.dofilter(webappfilterchain.java:92) [6/25/15 18:06:39:828 ist] 00000037 systemerr r @ org.game.prev.gamer.filters.sessionfilter.dofilter(sessionfilter.java:104) [6/25/15 18:06:39:828 ist] 00000037 systemerr r @ com.game.ws.webcontainer.filter.filterinstancewrapper.dofilter(filterinstancewrapper.java:192) [6/25/15 18:06:39:829 ist] 00000037 systemerr r @ com.game.ws.webcontainer.filter.webappfilterchain.dofilter(webappfilterchain.java:89) [6/25/15 18:06:39:829 ist] 00000037 systemerr r @ com.game.ws.webcontainer.filter.webappfiltermanager.dofilter(webappfiltermanager.java:926) [6/25/15 18:06:39:829 ist] 00000037 systemerr r @ com.game.ws.webcontainer.filter.webappfiltermanager.invokefilters(webappfiltermanager.java:1023) [6/25/15 18:06:39:829 ist] 00000037 systemerr r @ com.game.ws.webcontainer.webapp.webapp.handlerequest(webapp.java:3703) [6/25/15 18:06:39:829 ist] 00000037 systemerr r @ com.game.ws.webcontainer.webapp.webgroup.handlerequest(webgroup.java:304) [6/25/15 18:06:39:829 ist] 00000037 systemerr r @ com.game.ws.webcontainer.webcontainer.handlerequest(webcontainer.java:962) [6/25/15 18:06:39:829 ist] 00000037 systemerr r @ com.game.ws.webcontainer.wswebcontainer.handlerequest(wswebcontainer.java:1662) [6/25/15 18:06:39:829 ist] 00000037 systemerr r @ com.game.ws.webcontainer.channel.wcchannellink.ready(wcchannellink.java:195) [6/25/15 18:06:39:829 ist] 00000037 systemerr r @ com.game.ws.http.channel.inbound.impl.httpinboundlink.handlediscrimination(httpinboundlink.java:452) [6/25/15 18:06:39:829 ist] 00000037 systemerr r @ com.game.ws.http.channel.inbound.impl.httpinboundlink.handlenewrequest(httpinboundlink.java:511) [6/25/15 18:06:39:830 ist] 00000037 systemerr r @ com.game.ws.http.channel.inbound.impl.httpinboundlink.processrequest(httpinboundlink.java:305) [6/25/15 18:06:39:830 ist] 00000037 systemerr r @ com.game.ws.http.channel.inbound.impl.httpiclreadcallback.complete(httpiclreadcallback.java:83) [6/25/15 18:06:39:830 ist] 00000037 systemerr r @ com.game.ws.tcp.channel.impl.aioreadcompletionlistener.futurecompleted(aioreadcompletionlistener.java:165) [6/25/15 18:06:39:830 ist] 00000037 systemerr r @ com.game.io.async.abstractasyncfuture.invokecallback(abstractasyncfuture.java:217) [6/25/15 18:06:39:830 ist] 00000037 systemerr r @ com.game.io.async.asyncchannelfuture.firecompletionactions(asyncchannelfuture.java:161) [6/25/15 18:06:39:830 ist] 00000037 systemerr r @ com.game.io.async.asyncfuture.completed(asyncfuture.java:138) [6/25/15 18:06:39:830 ist] 00000037 systemerr r @ com.game.io.async.resulthandler.complete(resulthandler.java:204) [6/25/15 18:06:39:830 ist] 00000037 systemerr r @ com.game.io.async.resulthandler.runeventprocessingloop(resulthandler.java:775) [6/25/15 18:06:39:830 ist] 00000037 systemerr r @ com.game.io.async.resulthandler$2.run(resulthandler.java:905) [6/25/15 18:06:39:830 ist] 00000037 systemerr r @ com.game.ws.util.threadpool$worker.run(threadpool.java:1659)
i believe there has way around this.. not getting correct datatype/method correctly extract blob file. please help.
file2
byte array, not file. that's why classcastexception. calling tostring() on byte array won't lead useful. cast file2 byte[], , write byte array response output stream.
Comments
Post a Comment