【问题标题】:Upload an image to google app engine blobstore with java programmatically以编程方式使用 java 将图像上传到谷歌应用引擎 blobstore
【发布时间】:2013-01-23 03:37:15
【问题描述】:

我已经在 google 应用引擎页面中观看了“Writing Files to the Blobstore (Experimental)”。

这就是我所拥有的:

// 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 不受支持。我该怎么办?

【问题讨论】:

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


    【解决方案1】:

    你应该这样做:

    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);
    }
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 感谢您的回答。现在,我可以存储图片,也可以从 blobstore 获取。但是图片(来自blobstore)不清楚。它没有很好的分辨率,(对不起,我不擅长英文写作:()有没有其他方法可以将图片存储在输入流类型上?
      • Blobstore 是二进制存储 - 它不会更改数据内容。您的问题一定出在其他地方。
      • 是的,字节变量的大小有问题。所以我得到了输入流的大小,并使字节变量大小与输入流类型变量的大小有关。所以感谢您的帮助,我可以将图片存储到 blobstore 中。
      猜你喜欢
      • 2012-06-11
      • 2014-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-18
      • 1970-01-01
      • 1970-01-01
      • 2010-12-03
      相关资源
      最近更新 更多