使用远程api的appengine文件服务

时间:2012-05-24 17:37:06

标签: java google-app-engine

我正在尝试以编程方式将一些文件上传到blobstore,并在数据存储区中更新某些内容。可以使用专门的servlet和CURL来完成它,但使用远程api会更优雅。 是否可以通过appengine的远程apis使用文件服务? 如果是,为什么我在执行bos.write(b)时遇到此异常? 谢谢 代码:

String localFileName = "c:\\actcut fail.jpg";
                AppEngineFile aeF = fileService.createNewBlobFile("", "actcutfail.jpg");
                FileWriteChannel writeChannel = (FileWriteChannel) fileService.openWriteChannel(aeF, true); 
                BufferedOutputStream bos = new BufferedOutputStream(Channels.newOutputStream(writeChannel));
                FileInputStream fis = new FileInputStream(localFileName);
                try {
                    while (true) {
                        int b = fis.read();
                        bos.write(b);
                    }
                } catch (EOFException eofex) {
                    // end of file reached
                } catch (Exception ex) {
                    ex.printStackTrace();
                } finally {
                    fis.close();
                    bos.flush();
                    bos.close();
                    writeChannel.closeFinally();
                    System.out.println("uploaded blob file with key=" + fileService.getBlobKey(aeF).getKeyString());
                }

异常:

java.io.IOException
    at com.google.appengine.api.files.FileServiceImpl.translateException(FileServiceImpl.java:601)
    at com.google.appengine.api.files.FileServiceImpl.makeSyncCall(FileServiceImpl.java:574)
    at com.google.appengine.api.files.FileServiceImpl.append(FileServiceImpl.java:510)
    at com.google.appengine.api.files.FileServiceImpl.append(FileServiceImpl.java:255)
    at com.google.appengine.api.files.FileWriteChannelImpl.write(FileWriteChannelImpl.java:52)
    at com.google.appengine.api.files.FileWriteChannelImpl.write(FileWriteChannelImpl.java:44)
    at java.nio.channels.Channels.write(Channels.java:63)
    at java.nio.channels.Channels.access$000(Channels.java:47)
    at java.nio.channels.Channels$1.write(Channels.java:134)
    at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:65)
    at java.io.BufferedOutputStream.write(BufferedOutputStream.java:78)
    at cri.uploaddevalle.UploadDevalle.main(UploadDevalle.java:61)
Caused by: com.google.apphosting.api.ApiProxy$ApplicationException: ApplicationError: 10: File not opened: /blobstore/writable:GlPgcwTQdPtgYVY_Jpk9hg
    at com.google.appengine.api.files.dev.LocalFileService.throwError(LocalFileService.java:206)
    at com.google.appengine.api.files.dev.LocalFileService.append(LocalFileService.java:352)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.google.appengine.tools.development.ApiProxyLocalImpl$AsyncApiCall.callInternal(ApiProxyLocalImpl.java:498)
    at com.google.appengine.tools.development.ApiProxyLocalImpl$AsyncApiCall.call(ApiProxyLocalImpl.java:452)
    at com.google.appengine.tools.development.ApiProxyLocalImpl$AsyncApiCall.call(ApiProxyLocalImpl.java:430)
    at java.util.concurrent.Executors$PrivilegedCallable$1.run(Executors.java:463)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.util.concurrent.Executors$PrivilegedCallable.call(Executors.java:460)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:619)
java.io.IOException
    at com.google.appengine.api.files.FileServiceImpl.translateException(FileServiceImpl.java:601)
    at com.google.appengine.api.files.FileServiceImpl.makeSyncCall(FileServiceImpl.java:574)
    at com.google.appengine.api.files.FileServiceImpl.append(FileServiceImpl.java:510)
    at com.google.appengine.api.files.FileServiceImpl.append(FileServiceImpl.java:255)
    at com.google.appengine.api.files.FileWriteChannelImpl.write(FileWriteChannelImpl.java:52)
    at com.google.appengine.api.files.FileWriteChannelImpl.write(FileWriteChannelImpl.java:44)
    at java.nio.channels.Channels.write(Channels.java:63)
    at java.nio.channels.Channels.access$000(Channels.java:47)
    at java.nio.channels.Channels$1.write(Channels.java:134)
    at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:65)
    at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:123)
    at cri.uploaddevalle.UploadDevalle.main(UploadDevalle.java:69)
Caused by: com.google.apphosting.api.ApiProxy$ApplicationException: ApplicationError: 10: File not opened: /blobstore/writable:GlPgcwTQdPtgYVY_Jpk9hg
    at com.google.appengine.api.files.dev.LocalFileService.throwError(LocalFileService.java:206)
    at com.google.appengine.api.files.dev.LocalFileService.append(LocalFileService.java:352)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.google.appengine.tools.development.ApiProxyLocalImpl$AsyncApiCall.callInternal(ApiProxyLocalImpl.java:498)
    at com.google.appengine.tools.development.ApiProxyLocalImpl$AsyncApiCall.call(ApiProxyLocalImpl.java:452)
    at com.google.appengine.tools.development.ApiProxyLocalImpl$AsyncApiCall.call(ApiProxyLocalImpl.java:430)
    at java.util.concurrent.Executors$PrivilegedCallable$1.run(Executors.java:463)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.util.concurrent.Executors$PrivilegedCallable.call(Executors.java:460)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:619)

1 个答案:

答案 0 :(得分:4)

目前,remote_api目前尚未完全支持File API。您应该可以使用它获取文件上载URL,但如果您想要实际上传文件,则需要发布到自定义servlet。