Upload an image to google app engine blobstore with java programmatically

后端 未结 2 560
粉色の甜心
粉色の甜心 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);
    }
    

提交回复
热议问题