App Engine 和 Commons FileUpload
我正在使用此代码从 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,它接收此发布请求并在moment 只有以下代码:
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 可能会因为大小而被分成两部分,但这完全是猜测,我真的不知道幕后发生的事情的内部工作原理。无论如何可以解释为什么会发生这种情况吗?如果需要,我可以发布更多代码。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您调用 client.execute() 两次。
您确定您没有发布并接收两个单独的请求,而不是在单个 POST 中将字段发送两次吗?
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?