当我试图模拟post请求使用Java上传到应用程序引擎Java商店时,我得到了以下异常:
警告:/_ah/登录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在数据存储中保持并更新得很好,但是我仍然得到了这个异常。
发布于 2011-04-19 17:37:18
我想出了解决办法。问题是,我从其中提取的MultiPartStream代码示例没有为身份验证cookie设置标头。
我只是在中间的servlet中添加了以下内容,该servlet将Post数据传递给上传servlet。
Cookie [] cookies = req.getCookies();
String name = cookies[0].getName();
String value = cookies[0].getValue();..。
urlConn.setRequestProperty("Cookie", name + "=" + value);https://stackoverflow.com/questions/5714972
复制相似问题