2012年4月19日木曜日

[java]GAEでバイナリデータをBlobstoreに登録する←FileAPI


GAE/Jでサーバのメモリ上で展開しているバイナリデータ(JPEG)をBlobstoreに追加するスニペット。

    private BlobKey registerBlob(byte[] bytes) throws IOException{
        // Get a file service
        FileService fileService = FileServiceFactory.getFileService();
        // Create a new Blob file with mime-type "application/octet-stream"
        AppEngineFile file = fileService.createNewBlobFile("image/jpeg");
        // Open a channel to write to it
        boolean lock = true;
        FileWriteChannel writeChannel = fileService.openWriteChannel(file, lock);
        // This time we write to the channel using standard Java
        writeChannel.write(ByteBuffer.wrap(bytes));
        // Now finalize
        writeChannel.closeFinally();
        return fileService.getBlobKey(file);
    }


0 件のコメント:

コメントを投稿