App Engine 可以存储诸如 ObjectOutputStream 之类的信息吗?
问问题
387 次
3 回答
0
FileService fileService = FileServiceFactory.getFileService();
// Create a new Blob file with mime-type "text/plain"
AppEngineFile file = fileService.createNewBlobFile("text/plain");
// Open a channel to write to it
boolean lock = true;
FileWriteChannel writeChannel = fileService.openWriteChannel(file, lock);
MyObject obj = new MyObject();
obj.name="testing now";
ObjectOutputStream oos = new ObjectOutputStream(Channels.newOutputStream(writeChannel));
oos.writeObject(obj);
oos.flush();
oos.close();
// Now finalize
writeChannel.closeFinally();
// Later, read from the file using the file API
FileReadChannel readChannel = fileService.openReadChannel(file, false);
ObjectInputStream ooi = new ObjectInputStream(Channels.newInputStream(readChannel));
resp.setContentType("text/plain");
try {
resp.getWriter().print(ooi.readObject().toString());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
这是将 POJO 保存到 blobstore 的代码。MyObject 需要实现 Serializable 接口。
于 2012-07-10T10:17:06.913 回答