java - 以编程方式使用java将图像上传到google app engine blobstore

标签 java google-app-engine blobstore image-upload

我已经在 google app engine 页面上看过“将文件写入 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 但在谷歌应用引擎中, ImageWriter 不受支持。我该怎么办?

最佳答案

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

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

关于java - 以编程方式使用java将图像上传到google app engine blobstore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14754476/

相关文章:

java - 使用 Xpath 从一个 html 标签切换到另一个

java - 如何自定义Android评级栏形状并编号1-10?

java - 我得到 "NoClassDefFoundError: org/apache/logging/log4j/util/ReflectionUtil"

java - 在有限域上求解方程组

java - 为什么我在 try catch JSP 输出时无法包装 ServletRequest

python - 为什么 PostgreSQL 适配器 psycopg2 在 Google App Engine dev_appserver.py 中失败?

php - 如何在 Cloud SQL 实例中设置授权 App Engine 应用程序

java - 如何从谷歌驱动器读取大文件到 gae blobstore

java - java中的getServingUrl方法在Google云存储中获取异常

java - 在谷歌应用程序引擎上保存转换后的图像