Upload an image to google app engine blobstore with java programmatically
Asked Answered
E

2

2

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

This is what I have :

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

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?

Eyeglasses answered 7/2, 2013 at 15:13 Comment(0)
L
3

You can simply write binary data to blobstore:

byte[] yourBinaryData = // get your data from request
writeChannel.write(ByteBuffer.wrap(yourBinaryData));
Loella answered 7/2, 2013 at 17:43 Comment(3)
thanks for your answer. Now, i can store a picture and also get from blobstore. But the picture (from blobstore) is not clear. It has no good resolution, (sorry, i am not good at english writing :() is there any other way to store a picture on a inputstream type?Eyeglasses
Blobstore is a binary store - it does not change data contents. Your problem must be somewhere else.Loella
yeah the size of byte variable was problem. so i got a size of inputstream and make the byte variable size as to size of inputstream type variable. so i can store a picture to the blobstore well thanks to your help.Eyeglasses
C
3

You should do this:

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);
}
Cerement answered 27/4, 2013 at 1:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.