如何按名称检索blobstore文件

时间:2012-05-06 17:27:36

标签: google-app-engine blobstore

我写了以下代码:

                     FileService fileService = FileServiceFactory
                 .getFileService();

                 // Create a new Blob file with mime-type "text/plain"
                 AppEngineFile file = fileService.createNewBlobFile(
                 "image/jpeg", name);

                 if(null!=file)
                 log.warn(file.toString());

                 FileReadChannel readChannel =
                 fileService.openReadChannel(
                 file, true);

                 // Again, different standard Java ways of reading from
                 // the
                 // channel.
                 BufferedReader reader = new BufferedReader(Channels
                 .newReader(readChannel, "UTF8"));
                 String line = reader.readLine();

                 resp.setContentType("text/plain");
                 resp.getWriter().println(line);

                 readChannel.close();

但不幸的是,它给了我以下例外:

  

来自servlet的未捕获异常   com.google.appengine.api.files.FinalizationException       在com.google.appengine.api.files.FileServiceImpl.translateException(FileServiceImpl.java:597)       在com.google.appengine.api.files.FileServiceImpl.makeSyncCall(FileServiceImpl.java:574)       在com.google.appengine.api.files.FileServiceImpl.open(FileServiceImpl.java:496)       在com.google.appengine.api.files.FileServiceImpl.openForRead(FileServiceImpl.java:456)       在com.google.appengine.api.files.FileServiceImpl.openForRead(FileServiceImpl.java:447)       在com.google.appengine.api.files.FileServiceImpl.openReadChannel(FileServiceImpl.java:202)       在com.myfileshost.MyfileshostServlet.doGet(MyfileshostServlet.java:362)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:617)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:717)       在org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)       在org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1166)       在com.google.apphosting.utils.servlet.ParseBlobUploadFilter.doFilter(ParseBlobUploadFilter.java:102)       在org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1157)       在com.google.apphosting.runtime.jetty.SaveSessionFilter.doFilter(SaveSessionFilter.java:35)       在org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1157)       在com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43)       在org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1157)       在org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)       在org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)       在org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)       在org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)       在org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)       在com.google.apphosting.runtime.jetty.AppVersionHandlerMap.handle(AppVersionHandlerMap.java:249)       在org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)       在org.mortbay.jetty.Server.handle(Server.java:326)       在org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)       at org.mortbay.jetty.HttpConnection $ RequestHandler.headerComplete(HttpConnection.java:923)       在com.google.apphosting.runtime.jetty.RpcRequestParser.parseAvailable(RpcRequestParser.java:76)       在org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)       在com.google.apphosting.runtime.jetty.JettyServletEngineAdapter.serviceRequest(JettyServletEngineAdapter.java:135)       在com.google.apphosting.runtime.JavaRuntime $ RequestRunnable.run(JavaRuntime.java:446)       在com.google.tracing.TraceContext $ TraceContextRunnable.runInContext(TraceContext.java:449)       在com.google.tracing.TraceContext $ TraceContextRunnable $ 1.run(TraceContext.java:455)       在com.google.tracing.TraceContext.runInContext(TraceContext.java:695)       在com.google.tracing.TraceContext $ AbstractTraceContextCallback.runInInheritedContextNoUnref(TraceContext.java:333)       在com.google.tracing.TraceContext $ AbstractTraceContextCallback.runInInheritedContext(TraceContext.java:325)       在com.google.tracing.TraceContext $ TraceContextRunnable.run(TraceContext.java:453)       在com.google.apphosting.runtime.ThreadGroupPool $ PoolEntry.run(ThreadGroupPool.java:251)       在java.lang.Thread.run(Thread.java:679)

1 个答案:

答案 0 :(得分:1)

当通过GAE FileService创建文件时,必须先将其最终确定,然后才能读取。

openReadChannel(..)的Javadoc明确指出,如果文件未最终确定,此方法将抛出FinalizationException

以下是有关如何创建文件,完成文件然后从中进行阅读的完整示例:https://developers.google.com/appengine/docs/java/blobstore/overview#Writing_Files_to_the_Blobstore