簡體   English   中英

如何從Appengine數據存儲區實體對象中逐字節讀取

[英]How to read byte by byte from appengine datastore Entity Object

簡而言之,由於GAE無法寫入文件系統,因此我決定將數據持久化到數據存儲中(使用JDO)。 現在,我想逐字節檢索數據並將其作為輸入流傳遞給客戶端。 gwtupload庫(http://code.google.com/p/gwtupload/)中的代碼(請參見下文)在GAE上會中斷,因為它會寫入系統文件系統。 我希望能夠提供GAE移植的解決方案。

public static void copyFromInputStreamToOutputStream(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[100000];
    while (true) {
      synchronized (buffer) {
        int amountRead = in.read(buffer);
        if (amountRead == -1) {
          break;
        }
        out.write(buffer, 0, amountRead);
      }
    }
    in.close();
    out.flush();
    out.close();
  }

我嘗試過的一種解決方法(無法解決)是從數據存儲中檢索數據,作為這樣的資源:

InputStream resourceAsStream = null;
    PersistenceManager pm = PMF.get().getPersistenceManager();
    try {
        Query q = pm.newQuery(ImageFile.class);
        lf  = q.execute();
        resourceAsStream = getServletContext().getResourceAsStream((String) pm.getObjectById(lf));
    } finally {
      pm.close();
    }
    if (lf != null) {
      response.setContentType(receivedContentTypes.get(fieldName));
      copyFromInputStreamToOutputStream(resourceAsStream, response.getOutputStream());

    } 

我歡迎您的建議。

問候

將數據存儲在字節數組中,並使用ByteArrayInputStreamByteArrayOutputStream將其傳遞給需要流的庫。

但是,如果“客戶端”是指“ HTTP客戶端”或瀏覽器,則沒有理由這樣做-只需處理終端的常規字節數組,就可以像處理其他任何數據一樣將它們發送給用戶/從用戶發送出去。 弄亂這樣的流的唯一原因是,如果您有一些期望它們的庫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM