Upload an image to google app engine blobstore with java programmatically

后端 未结 2 551
粉色の甜心
粉色の甜心 2020-12-17 07:01

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
          


        
相关标签:
2条回答
  • 2020-12-17 07:46

    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);
    }
    
    0 讨论(0)
  • 2020-12-17 08:03

    You can simply write binary data to blobstore:

    byte[] yourBinaryData = // get your data from request
    writeChannel.write(ByteBuffer.wrap(yourBinaryData));
    
    0 讨论(0)
提交回复
热议问题