Google App Engine-使用模拟的POST通过字节数组更新blobstore
||
当我尝试模拟使用Java上传到应用程序引擎Blobstore的发布请求时,出现以下异常:
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的异常?
附言writeFields持久存在并且在数据存储区中更新得还不错,但是我仍然遇到此异常。
没有找到相关结果
已邀请:
1 个回复
吠强祷豪硅
...