Multiple image upload in single multipart request with String Data Android - android

I'm Working on used goods sell Android application where I want to upload multiple images of goods on server.User can upload maximum 4 images of and at least he has to upload one image of goods.The number of goods images may vary between 1 to 4,it depends on user what number of images he wants to upload.
Now my question is that how can I upload multiple images with String data to server in a single multipart request
Condition
the number of images upload is vary between 1 to 4 images.
Below is my codes for uploading single image to server with string data.
try {
if (selectedImage.equals(null)) {
Toast.makeText(MainActivity.this, "Choose Image First", Toast.LENGTH_LONG).show();
} else {
tvLoad.setVisibility(View.VISIBLE);
Map<String, String> params = new HashMap<String, String>();
params.put("user_id", "34242");
params.put("user_city", "Delhi");
params.put("category", "vehicle");
params.put("subcategory", "car");
params.put("brand", "honda");
params.put("model", "2019");
params.put("fuel", "petrol");
params.put("conditn", "good");
params.put("title", "title");
params.put("description", "description");
params.put("year", "2018");
params.put("kmdriven", "90000");
params.put("price", "100000");
Log.e("abc", " =============" + link);
try {
multipartRequest(link, params, selectedImage + "", "image", "image/jpg");
} catch (Exception e) {
e.printStackTrace();
}
}
}catch (Exception e){
e.printStackTrace();
Toast.makeText(MainActivity.this, "Choose Image First", Toast.LENGTH_LONG).show();
}
public void multipartRequest(String urlTo, Map<String, String>
parmas, String filepath, String filefield, String fileMimeType) throws
Exception {
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
InputStream inputStream = null;
String twoHyphens = "--";
String boundary = "*****" +
Long.toString(System.currentTimeMillis()) + "*****";
String lineEnd = "\r\n";
String result = "";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
String[] q = filepath.split("/");
int idx = q.length - 1;
try {
File file = new File(filepath);
FileInputStream fileInputStream = new FileInputStream(file);
URL url = new URL(urlTo);
connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("User-Agent", "Android Multipart HTTP Client 1.0");
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"" + filefield + "\"; filename=\"" + q[idx] + "\"" + lineEnd);
outputStream.writeBytes("Content-Type: " + fileMimeType + lineEnd);
outputStream.writeBytes("Content-Transfer-Encoding: binary" + lineEnd);
outputStream.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
outputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
outputStream.writeBytes(lineEnd);
// Upload POST Data
Iterator<String> keys = parmas.keySet().iterator();
while (keys.hasNext()) {
String key = keys.next();
String value = parmas.get(key);
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"" + key + "\"" + lineEnd);
outputStream.writeBytes("Content-Type: text/plain" + lineEnd);
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(value);
outputStream.writeBytes(lineEnd);
}
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
if (200 != connection.getResponseCode()) {
throw new Exception("Failed to upload code:" + connection.getResponseCode() + " " + connection.getResponseMessage());
}
inputStream = connection.getInputStream();
result = this.convertStreamToString(inputStream);
fileInputStream.close();
inputStream.close();
outputStream.flush();
outputStream.close();
JSONObject jsonObject = new JSONObject(result);
if(jsonObject.getString("success").equals("true")){
Toast.makeText(MainActivity.this, "Service Added", Toast.LENGTH_LONG).show();
}else{
}
tvLoad.setText("Successfully loaded");
Log.e("abc", " ========= result === " + result) ;
try {
JSONObject jsonObject1 = new JSONObject(result);
String link = jsonObject1.getString("link");
tvLink.setText(link);
}catch (Exception e){
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}

I recommend my lightweight library with I have described in this answer https://stackoverflow.com/a/53253933/8849079

Related

How to send File and String data using HttpURLConnection in android

