我正在尝试以编程方式将一些文件上传到 blobstore 并更新数据存储中的某些内容。可以使用专门的 servlet 和 CURL 来完成,但使用远程 api 会更优雅。是否可以通过 appengine 的远程 API 使用文件服务?如果是,为什么在执行 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)