Reputation: 521
One feature of an Android app I am working on is to take a picture and upload it via HTTP Post to a Java servlet. I have found a number of examples of how the general process should work, and tried all of them. Currently, the code looks like this:
Client:
String fileName = pathToFile;
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost method = new HttpPost(path);
MultipartEntity entity = new MultipartEntity();
FileBody fileBody = new FileBody(new File(fileName));
entity.addPart("file", fileBody);
method.setEntity(entity);
HttpResponse response = httpclient.execute(method);
Server:
List<FileItem> fileItems =
new ServletFileUpload( new DiskFileItemFactory( 1024 * 1024, new File("C:\\tmp" ))).
parseRequest(request);
for ( FileItem item : fileItems ) {
String fieldName = item.getFieldName();
if ( item.isFormField()) {
item.getString();
}
else {
item.getInputStream();
} // File uploaded
}
Right now, I am not worried about what to do with the input stream, because this code fails when we call parseRequest(request) with this error:
org.apache.tomcat.util.http.fileupload.MultipartStream$MalformedStreamException: Stream ended unexpectedly
I am a new user of the Apache HttpUpload library. What am I missing here?
Thanks in advance.
Upvotes: 2
Views: 875
Reputation: 72311
Try using the constructor :
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
The default is using mode STRICT and this may be the problem.
Upvotes: 1