提取 Blob 值并传递给 Spring MVC 中的控制器(不使用休眠)

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

有没有办法在不使用休眠的情况下从(Oracle)数据库中提取 blob 文件并将其传递给 spring MVC 中的控制器?

以下是我的table结构:

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

我现在的代码如下:

@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();
            }   
    }

堆栈跟踪:

[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.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)

执行 Select 语句后的日志文件显示:
[INFO]-检索到的记录是:{PROJECT_ID=63, FILE_NAME=MAT.png, DOCUMENTS=[B@1edef76, ALIAS=null, FILE_TYPE=image/png}

这表明正在将某些值提取到 blob 字段中 "DOCUMENTS",我只是无法正确检索它。

我也尝试了这部分代码,这甚至导致了 /DownloadFile.htm 页面,在那里我什至得到了一个 0 字节的文件作为下载输出:

@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.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)

我相信一定有办法解决这个问题。我只是没有得到正确的 DataType/method 来正确提取 blob 文件。请帮忙。

file2 是字节数组,不是文件。这就是您获得 ClassCastException 的原因。在字节数组上调用 toString() 不会产生任何有用的结果。只需将 file2 转换为 byte[],并将此字节数组写入响应输出流。