简体   繁体   中英

Extract Blob value and pass to Controller in Spring MVC (without using hibernate)

Is there any way to extract a blob file from a (an Oracle) DB and pass it to the Controller in spring MVC without using hibernate ?

The following is my table structure:

desc PROJECT_STORAGE;
PROJECT_ID      NUMBER(38)   
FILE_NAME       VARCHAR2(20) 
DOCUMENTS       BLOB         
ALIAS           VARCHAR2(50) 
FILE_TYPE       VARCHAR2(50)

My code as of now is as 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 * from project_storage  WHERE PROJECT_ID = 63 ");
           log.info("The record retrieved is : "+file);   //The record retrieved is : {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 is : "+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 * from project_storage  WHERE PROJECT_ID = 63 || Object : [Ljava.lang.Object;@20f2141
[6/25/15 17:52:53:893 IST] 00000036 SystemErr     R java.lang.ClassCastException: [B incompatible with java.io.File
[6/25/15 17:52:53:906 IST] 00000036 SystemErr     R     at org.game.prev.gamer.controller.AdminController.downloadFile(AdminController.java:199)
[6/25/15 17:52:53:906 IST] 00000036 SystemErr     R     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[6/25/15 17:52:53:906 IST] 00000036 SystemErr     R     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
[6/25/15 17:52:53:906 IST] 00000036 SystemErr     R     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
[6/25/15 17:52:53:911 IST] 00000036 SystemErr     R     at java.lang.reflect.Method.invoke(Method.java:611)
[6/25/15 17:52:53:912 IST] 00000036 SystemErr     R     at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
[6/25/15 17:52:53:912 IST] 00000036 SystemErr     R     at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
[6/25/15 17:52:53:912 IST] 00000036 SystemErr     R     at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
[6/25/15 17:52:53:918 IST] 00000036 SystemErr     R     at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
[6/25/15 17:52:53:918 IST] 00000036 SystemErr     R     at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
[6/25/15 17:52:53:918 IST] 00000036 SystemErr     R     at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
[6/25/15 17:52:53:919 IST] 00000036 SystemErr     R     at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
[6/25/15 17:52:53:925 IST] 00000036 SystemErr     R     at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
[6/25/15 17:52:53:925 IST] 00000036 SystemErr     R     at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
[6/25/15 17:52:53:925 IST] 00000036 SystemErr     R     at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
[6/25/15 17:52:53:925 IST] 00000036 SystemErr     R     at javax.servlet.http.HttpServlet.service(HttpServlet.java:575)
[6/25/15 17:52:53:925 IST] 00000036 SystemErr     R     at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
[6/25/15 17:52:53:925 IST] 00000036 SystemErr     R     at com.game.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1214)
[6/25/15 17:52:53:925 IST] 00000036 SystemErr     R     at com.game.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:774)
[6/25/15 17:52:53:925 IST] 00000036 SystemErr     R     at com.game.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:456)
[6/25/15 17:52:53:925 IST] 00000036 SystemErr     R     at com.game.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:178)
[6/25/15 17:52:53:925 IST] 00000036 SystemErr     R     at com.game.ws.webcontainer.filter.WebAppFilterChain.invokeTarget(WebAppFilterChain.java:125)
[6/25/15 17:52:53:926 IST] 00000036 SystemErr     R     at com.game.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:92)
[6/25/15 17:52:53:926 IST] 00000036 SystemErr     R     at org.game.prev.gamer.filters.SessionFilter.doFilter(SessionFilter.java:104)
[6/25/15 17:52:53:926 IST] 00000036 SystemErr     R     at com.game.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:192)
[6/25/15 17:52:53:926 IST] 00000036 SystemErr     R     at com.game.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:89)
[6/25/15 17:52:53:926 IST] 00000036 SystemErr     R     at com.game.ws.webcontainer.filter.WebAppFilterManager.doFilter(WebAppFilterManager.java:926)
[6/25/15 17:52:53:926 IST] 00000036 SystemErr     R     at com.game.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1023)
[6/25/15 17:52:53:926 IST] 00000036 SystemErr     R     at com.game.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3703)
[6/25/15 17:52:53:926 IST] 00000036 SystemErr     R     at com.game.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:304)
[6/25/15 17:52:53:926 IST] 00000036 SystemErr     R     at com.game.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:962)
[6/25/15 17:52:53:926 IST] 00000036 SystemErr     R     at com.game.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1662)
[6/25/15 17:52:53:926 IST] 00000036 SystemErr     R     at com.game.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:195)
[6/25/15 17:52:53:926 IST] 00000036 SystemErr     R     at com.game.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:452)
[6/25/15 17:52:53:927 IST] 00000036 SystemErr     R     at com.game.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:511)
[6/25/15 17:52:53:927 IST] 00000036 SystemErr     R     at com.game.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:305)
[6/25/15 17:52:53:928 IST] 00000036 SystemErr     R     at com.game.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLReadCallback.java:83)
[6/25/15 17:52:53:928 IST] 00000036 SystemErr     R     at com.game.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:165)
[6/25/15 17:52:53:928 IST] 00000036 SystemErr     R     at com.game.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
[6/25/15 17:52:53:928 IST] 00000036 SystemErr     R     at com.game.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
[6/25/15 17:52:53:928 IST] 00000036 SystemErr     R     at com.game.io.async.AsyncFuture.completed(AsyncFuture.java:138)
[6/25/15 17:52:53:928 IST] 00000036 SystemErr     R     at com.game.io.async.ResultHandler.complete(ResultHandler.java:204)
[6/25/15 17:52:53:928 IST] 00000036 SystemErr     R     at com.game.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)
[6/25/15 17:52:53:928 IST] 00000036 SystemErr     R     at com.game.io.async.ResultHandler$2.run(ResultHandler.java:905)
[6/25/15 17:52:53:928 IST] 00000036 SystemErr     R     at com.game.ws.util.ThreadPool$Worker.run(ThreadPool.java:1659)

