首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用模拟POST通过字节数组更新byte存储

使用模拟POST通过字节数组更新byte存储
EN

Stack Overflow用户
提问于 2011-04-19 10:26:59
回答 1查看 1.7K关注 0票数 0

当我试图模拟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请求的代码:

代码语言:javascript
复制
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

代码语言:javascript
复制
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在数据存储中保持并更新得很好,但是我仍然得到了这个异常。

EN

回答 1

Stack Overflow用户

发布于 2011-04-19 17:37:18

我想出了解决办法。问题是,我从其中提取的MultiPartStream代码示例没有为身份验证cookie设置标头。

我只是在中间的servlet中添加了以下内容,该servlet将Post数据传递给上传servlet。

代码语言:javascript
复制
    Cookie [] cookies = req.getCookies();
    String name = cookies[0].getName();
    String value = cookies[0].getValue();

..。

代码语言:javascript
复制
 urlConn.setRequestProperty("Cookie", name + "=" + value);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5714972

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档