App Engine and commons FileUpload

帅比萌擦擦* 提交于 2019-12-10 21:09:22

问题


I'm sending a message built with google's protocol buffer from and android device using this code:

// 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) {

}

Eventually this code will actually be sending more than one part...

I have a servlet running on Google's app engine that receives this post request and at the moment only has the following code:

  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);
    }
  }

Obviously the server doesn't do very much right now! But I have noticed that it seems to receive two parts both called "message" and file "sdroidmsg", which I really don't understand. Surely it should only receive this once? I thought perhaps that sdroidmsg might be being split into two part because of size, but that is a complete guess, I don't really know the inner working of what's happening behind the scenes. Could anyway explain why this is happening? I can post more code if required.


回答1:


You call client.execute() twice.

Are you sure you're not POSTing and receiving two separate requests, rather than the fields being sent up twice in a single POST?



来源:https://stackoverflow.com/questions/2499954/app-engine-and-commons-fileupload

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!