【问题标题】:App Engine and commons FileUploadApp Engine 和公共文件上传
【发布时间】:2011-09-04 23:21:12
【问题描述】:

我正在使用以下代码从 android 设备发送使用 google 协议缓冲区构建的消息:

// Set up the HttpClient
HttpClient client = new DefaultHttpClient();
String url = "http://192.168.2.103:8888/sdroidmarshal";
HttpPost postRequest = new HttpPost(url);

// Create the content for the message
AbstractContentBody[] parts = new AbstractContentBody[1];
InputStream ins = new ByteArrayInputStream(offers.build().toByteArray());
parts[0] = new InputStreamBody(ins, "sdroidmsg");

// Add the content to the message
MultipartEntity requestContent = new MultipartEntity();
requestContent.addPart("message", parts[0]);

// Send!
postRequest.setEntity(requestContent);
client.execute(postRequest);

try {
  ResponseHandler<String> responseHandler = new BasicResponseHandler();
  String responseBody = client.execute(postRequest, responseHandler);

} catch (Throwable t) {

}

最终这段代码实际上会发送不止一个部分...

我有一个在 Google 的应用引擎上运行的 servlet,它接收这个发布请求,目前只有以下代码:

  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {

    super.doPost(req, resp);

    try {
      ServletFileUpload upload = new ServletFileUpload();
      resp.setContentType("text/plain");

      FileItemIterator iterator = upload.getItemIterator(req);
      while (iterator.hasNext()) {
        FileItemStream item = iterator.next();
        InputStream stream = item.openStream();

        if (item.isFormField()) {
          log.warning("Got a form field: " + item.getFieldName());
        } else {
          log.warning("Got an uploaded file: " + item.getFieldName() +
                      ", name = " + item.getName());

        }
      }
    } catch (Exception ex) {
      throw new ServletException(ex);
    }
  }

显然,服务器现在并没有做太多事情!但是我注意到它似乎收到了两个部分,分别称为“消息”和文件“sdroidmsg”,我真的不明白。当然它应该只收到一次?我想也许 sdroidmsg 可能因为大小而被分成两部分,但这是一个完整的猜测,我真的不知道幕后发生的事情的内部工作。无论如何可以解释为什么会这样吗?如果需要,我可以发布更多代码。

【问题讨论】:

    标签: java google-app-engine file-upload protocol-buffers


    【解决方案1】:

    你调用了 client.execute() 两次。

    您确定不是 POST 和接收两个单独的请求,而不是在一个 POST 中将字段发送两次?

    【讨论】:

      猜你喜欢
      • 2011-02-12
      • 2013-11-25
      • 2010-11-01
      • 2010-10-24
      • 2014-12-10
      • 1970-01-01
      • 2014-01-18
      • 1970-01-01
      • 2010-09-10
      相关资源
      最近更新 更多