I am using HttpURLConnection to upload a file to the server but i could not figure out how can i add other parameters to send String data along with file.
This is the code i am using to upload the file.
public String sendFileToServer(String filename, String targetUrl) {
String response = "error";
Log.e("Image filename", filename);
Log.e("url", targetUrl);
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
// DataInputStream inputStream = null;
String pathToOurFile = filename;
String urlServer = targetUrl;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
DateFormat df = new SimpleDateFormat("yyyy_MM_dd_HH:mm:ss");
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024;
try {
FileInputStream fileInputStream = new FileInputStream(new File(
pathToOurFile));
URL url = new URL(urlServer);
connection = (HttpURLConnection) url.openConnection();
// Allow Inputs & Outputs
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setChunkedStreamingMode(1024);
// Enable POST method
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
String connstr = null;
connstr = "Content-Disposition: form-data; name=\"uploadedfile\";filename=\""
+ pathToOurFile + "\"" + lineEnd;
Log.i("Connstr", connstr);
outputStream.writeBytes(connstr);
outputStream.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// Read file
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
Log.e("Image length", bytesAvailable + "");
try {
while (bytesRead > 0) {
try {
outputStream.write(buffer, 0, bufferSize);
} catch (OutOfMemoryError e) {
e.printStackTrace();
response = "outofmemoryerror";
return response;
}
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
} catch (Exception e) {
e.printStackTrace();
response = "error";
return response;
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens
+ lineEnd);
// Responses from the server (code and message)
int serverResponseCode = connection.getResponseCode();
String serverResponseMessage = connection.getResponseMessage();
Log.i("Server Response Code ", "" + serverResponseCode);
Log.i("Server Response Message", serverResponseMessage);
if (serverResponseCode == 200) {
response = "true";
}
String CDate = null;
Date serverTime = new Date(connection.getDate());
try {
CDate = df.format(serverTime);
} catch (Exception e) {
e.printStackTrace();
Log.e("Date Exception", e.getMessage() + " Parse Exception");
}
Log.i("Server Response Time", CDate + "");
filename = CDate
+ filename.substring(filename.lastIndexOf("."),
filename.length());
Log.i("File Name in Server : ", filename);
fileInputStream.close();
outputStream.flush();
outputStream.close();
outputStream = null;
} catch (Exception ex) {
// Exception handling
response = "error";
Log.e("Send file Exception", ex.getMessage() + "");
ex.printStackTrace();
}
return response;
}
I have used Volley previously but it fails to upload when file is large, I have to upload Video files up to 20 MB, so if you have any better solution please let me know.
I am using this code in Service so even if user close the app the uploading should continue.
The following code should work:
outputStream = new DataOutputStream(connection.getOutputStream());
// To send string data
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"" + stringName + "\""+lineEnd);
outputStream.writeBytes("Content-Type: text/plain; charset=UTF-8" + lineEnd);
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(stringValue + lineEnd);
outputStream.flush();
// To send file data
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile + "\"" + lineEnd);
outputStream.writeBytes(lineEnd);
For more details: link

how to add token in httpurlconnection class

i am using this code for API access.But i have no idea how to add token as header in httpurlconnection class.my tag for token is "token".I want to add "token" tag with token value in header field but i have no idea.i have googled but i am confused
public JSONObject makeHttpRequest(String murl, Utils.method m, Map<String, String> parmas, String Token, String filepath, String filefield, String filemimetype){
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
InputStream inputStream = null;
String twoHyphens = "--";
String boundary = "*****" + Long.toString(System.currentTimeMillis()) + "*****";
String lineEnd = "\r\n";
String urlTo = SERVER_PATH + murl;
String result = "";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
try {
URL url = new URL(urlTo);
connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
// connection.setRequestProperty("User-Agent", "Android Multipart HTTP Client 1.0");
connection.setRequestProperty("Cache-Control", "no-cache");
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
if (Token != null){
connection.setRequestProperty("token", "Basic " + new String(Base64.encode(Token.getBytes(), 0)));
}
outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
if (filepath != null) {
String[] q = filepath.split("/");
int idx = q.length - 1;
File file = new File(filepath);
FileInputStream fileInputStream = new FileInputStream(file);
outputStream.writeBytes("Content-Disposition: form-data; name=\"" + filefield + "\"; filename=\"" + q[idx] + "\"" + lineEnd);
outputStream.writeBytes("Content-Type: " + filemimetype + lineEnd);
outputStream.writeBytes("Content-Transfer-Encoding: binary" + lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
outputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
outputStream.writeBytes(lineEnd);
}
// outputStream.writeBytes(getQuery(parmas));
Iterator<String> keys = parmas.keySet().iterator();
while (keys.hasNext()) {
String key = keys.next();
String value = parmas.get(key);
System.out.println("Key :"+key+" and Value :"+value);
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"" + key + "\"" + lineEnd);
outputStream.writeBytes("Content-Type: text/plain" + lineEnd);
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(value);
outputStream.writeBytes(lineEnd);
}
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
System.out.println("Response Code :"+connection.getResponseCode());
if (200 != connection.getResponseCode()) {
}
inputStream = connection.getInputStream();
result = this.convertStreamToString(inputStream);
jobj = new JSONObject(result);
inputStream.close();
outputStream.flush();
outputStream.close();
connection.disconnect();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return jobj;
}

POST a file with other form data using HttpURLConnection

Im trying to POST a file to an API of mine along with other parameters.
Eg. POST /media
with the parameters
filename = 'test.png'
file = -the-actual-file-
I can do this successfully with Postman (using form-data), so the api side of things are fine.
Here is my android code using HttpURLConnection:
nameValuePairs.add(new BasicNameValuePair("filename", "test.png"));
URL object = new URL(url);
HttpURLConnection connection = (HttpURLConnection) object.openConnection();
connection.setReadTimeout(60 * 1000);
connection.setConnectTimeout(60 * 1000);
String auth = username+":"+password;
byte[] data = auth.getBytes();
String encodeAuth = "Basic " + Base64.encodeToString(data, Base64.DEFAULT);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestProperty("Authorization", encodeAuth);
connection.setRequestProperty("Accept", ACCEPT);
connection.setRequestMethod("POST");
connection.setRequestProperty("ENCTYPE", "multipart/form-data");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
dataOutputStream = new DataOutputStream(connection.getOutputStream());
writer = new BufferedWriter(new OutputStreamWriter(dataOutputStream, "UTF-8"));
writer.write(getQuery(nameValuePairs));
writer.write("&file=" + "image.jpg");
writer.write
File file = getFile(item);
if (file == null) {
Log.e("uploadFile", "Source File not exist " );
} else {
addFilePart("file", file);
}
}
writer.flush();
writer.close();
dataOutputStream.close();
connection.connect();
Android multipart upload.
public String multipartRequest(String urlTo, Map<String, String> parmas, String filepath, String filefield, String fileMimeType) throws CustomException {
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
InputStream inputStream = null;
String twoHyphens = "--";
String boundary = "*****" + Long.toString(System.currentTimeMillis()) + "*****";
String lineEnd = "\r\n";
String result = "";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
String[] q = filepath.split("/");
int idx = q.length - 1;
try {
File file = new File(filepath);
FileInputStream fileInputStream = new FileInputStream(file);
URL url = new URL(urlTo);
connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("User-Agent", "Android Multipart HTTP Client 1.0");
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"" + filefield + "\"; filename=\"" + q[idx] + "\"" + lineEnd);
outputStream.writeBytes("Content-Type: " + fileMimeType + lineEnd);
outputStream.writeBytes("Content-Transfer-Encoding: binary" + lineEnd);
outputStream.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
outputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
outputStream.writeBytes(lineEnd);
// Upload POST Data
Iterator<String> keys = parmas.keySet().iterator();
while (keys.hasNext()) {
String key = keys.next();
String value = parmas.get(key);
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"" + key + "\"" + lineEnd);
outputStream.writeBytes("Content-Type: text/plain" + lineEnd);
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(value);
outputStream.writeBytes(lineEnd);
}
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
if (200 != connection.getResponseCode()) {
throw new CustomException("Failed to upload code:" + connection.getResponseCode() + " " + connection.getResponseMessage());
}
inputStream = connection.getInputStream();
result = this.convertStreamToString(inputStream);
fileInputStream.close();
inputStream.close();
outputStream.flush();
outputStream.close();
return result;
} catch (Exception e) {
logger.error(e);
throw new CustomException(e);
}
}
private String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
Calling code:
//setup params
Map<String, String> params = new HashMap<String, String>(2);
params.put("foo", hash);
params.put("bar", caption);
String result = multipartRequest(URL_UPLOAD_VIDEO, params, pathToVideoFile, "video", "video/mp4");
//next parse result string
Ref Link https://stackoverflow.com/a/26145565/1143026
Accepted answer needs a small update if you are using nanoHttpd for server end., as this issue took a lot of debugging time.. and had to use the below timeline source code from insomnia to understand the problem.
just avoid using (//comment out) outputStream.writeBytes("Content-Type: text/plain" + lineEnd);
as this ends up setting the post parameters to null

Android upload removes bytes

I'm trying to upload a video from my android phone (Samsung Galaxy s2) with version 4.1.2.
I have configured an apache server which hosts the uploaded video. My problem is the following : everytime I upload a video from my phone, I have a corrupted file. The file on the server has a few bytes less than the phone's file. It does not come from the server as I've made some tests using an html form and everything went well. So I guess, it's coming from my code but I just can't figure where. Can somebody help me please, I've spent days on it.
This is my code :
#Override
public boolean uploadFile() {
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
String pathToOurFile = mMedia.getLocalPath();
String urlServer = UploadConf.UPLOAD_SERVER;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize, totalBytesRead;
byte[] buffer;
int maxBufferSize = UploadConf.MAX_BUFFER_SIZE;
try {
File file = new File(pathToOurFile);
FileInputStream fileInputStream = new FileInputStream(file);
Log.i(LOG_TAG, "MediaUploader upload" + file.getName() + " => " + file.getPath());
Map<String, String> params = new HashMap<String, String>();
params.put("mediaTitle", file.getName());
URL url = new URL(urlServer + getFormattedParams(params));
Log.i(LOG_TAG, "URL => " + urlServer + getFormattedParams(params));
connection = (HttpURLConnection) url.openConnection();
// Allow Inputs & Outputs
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setChunkedStreamingMode(UploadConf.CHUNK_SIZE);
// Enable POST method
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
outputStream = new DataOutputStream( connection.getOutputStream() );
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"mediaFile\"" + lineEnd + lineEnd + file.getName() + lineEnd);
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"video_file\";filename=\"" + file.getName());
outputStream.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// Read file
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
totalBytesRead = bytesRead;
while (bytesRead > 0)
{
outputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
Log.i(LOG_TAG, "Progress " + totalBytesRead + " Read ");
totalBytesRead += bytesRead;
//outputStream.flush();
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
//Get Response
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
// Responses from the server (code and message)
int serverResponseCode = connection.getResponseCode();
String serverResponseMessage = connection.getResponseMessage();
Log.d(LOG_TAG,"Server Response Code " + serverResponseCode);
Log.d(LOG_TAG,"Server Response Message " + serverResponseMessage);
Log.d(LOG_TAG,"Server Response " + response.toString());
String result = response.toString();
if (result == null || result.equals("")) {
broadcastIntent(mContext.getString(R.string.upload_failed_notification), PRIORITY.ERROR, fileInputStream, outputStream);
}
JSONObject json = null;
try {
json = new JSONObject(result);
}
catch (JSONException e) {
Log.e(LOG_TAG, "Impossible to parse Json String");
broadcastIntent(mContext.getString(R.string.upload_failed_notification), PRIORITY.ERROR, fileInputStream, outputStream);
return false;
}
JSONObject jsonResult = json.getJSONObject(Constants.JSON_RESULT_TAG);
if (jsonResult == null) {
Log.e(LOG_TAG, "Json result is null ");
broadcastIntent(mContext.getString(R.string.upload_failed_notification), PRIORITY.ERROR, fileInputStream, outputStream);
return false;
}
JSONArray errors;
if (jsonResult.has(Constants.JSON_ERRORS_TAG)) {
if ((errors = jsonResult.getJSONArray(Constants.JSON_ERRORS_TAG)) != null) {
String notification = "";
for (int i = 0; i < errors.length(); i++) {
JSONObject o = errors.getJSONObject(i);
notification += o.getString(Constants.JSON_ERROR_TAG);
}
broadcastIntent(notification, PRIORITY.ERROR, null, null);
return false;
}
}
mMedia.setSyncState(SyncState.SYNCED.ordinal());
mMediaManager.updateMedia(mMedia);
broadcastIntent(mContext.getString(R.string.upload_succeedeed_notification), PRIORITY.INFO, fileInputStream, outputStream);
return true;
} catch (UnknownHostException ex) {
broadcastIntent(mContext.getString(R.string.upload_hostname_error_notification), PRIORITY.ERROR, null, null);
Log.d(LOG_TAG, ex.getStackTrace().toString());
} catch (Exception ex) {
broadcastIntent(mContext.getString(R.string.upload_failed_notification), PRIORITY.ERROR, null, null);
Log.d(LOG_TAG,"Upload failed");
ex.printStackTrace();
}
return false;
}

Android HTTP POST file upload not working with HttpUrlConnection

I am trying to upload a file to a server via HTTP POST from my device. I have two method upload1 and upload2.
Upload1 uses the HttpPost class and it works, but with bigger files it throws out of memory exception.
Upload2 uses HttpURLConnection and it does not work. (I get BAD REQUEST message from the server.) I want upload2 to work, because it uses stream to send the data and throws no out of memory exception.
I looked at the packages in wireshark, the headers are seems to be the same, however the length with upload1 is 380, with upload2 is only 94. What could be the problem?
private void upload1(File file) {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url + "?recname="
+ fileName);
// ///////////////////////////////////////
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "---------------------------This is the boundary";
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
FileInputStream fin = new FileInputStream(file);
byte audioData[] = new byte[(int) file.length()];
fin.read(audioData);
fin.close();
// Send a binary file
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"file\";filename=\""
+ fileName + "\"" + lineEnd);
dos.writeBytes("Content-Type: audio/mp4" + lineEnd);
dos.writeBytes("Content-Transfer-Encoding: binary" + lineEnd);
dos.writeBytes(lineEnd);
dos.write(audioData);
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
dos.flush();
dos.close();
ByteArrayInputStream content = new ByteArrayInputStream(
baos.toByteArray());
BasicHttpEntity entity = new BasicHttpEntity();
entity.setContent(content);
entity.setContentLength(baos.toByteArray().length);
httppost.addHeader("Content-Type", "multipart/form-data; boundary="
+ boundary);
httppost.setEntity(entity);
// //////////////////////////////////
HttpResponse response = httpclient.execute(httppost);
BufferedReader reader = new BufferedReader(new InputStreamReader(
response.getEntity().getContent(), "UTF-8"));
StringBuilder builder = new StringBuilder();
for (String line = null; (line = reader.readLine()) != null;) {
builder.append(line).append("\n");
}
reader.close();
message = builder.toString();
System.out.println(message);
} catch (UnknownHostException e) {
message = "Error! Server is unreachable. Check you internet connection!";
} catch (Exception e) {
message = "error: " + e.toString();
}
}
/////////////////////////////////////////////////////////////////////////////////////////
private void upload2(File file) {
HttpURLConnection connection = null;
String pathToOurFile = file.getPath();
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "---------------------------This is the boundary";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
try {
fileInputStream = new FileInputStream(new File(pathToOurFile));
URL server_url = new URL(url+ "?recname="
+ fileName);
connection = (HttpURLConnection) server_url.openConnection();
// Allow Inputs & Outputs
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
// Enable POST method
connection.setRequestMethod("POST");
String bodyHeader = twoHyphens
+ boundary
+ lineEnd
+ "Content-Disposition: form-data; name=\"file\";filename=\""
+ fileName + "\"" + lineEnd + "Content-Type: audio/mp4"
+ lineEnd + "Content-Transfer-Encoding: binary" + lineEnd
+ lineEnd + twoHyphens + boundary + twoHyphens ;
byte[] bodyHeaderAray = bodyHeader.getBytes();
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
connection.setRequestProperty("Expect", "100-continue");
// Content-Length
int bodyHeaderSize = (int) file.length() + bodyHeaderAray.length;
System.out.println("body header size: " + bodyHeaderSize);
// connection.setFixedLengthStreamingMode(bodyHeaderSize);
outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream
.writeBytes("Content-Disposition: form-data; name=\"file\";filename=\""
+ fileName + "\"");
outputStream.writeBytes(lineEnd);
outputStream.writeBytes("Content-Type: audio/mp4" + lineEnd);
outputStream.writeBytes("Content-Transfer-Encoding: binary"
+ lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// Read file
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
outputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens
+ lineEnd);
fileInputStream.close();
outputStream.flush();
outputStream.close();
// Responses from the server (code and message)
int serverResponseCode = connection.getResponseCode();
String serverResponseMessage = connection.getResponseMessage();
message = serverResponseMessage;
} catch (Exception ex) {
System.out.println(ex);
}
}
According to the Problems you are getting to upload the files. you should use Multipart mechanism to upload the files to server.
httpclient-4.1.jar
httpcore-4.1.jar
httpmime-4.1.jar
apache-mime4j-0.6.1.jar
For that you need to add couple of libraries into your project and use it for HTTP connection and file data.
private boolean uploadFile(File mFile) {
boolean success = true;
String filename = mFile.getName();
MultipartEntity data_to_send = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
try {
data_to_send.addPart(
"name",
new StringBody(
filename.substring(filename.lastIndexOf("/") + 1)));
data_to_send.addPart("fileData", new FileBody(mFile));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
String responseData = ConsumeWebService.sendRequest(data_to_send,
Global.BASE_URL + serviceUrl);
if (TextUtils.isEmpty(responseData)
|| responseData.equals(ConsumeWebService.ERROR_CODE)) {
success = false;
}
} catch (Exception e) {
success = false;
e.printStackTrace();
}
return success;
}
public static String sendRequest(MultipartEntity data, String url) {
String response = "";
response = postData(url, data);
return response;
}
private static String postData(String url, MultipartEntity data) {
String strResponse = "";
try {
Log.d(Global.TAG, "Post URL is " + url);
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(data);
strResponse = httpClient.execute(httpPost,
new BasicResponseHandler());
} catch (UnsupportedEncodingException e) {
strResponse = ERROR_CODE;
e.printStackTrace();
} catch (ClientProtocolException e) {
strResponse = ERROR_CODE;
e.printStackTrace();
} catch (IOException e) {
strResponse = ERROR_CODE;
e.printStackTrace();
}
return strResponse;
}
You can build by your own your POST Request by following this w3.org docs about forms.
I think in upload2 you are missing one lineEnd where you do:
....
outputStream.writeBytes("Content-Transfer-Encoding: binary"
+ lineEnd);
After this, you have to transmit the data and it takes two lineEnds before actual data, so it should be:
....
outputStream.writeBytes("Content-Transfer-Encoding: binary"
+ lineEnd + lineEnd);
For reading files and place them in multipart/form-data structure, I suggest you this way that works for me:
FileInputStream fileInputStream=new FileInputStream(file);
byte[] bytes= new bytes[fileInputStream.getChannel().size()];
fileInputStream.read(bytes);
fileInputStream.close();
outputStream.write(bytes);
outputStream.writeBytes(lineEnd);
Hope it helps.

Categories

Resources