I'm sending an image from an Android phone to an AppEngine. The AppEngine is throwing content exceptions. My process of elimination has failed so I suspect I've done something larger wrong. Do I have a content type problem or a larger problem?
The AppEngine log shows the following errors depending on the content type [content type --> resulting error]:
multipart/mixed stream --> org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found the request was rejected because no multipart boundary was found
image/jpg --> org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is image/jpg
binary/octet-stream --> org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is binary/octet-stream
ANDROID CODE:
static void apachePost(File file) {
File file = new File(directory, fileName);
try {
String url = "http://chexserve.appspot.com/chexappengine";
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
FileEntity entity = new FileEntity(file, "multipart/mixed stream");
httpPost.setEntity(entity);
Log.v(Constants.CHEX,
"executing request " + httpPost.getRequestLine());
HttpResponse response = httpclient.execute(httpPost);
Log.v(Constants.CHEX, "received http response " + response);
HttpEntity resEntity = response.getEntity();
httpclient.getConnectionManager().shutdown();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
APPENGINE CODE:
public class ChexAppEngineServlet extends HttpServlet {
private static final Logger LOG = Logger
.getLogger(ChexAppEngineServlet.class.getName());
/** Processes the incoming request */
@Override
protected v开发者_如何转开发oid doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
LOG.info("LOG: received post");
System.out.println("PRINTLN: received post " + request);
try {
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator iter;
iter = upload.getItemIterator(request);
if (iter.hasNext()) {
FileItemStream imageItem = iter.next();
InputStream imgStream = imageItem.openStream();
System.out.println("PRINTLN: created imgStream " + imgStream);
// construct our entity objects
Blob imageBlob = new Blob(IOUtils.toByteArray(imgStream));
MyImage myImage = new MyImage(imageItem.getName(), imageBlob);
System.out.println("PRINTLN: created my image " + myImage);
} else {
System.out.println("PRINTLN: no file in post");
LOG.info("LOG: no file in post");
}
} catch (FileUploadException e) {
LOG.info("failed to parse post request: " + e);
e.printStackTrace();
}
}
Update:
AppEngine is still rejecting the request with "the request was rejected because no multipart boundary was found" error after I changed the Android's entity line to:
FileEntity entity = new FileEntity(file, "multipart/mixed");
According to the documentation [http://hc.apache.org/httpclient-3.x/preference-api.html],"The multipart boundary string to use in conjunction with the MultipartRequestEntity. When not set a random value will be generated for each request."
Further, there are no setter methods for the boundary.
How can I set the boundary?
The content type is "multipart/mixed", not "multipart/mixed stream". Also valid should be "multipart/form-data". If you're just uploading the image, though, you can upload it as image/jpeg, and read the raw data from the request on the server, by calling (If I recall correctly), request.getInputStream() - no need to use the Apache forms library at all.
精彩评论