2013-02-07 57 views
2

我已經在谷歌應用程序引擎頁面中觀看了「將文件寫入Blobstore(實驗)」。以編程方式將圖像上傳到谷歌應用程序引擎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。我該怎麼辦?

回答

3

你可以簡單的寫二進制數據BLOBSTORE:

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

謝謝您的回答。現在,我可以存儲一張照片,也可以從blobstore中獲取。但圖片(來自blobstore)尚不清楚。它沒有好的分辨率,(對不起,我不擅長英文寫作:()有沒有其他的方式來存儲輸入流類型的圖片? –

+0

Blobstore是一個二進制存儲 - 它不會改變數據內容。 –

+0

是的,字節變量的大小是有問題的,所以我得到了一個inputstream的大小,並根據inputstream類型變量的大小創建了字節變量大小,所以我可以將圖片存儲到blobstore,謝謝你的幫助。 –

3

你應該這樣做:

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); 
} 
相關問題