我使用以下代码将文件从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;
}
答案 0 :(得分:0)
您需要使用Java Client library:
GcsOutputChannel outputChannel =
gcsService.createOrReplace(fileName, GcsFileOptions.getDefaultInstance());
outputChannel.write(ByteBuffer.wrap(content));
outputChannel.close();