The log file after execution of the Select Statement shows :
[INFO]-The record retrieved is : {PROJECT_ID=63, FILE_NAME=MAT.png, DOCUMENTS=[B@1edef76, ALIAS=null, FILE_TYPE=image/png}

This shows that some value is being fetched into the blob field "DOCUMENTS", I am just not able to retrieve it properly.

I also tried this section of code, this even leads to the /DownloadFile.htm page, where I even got a 0 byte file as 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 * from project_storage  WHERE 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 is : "+file2.toString());     
                ObjectInputStream inputOStream  = new ObjectInputStream(inputFile);
                FileCopyUtils.copy(inputOStream, response.getOutputStream());               
            } catch (Exception e) {
                e.printStackTrace();
            }   
    }   
This however gives the FileNotFound Exception. Here is the StackTrace :


[6/25/15 18:06:39:825 IST] 00000037 SystemErr     R java.io.FileNotFoundException: [B@22b6d45 (The system cannot find the file specified.)
[6/25/15 18:06:39:826 IST] 00000037 SystemErr     R     at java.io.FileInputStream.open(Native Method)
[6/25/15 18:06:39:826 IST] 00000037 SystemErr     R     at java.io.FileInputStream.<init>(FileInputStream.java:123)
[6/25/15 18:06:39:826 IST] 00000037 SystemErr     R     at java.io.FileInputStream.<init>(FileInputStream.java:83)
[6/25/15 18:06:39:826 IST] 00000037 SystemErr     R     at org.game.prev.gamer.controller.AdminController.downloadFile(AdminController.java:210)
[6/25/15 18:06:39:826 IST] 00000037 SystemErr     R     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[6/25/15 18:06:39:826 IST] 00000037 SystemErr     R     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
[6/25/15 18:06:39:827 IST] 00000037 SystemErr     R     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
[6/25/15 18:06:39:827 IST] 00000037 SystemErr     R     at java.lang.reflect.Method.invoke(Method.java:611)
[6/25/15 18:06:39:827 IST] 00000037 SystemErr     R     at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
[6/25/15 18:06:39:827 IST] 00000037 SystemErr     R     at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
[6/25/15 18:06:39:827 IST] 00000037 SystemErr     R     at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
[6/25/15 18:06:39:827 IST] 00000037 SystemErr     R     at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
[6/25/15 18:06:39:827 IST] 00000037 SystemErr     R     at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
[6/25/15 18:06:39:827 IST] 00000037 SystemErr     R     at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
[6/25/15 18:06:39:827 IST] 00000037 SystemErr     R     at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
[6/25/15 18:06:39:827 IST] 00000037 SystemErr     R     at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
[6/25/15 18:06:39:827 IST] 00000037 SystemErr     R     at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
[6/25/15 18:06:39:828 IST] 00000037 SystemErr     R     at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
[6/25/15 18:06:39:828 IST] 00000037 SystemErr     R     at javax.servlet.http.HttpServlet.service(HttpServlet.java:575)
[6/25/15 18:06:39:828 IST] 00000037 SystemErr     R     at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
[6/25/15 18:06:39:828 IST] 00000037 SystemErr     R     at com.game.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1214)
[6/25/15 18:06:39:828 IST] 00000037 SystemErr     R     at com.game.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:774)
[6/25/15 18:06:39:828 IST] 00000037 SystemErr     R     at com.game.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:456)
[6/25/15 18:06:39:828 IST] 00000037 SystemErr     R     at com.game.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:178)
[6/25/15 18:06:39:828 IST] 00000037 SystemErr     R     at com.game.ws.webcontainer.filter.WebAppFilterChain.invokeTarget(WebAppFilterChain.java:125)
[6/25/15 18:06:39:828 IST] 00000037 SystemErr     R     at com.game.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:92)
[6/25/15 18:06:39:828 IST] 00000037 SystemErr     R     at org.game.prev.gamer.filters.SessionFilter.doFilter(SessionFilter.java:104)
[6/25/15 18:06:39:828 IST] 00000037 SystemErr     R     at com.game.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:192)
[6/25/15 18:06:39:829 IST] 00000037 SystemErr     R     at com.game.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:89)
[6/25/15 18:06:39:829 IST] 00000037 SystemErr     R     at com.game.ws.webcontainer.filter.WebAppFilterManager.doFilter(WebAppFilterManager.java:926)
[6/25/15 18:06:39:829 IST] 00000037 SystemErr     R     at com.game.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1023)
[6/25/15 18:06:39:829 IST] 00000037 SystemErr     R     at com.game.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3703)
[6/25/15 18:06:39:829 IST] 00000037 SystemErr     R     at com.game.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:304)
[6/25/15 18:06:39:829 IST] 00000037 SystemErr     R     at com.game.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:962)
[6/25/15 18:06:39:829 IST] 00000037 SystemErr     R     at com.game.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1662)
[6/25/15 18:06:39:829 IST] 00000037 SystemErr     R     at com.game.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:195)
[6/25/15 18:06:39:829 IST] 00000037 SystemErr     R     at com.game.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:452)
[6/25/15 18:06:39:829 IST] 00000037 SystemErr     R     at com.game.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:511)
[6/25/15 18:06:39:830 IST] 00000037 SystemErr     R     at com.game.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:305)
[6/25/15 18:06:39:830 IST] 00000037 SystemErr     R     at com.game.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLReadCallback.java:83)
[6/25/15 18:06:39:830 IST] 00000037 SystemErr     R     at com.game.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:165)
[6/25/15 18:06:39:830 IST] 00000037 SystemErr     R     at com.game.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
[6/25/15 18:06:39:830 IST] 00000037 SystemErr     R     at com.game.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
[6/25/15 18:06:39:830 IST] 00000037 SystemErr     R     at com.game.io.async.AsyncFuture.completed(AsyncFuture.java:138)
[6/25/15 18:06:39:830 IST] 00000037 SystemErr     R     at com.game.io.async.ResultHandler.complete(ResultHandler.java:204)
[6/25/15 18:06:39:830 IST] 00000037 SystemErr     R     at com.game.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)
[6/25/15 18:06:39:830 IST] 00000037 SystemErr     R     at com.game.io.async.ResultHandler$2.run(ResultHandler.java:905)
[6/25/15 18:06:39:830 IST] 00000037 SystemErr     R     at com.game.ws.util.ThreadPool$Worker.run(ThreadPool.java:1659)

I believe there has to be a way around this.. I just am not getting the correct DataType/method to correctly extract the blob file. Please help.

file2 is a byte array, not a File. That's why you get a ClassCastException. Calling toString() on a byte array won't lead to anything useful. Just cast file2 to byte[], and write this byte array to the response output stream.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM