I am using MultipartEntityBuilder and i want to send image on server. I have image Uri. Image may be local or not, so i get input stream and send it this way:
HttpClient httpclient = new DefaultHttpClient();
JSONObject result;
HttpPost httppost = new HttpPost("http://www.ezduki.ru/api/content/add/image/");
InputStream iStream = con.getContentResolver().openInputStream(imageUri);
MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntity.addTextBody("token", code);
multipartEntity.addBinaryBody("file", iStream);
httppost.setEntity(multipartEntity.build());
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
result = new JSONObject(EntityUtils.toString(entity));
where con is my main activity context (code runs in AsyncTask). I am trying to send local file, and i get error from web server in result, here is log from web server:
[Fri Dec 13 10:01:03 2013] [error] [client 93.157.241.232] (70014)End of file found: mod_wsgi (pid=28449): Unable to get bucket brigade for request. [Fri Dec 13 15:01:03 2013] [error] ERROR:django.request:Internal Server Error: /api/content/add/image/ [Fri Dec 13 15:01:03 2013] [error] Traceback (most recent call last): [Fri Dec 13 15:01:03 2013] [error] File "/var/www/ezduki/venv/lib/python2.6/site-packages . . . packages/django/core/handlers/wsgi.py", line 92, in _read_limited [Fri Dec 13 15:01:03 2013] [error] result = self.stream.read(size) [Fri Dec 13 15:01:03 2013] [error] IOError: request data read error [Fri Dec 13 15:01:03 2013] [error] [client 93.157.241.232] mod_wsgi (pid=28709): Exception occurred processing WSGI script '/var/www/ezduki/app/wsgi.py'. [Fri Dec 13 15:01:03 2013] [error] [client 93.157.241.232] IOError: failed to write data
How must i use MultipartEntityBuilder and InputStream together? Sending file like this
File f = new File(filePath);
multipartEntity.addPart("file", new FileBody(f));
works perfectly
While sending images alone with text ,we should mention the file type..
Here is implementation
Did not find direct solution. Do it this way know:
maybe you have already found out. But I think you can use:
if you send the file by stream,you just only can get the file by InputStream of Request on server,i'm use the dot net server,you can reference my code.
1,the client way
server receive
2,you want way by stream
the client
receive server
Did you try with MultiparEntity? See this code and try
As you see below, attachImage is a bitmap and I transform in Bytes then i try to send a multipart to the server