2011-04-06 54 views
1

簡而言之,由於GAE無法寫入文件系統,因此我決定將數據保存到數據存儲中(使用JDO)。現在,我希望逐字節地檢索數據並將其作爲輸入流傳遞給客戶端。 gwtupload庫(http://code.google.com/p/gwtupload/)中有代碼(見下文),它在GAE上中斷,因爲它寫入系統文件系統。我希望能夠提供一個GAE移植解決方案。如何從appengine數據存儲中逐字節讀取實體對象

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()); 

    } 

我歡迎您的建議。

問候

+0

您是否試圖序列化客戶端/服務器之間的POJO? – 2011-04-07 02:58:17

+0

不!我基本上試圖將文件上傳到GAE,使用GWT作爲客戶端。在進行上傳時,我想逐字節地監視它,如進度條或%讀數。 – drecute 2011-04-10 17:37:24

回答

0

存儲數據在一個字節數組,並使用一個或ByteArrayInputStreamByteArrayOutputStream它傳遞給該期望流庫。

如果通過'客戶端'表示'HTTP客戶端'或瀏覽器,但是沒有理由這麼做 - 只需處理您的端點上的常規字節數組並將它們發送到用戶/從用戶發送,就像其他任何數據一樣。弄亂這樣的流的唯一原因是如果你有一些期望它們的庫。

相關問題