本文介绍了以编程方式将图像上传到Google应用引擎blobstore的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在谷歌应用程序引擎页面中观看了将文件写入Blobstore(实验)。

I have already watched "Writing Files to the Blobstore (Experimental)" in the google app engine page.

这就是我所拥有的:

// Get a file service
  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 = false;
  FileWriteChannel writeChannel = fileService.openWriteChannel(file, lock);

  // Different standard Java ways of writing to the channel
  // are possible. Here we use a PrintWriter:
  **PrintWriter** out = new PrintWriter(Channels.newWriter(writeChannel, "UTF8"));
  out.println("The woods are lovely dark and deep.");
  out.println("But I have promises to keep.");

  // Close without finalizing and save the file path for writing later
  out.close();
  String path = file.getFullPath();

  // Write more to the file in a separate request:
  file = new AppEngineFile(path);

  // This time lock because we intend to finalize
  lock = true;
  writeChannel = fileService.openWriteChannel(file, lock);

  // This time we write to the channel directly
  writeChannel.write(ByteBuffer.wrap
            ("And miles to go before I sleep.".getBytes()));

  // Now finalize
  writeChannel.closeFinally();

  // Later, read from the file using the file API
  lock = false; // Let other people read at the same time
  FileReadChannel readChannel = fileService.openReadChannel(file, false);

  // Again, different standard Java ways of reading from the channel.
  BufferedReader reader =
          new BufferedReader(Channels.newReader(readChannel, "UTF8"));
       String line = reader.readLine();
  // line = "The woods are lovely dark and deep."

  readChannel.close();

  // Now read from the file using the Blobstore API
  BlobKey blobKey = fileService.getBlobKey(file);
  BlobstoreService blobStoreService = BlobstoreServiceFactory.getBlobstoreService();
  String segment = new String(blobStoreService.fetchData(blobKey, 30, 40));

不幸的是,这仅仅适用于文本文件。我假设 PrintWriter 应改为 ImageWriter ,但在谷歌应用程序引擎中, ImageWriter 不受支持。我应该怎么做?

Unfortunately, this is just for text file. I would assume that PrintWriter should be changed to ImageWriter but in google app engine, ImageWriter is not supported. What should I do?

推荐答案

您可以简单地将二进制数据写入Blobstore:

You can simply write binary data to blobstore:

byte[] yourBinaryData = // get your data from request
writeChannel.write(ByteBuffer.wrap(yourBinaryData));

这篇关于以编程方式将图像上传到Google应用引擎blobstore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 18:16