繁体   English   中英

提取Blob值并传递到Spring MVC中的Controller(不使用休眠)

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

有什么方法可以在不使用休眠的情况下从(Oracle)数据库中提取Blob文件并将其传递到Spring MVC中的Controller中?

以下是我的表结构:

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$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)

执行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$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)

我相信必须有一种解决方法。我只是没有获得正确的DataType /方法来正确提取blob文件。 请帮忙。

file2是字节数组,而不是File。 这就是为什么您收到ClassCastException。 在字节数组上调用toString()不会导致任何有用的事情。 只需将file2强制转换为byte [],然后将此字节数组写入响应输出流。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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