2012-05-24 59 views
1

我試圖以編程方式將一些文件上傳到blobstore,並更新數據存儲區中的某些內容。它可以使用專門的servlet和CURL來完成,但使用遠程API會更加優雅。 是否可以通過appengine的遠程apis使用文件服務? 如果是,爲什麼執行bos.write(b)時會得到這個異常? 感謝 代碼:使用遠程api的appengine fileservice

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) 

回答

4

文件API目前尚未完全超過remote_api的支持。你應該可以得到一個文件上傳的URL,但如果你想真正上傳文件,你需要發佈到一個自定義的servlet。

+0

非常感謝。沒有社區我會迷失方向。 –