繁体   English   中英

使用Java以编程方式将图像上传到Google App Engine Blobstore

[英]Upload an image to google app engine blobstore with java programmatically

我已经看过Google应用程序引擎页面中的“将文件写入Blobstore(实验性)”。

这就是我所拥有的:

// 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但是在Google App Engine中,不支持ImageWriter 我该怎么办?

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

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

你应该做这个:

public static BlobKey toBlobstore(Blob imageData) throws FileNotFoundException,     FinalizationException, LockException, IOException {
    if (null == imageData)
        return null;

    // Get a file service
    FileService fileService = FileServiceFactory.getFileService();

    // Create a new Blob file with mime-type "image/png"
    AppEngineFile file = fileService.createNewBlobFile("image/jpeg");// png

    // Open a channel to write to it
    boolean lock = true;
    FileWriteChannel writeChannel = fileService.openWriteChannel(file, lock);

    // This time we write to the channel directly
    writeChannel.write(ByteBuffer.wrap
        (imageData.getBytes()));

    // Now finalize
    writeChannel.closeFinally();
    return fileService.getBlobKey(file);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM