2009-10-03 65 views

回答

96

鏈接您提供"How do I handle file uploads to my app?"介紹瞭如何上傳圖片。

主辦的圖像,你需要使用Datastore service來與其他數據一起存儲和提供圖片。

這裏是一個示例代碼。它的意思是作爲一個草圖,你如何擁有自己的實體(例如企業,用戶等)擁有一個圖像領域。我忽略了所有錯誤處理和恢復來簡化代碼。

用圖像聲明你的實體。你可以想象有其他領域,例如標籤,位置等

@Entity 
public class MyImage { 
    @PrimaryKey 
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) 
    private Long id; 

    @Persistent 
    private String name; 

    @Persistent 
    Blob image; 

    public MyImage() { } 
    public MyImage(String name, Blob image) { 
     this.name = name; 
     this.image = image; 
    } 

    // JPA getters and setters and empty contructor 
    // ... 
    public Blob getImage()    { return image; } 
    public void setImage(Blob image) { this.image = image; } 
} 

然後,當你開始接受影像(注意,其中具有相同名稱的圖像已經在除了典型的文件上傳失敗上傳的情況下)。 ServletFileUploadIOUtils是Apache Commons庫的一部分。

// Your upload handle would look like 
public void doPost(HttpServletRequest req, HttpServletResponse res) { 
    // Get the image representation 
    ServletFileUpload upload = new ServletFileUpload(); 
    FileItemIterator iter = upload.getItemIterator(req); 
    FileItemStream imageItem = iter.next(); 
    InputStream imgStream = imageItem.openStream(); 

    // construct our entity objects 
    Blob imageBlob = new Blob(IOUtils.toByteArray(imgStream)); 
    MyImage myImage = new MyImage(imageItem.getName(), imageBlob); 

    // persist image 
    PersistenceManager pm = PMF.get().getPersistenceManager(); 
    pm.makePersistent(myImage); 
    pm.close(); 

    // respond to query 
    res.setContentType("text/plain"); 
    res.getOutputStream().write("OK!".getBytes()); 
} 

最後當你想成爲一個像它的名字:

Blob imageFor(String name, HttpServletResponse res) { 
    // find desired image 
    PersistenceManager pm = PMF.get().getPersistenceManager(); 
    Query query = pm.newQuery("select from MyImage " + 
     "where name = nameParam " + 
     "parameters String nameParam"); 
    List<MyImage> results = (List<MyImage>)query.execute(name); 
    Blob image = results.iterator().next().getImage(); 

    // serve the first image 
    res.setContentType("image/jpeg"); 
    res.getOutputStream().write(image.getBytes()); 
} 
+0

非常感謝你的這個清晰和易於理解的答案! :) – OXMO456 2009-10-03 17:29:56

+0

太棒了,謝謝。你能告訴我爲什麼字節數組不會給出正確的輸出,雖然它被存儲等,我剛纔提出了這個問題http://stackoverflow.com/questions/2018597/cannot-fetch-image-data-in- gwt-google-datastore-image-is-stre-out-out – dhaval 2010-01-29 18:44:22

+0

優秀的答案! – 2010-08-04 05:05:02

10

使用blobstore API

的Blob存儲API允許應用程序服務數據對象,稱爲blob,這比數據存儲服務中的對象所允許的大小要大得多。 Blob對於提供大型文件(如視頻或圖像文件)以及允許用戶上傳大型數據文件非常有用。通過HTTP請求上傳文件來創建Blob。通常,您的應用程序將通過向用戶呈現帶有文件上傳字段的表單來完成此操作。提交表單時,Blobstore會根據文件內容創建一個blob,並返回一個不透明的引用,稱爲blob密鑰,稍後您可以使用該引用來提供blob。應用程序可以響應用戶請求提供完整的blob值,也可以直接使用類流式文件接口讀取值...

+3

這需要在您的應用程序中啓用計費。 – 2011-03-11 15:50:38

+1

@ luca-matteis這是http://groups.google.com/group/gaelyk/browse_thread/thread/7982f33b5093a162 – jontro 2011-05-23 22:58:50

+0

@LucaMatteis的解決方法嗨,是真的嗎?如果我在自由默認配額內使用,是否仍需要啓用結算? – GMsoF 2012-11-14 02:06:36

5

最簡單的方式使用Google App Engine Blob Store服務URL(你節省實例時間)

import com.google.appengine.api.files.FileService; 
import com.google.appengine.api.files.AppEngineFile; 
import com.google.appengine.api.files.FileWriteChannel; 
import com.google.appengine.api.blobstore.BlobKey; 
import com.google.appengine.api.images.ImagesServiceFactory; 
import com.google.appengine.api.images.ServingUrlOptions; 
... 


// your data in byte[] format 
byte[] data = image.getData(); 
/** 
* MIME Type for 
* JPG use "image/jpeg" for PNG use "image/png" 
* PDF use "application/pdf" 
* see more: https://en.wikipedia.org/wiki/Internet_media_type 
*/ 
String mimeType = "image/jpeg"; 

// save data to Google App Engine Blobstore 
FileService fileService = FileServiceFactory.getFileService(); 
AppEngineFile file = fileService.createNewBlobFile(mimeType); 
FileWriteChannel writeChannel = fileService.openWriteChannel(file, true); 
writeChannel.write(java.nio.ByteBuffer.wrap(data)); 
writeChannel.closeFinally(); 

// your blobKey to your data in Google App Engine BlobStore 
BlobKey blobKey = fileService.getBlobKey(file); 

// THANKS TO BLOBKEY YOU CAN GET FOR EXAMPLE SERVING URL FOR IMAGES 

// Get the image serving URL (in https:// format) 
String imageUrl = 
    ImagesServiceFactory.getImagesService().getServingUrl(
    ServingUrlOptions.Builder.withBlobKey(blobKey 
     ).secureUrl(true)); 
+1

警告!上面的代碼已被棄用。 – 2014-09-08 13:17:02

+1

以上代碼的任何替代方式已棄用? – 2016-03-24 04:29:40

+0

我相信服務圖像的新方式可能是這樣的雲存儲: https://cloud.google.com/python/getting-started/using-cloud-storage – 2017-09-11 20:12:08

相關問題