2011-04-19 64 views
0

,我發現了以下情況例外,當我嘗試使用Java來模擬一個POST請求上傳到App Engine的Blob存儲區通過字節數組更新Blob存儲區:谷歌應用程序引擎 - 使用模擬POST

WARNING: /_ah/login 
java.lang.NullPointerException 
    at com.google.appengine.api.users.dev.LoginCookieUtils.encodeEmailAsUserId(LoginCookieUtils.java:89) 
    at com.google.appengine.api.users.dev.LoginCookieUtils.createCookie(LoginCookieUtils.java:41) 
    at com.google.appengine.api.users.dev.LocalLoginServlet.doPost(LocalLoginServlet.java:90) 

這裏是做POST請求的代碼:

byte[] htmlData = // ...I already have the byte array I want to store/update) 

    BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService(); 
      String uploadURL = blobstoreService.createUploadUrl("/upload"); 

         //add host if in dev mode 
      if(uploadURL.indexOf("http") == -1) 
      { 
       uploadURL = "http://localhost:8888" + uploadURL; 
      } 
      URL url = new URL(uploadURL); 
      // create a boundary string 
      String boundary = MultiPartFormOutputStream.createBoundary(); 
      URLConnection urlConn = MultiPartFormOutputStream.createConnection(url); 
      urlConn.setReadTimeout(15000); 
      urlConn.setRequestProperty("Accept", "*/*"); 
      urlConn.setRequestProperty("Content-Type", MultiPartFormOutputStream.getContentType(boundary)); 
      // set some other request headers... 
      urlConn.setRequestProperty("Connection", "Keep-Alive"); 
      urlConn.setRequestProperty("Cache-Control", "no-cache"); 
      // no need to connect because getOutputStream() does it 
      MultiPartFormOutputStream out = new MultiPartFormOutputStream(urlConn.getOutputStream(), boundary); 
out.writeField("param", "value") 
out.writeFile("myFile", "multipart/form-data", "content.html", htmlData); 
     out.close(); 
在upload.java

然後

public class Upload extends HttpServlet 
{ 

    private BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService(); 

    public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException 
    { 
     UserService userService = UserServiceFactory.getUserService(); 
     User user = userService.getCurrentUser(); 
     Map<String, BlobKey> blobs = blobstoreService.getUploadedBlobs(req); 
     BlobKey blobKey = blobs.get("myFile"); 
     String value = req.getParameter("param"); 
     Topic t = pm.getObjectById(Topic.class, key); 
     t.setParam("value"); 
    try 
    { 
     pm.makePersistent(t); 
    } 

....

爲什麼我得到encodeEmailAsUserId異常?

P.S. writeFields在數據存儲中保持並更新得很好,但我仍然得到這個異常。

回答

0

我想出瞭解決方案。問題是我從MultiPartStream代碼中提取的示例沒有爲認證cookie設置標題。

我簡單地將以下內容添加到我的中間人servlet中,該servlet將Post數據傳遞到Upload servlet。

Cookie [] cookies = req.getCookies(); 
    String name = cookies[0].getName(); 
    String value = cookies[0].getValue(); 

...

urlConn.setRequestProperty("Cookie", name + "=" + value); 
+2

注意的是,在最近更新的SDK,可以以編程方式創建的斑點,沒有采購此類請求。您可以使用文件API,如http://code.google.com/intl/pl/appengine/docs/java/blobstore/overview.html#Writing_Files_to_the_Blobstore(對於Java)中所述。 – Xion 2011-04-20 09:34:58