如何在GoogleAppEngineJava中将文件包装为blob并将其提交为数据存储
我尝试使用文件服务将数据存储在Google app engine中,并成功上传了它,但后来我注意到它没有存储为blob值。所以我在谷歌上找到了这个由谷歌提供的链接 在本文件中,代码如下所示:如何在GoogleAppEngineJava中将文件包装为blob并将其提交为数据存储,java,google-app-engine,google-cloud-storage,Java,Google App Engine,Google Cloud Storage,我尝试使用文件服务将数据存储在Google app engine中,并成功上传了它,但后来我注意到它没有存储为blob值。所以我在谷歌上找到了这个由谷歌提供的链接 在本文件中,代码如下所示: FileItemIterator iterator = upload.getItemIterator(req); while (iterator.hasNext()) { FileItemStream item = iterator.next(); InputStream stream = item
FileItemIterator iterator = upload.getItemIterator(req);
while (iterator.hasNext()) {
FileItemStream item = iterator.next();
InputStream stream = item.openStream();
if (item.isFormField()) {
log.warning("Got a form field: " + item.getFieldName());
} else {
log.warning("Got an uploaded file: " + item.getFieldName() +", name = " + item.getName());
// You now have the filename (item.getName() and the
// contents (which you can read from stream). Here we just
// print them back out to the servlet output stream, but you
// will probably want to do something more interesting (for
// example, wrap them in a Blob and commit them to the
// datastore).
这里我不理解如何将它们包装为blob并提交到数据存储。谁能建议我怎么解决这个问题吗。这种方式是否将文件作为blob值存储在google app engine中?这种方式是将文件作为blob值存储还是作为blobstore中的文件存储?但是我需要将文件作为blob值存储。您能告诉我如何在不使用
createUploadURL
表单操作属性的情况下实现它吗?只需创建db实体并向其添加字节数组属性即可。FileServiceFactory现在是已弃用:(这是将文件存储为blob值还是存储为blob存储中的文件?但我需要存储为blob值您能告诉我如何在不使用createUploadURL
表单操作属性的情况下实现它吗?只需创建db实体并向其添加字节数组属性。FileServiceFactory现在已弃用:(
InputStream is = item.openStream();
try {
FileService fileService = FileServiceFactory.getFileService();
AppEngineFile file = fileService.createNewBlobFile(mime, fileName);
boolean lock = true;
FileWriteChannel writeChannel = fileService.openWriteChannel(file, lock);
byte[] buffer = new byte[BUFFER_SIZE];
int readBytes;
while ((readBytes = is.read(buffer)) != -1) {
writeChannel.write(ByteBuffer.wrap(buffer, 0, readBytes));
}
writeChannel.closeFinally();
String blobKey = fileService.getBlobKey(file).getKeyString();
} catch (Exception e) {
e.printStackTrace(resp.getWriter());
}