我使用以下代码将文件从Google驱动器移动到blobstore。但是现在FileWriteChannel已被弃用,代码不起作用。有没有针对此问题的替代解决方案?
private BlobKey getBlobKey(File f, DriveObject driveObject)
throws IOException, MalformedURLException {
Drive service = ((GoogleDrive) driveObject).getService();
byte[] buffer = new byte[(int) f.getFileSize().intValue()];
GenericUrl url = new GenericUrl(f.getDownloadUrl());
HttpResponse response = service.getRequestFactory()
.buildGetRequest(url).execute();
InputStream is = response.getContent();
FileService fileService = FileServiceFactory.getFileService();
AppEngineFile file = null;
boolean lock = true;
try {
file = fileService.createNewBlobFile("application/zip");
FileWriteChannel writeChannel = fileService.openWriteChannel(
file, lock);
int len;
while ((len = is.read(buffer)) >= 0) {
ByteBuffer bb = ByteBuffer.wrap(buffer, 0, len);
writeChannel.write(bb);
}
writeChannel.closeFinally();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BlobKey bk = fileService.getBlobKey(file);
return bk;
}
您需要使用 Java 客户端库:
GcsOutputChannel outputChannel =
gcsService.createOrReplace(fileName, GcsFileOptions.getDefaultInstance());
outputChannel.write(ByteBuffer.wrap(content));
outputChannel.close();