How to post large image using base64 in android? - android

Currently I am working one project which requires to post the data on the server database.
Here I post the data like name, id, image, etc.
I want to pass the image as a binary data. For that I am converting the image into a base64 string and then POST-ing the data,
but due to the large image resolution or size HTTP connection, I get the 413 error code which tells that the URL is to large.
Please can anyone give me the solution for this?
Here I give you my code which I had implemented.
values
Bitmap b1 = (Bitmap) data.getExtras().get("data");
b1=Bitmap.createScaledBitmap(b1, 100,100, true);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
b1.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
String base64String = Base64.encodeBytes(b);
Log.e("Base 64 conversion of image--------", base64String);
response = webService.doNetworkConnection("{\"method\":\"EventUploadPhoto\",\"data\":{\"ClientCode\":"
+ "\""
+ Util.clientCode
+ "\""
+ ",\"EventID\":"
+ "\""
+ itemId
+ "\""
+ ",\"ImageName\":"
+ "\""
+ filename
+ System.currentTimeMillis()
+ "\""
+ ",\"ImageData\":"
+ "\""
+ imgData
+ "\""
+ ",\"UserName\":"
+ "\""
+ username
+ "\""
+ ",\"TokenId\":"
+ "\""
+ tokenid
+ "\"" + "}}", "POST");
doNetworkConnection
public String doNetworkConnection(String request, String methodType) throws IOException {
String str = java.net.URLEncoder.encode(request, "UTF-8");
String resultstring = "";
System.out.println("string request:" + request);
int response = -1;
try {
URL url = new URL((mContext.getResources()
.getString(R.string.WebServiceUrl))
+ "" + str);
System.out.println("url:" + url);
URLConnection conn = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
httpConn.connect();
response = httpConn.getResponseCode();
System.out.println("response code:" + response);
if (response == HttpURLConnection.HTTP_OK) {
InputStream is = httpConn.getInputStream();
resultstring = convertinputStreamToString(is);
} else {
resultstring = "";
}
} catch (Exception e) {
resultstring = "";
}
System.out.println("result string:" + resultstring);
return resultstring;
}
Here the HTTP response code is 413, so it tells that the URL is to large and can not POST the image.

If you're set on HTTP: Since it's just a String, you can break it up into separate Strings and POST them individually. Then concat them together on the other side.
Otherwise, you can use FTP or get lower and stream it over TCP/IP. There are Java libraries like this one.

Related

Android Java file upload to Django backend

I have tried relentlessly to create a succesfull file upload from my JAVA/Android project to Django/Python backend.
The file I am trying to upload is a wav audio file which is stored on the phone.
I am trying to mix two sets of code.
The Android code I am using is the one taken from: How to upload a WAV file using URLConnection.
public class curlAudioToWatson extends AsyncTask<String, Void, String> {
String asrJsonString="";
#Override
protected String doInBackground(String... params) {
String result = "";
try {
Log.d("Msg","**** UPLOADING .WAV to ASR...");
URL obj = new URL(ASR_URL);
HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
//conn.setRequestProperty("X-Arg", "AccessKey=3fvfg985-2830-07ce-e998-4e74df");
conn.setRequestProperty("Content-Type", "audio/wav");
conn.setRequestProperty("enctype", "multipart/form-data");
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
String wavpath=mRcordFilePath;
File wavfile = new File(wavpath);
boolean success = true;
if (wavfile.exists()) {
Log.d("Msg","**** audio.wav DETECTED: "+wavfile);
}
else{
Log.d("Msg","**** audio.wav MISSING: " +wavfile);
}
String charset="UTF-8";
String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value.
String CRLF = "\r\n"; // Line separator required by multipart/form-data.
OutputStream output=null;
PrintWriter writer=null;
try {
output = conn.getOutputStream();
writer = new PrintWriter(new OutputStreamWriter(output, charset), true);
byte [] music=new byte[(int) wavfile.length()];//size & length of the file
InputStream is = new FileInputStream (wavfile);
BufferedInputStream bis = new BufferedInputStream (is, 16000);
DataInputStream dis = new DataInputStream (bis); // Create a DataInputStream to read the audio data from the saved file
int i = 0;
copyStream(dis,output);
}
catch(Exception e){
}
conn.connect();
int responseCode = conn.getResponseCode();
Log.d("Msg","POST Response Code : " + responseCode + " , MSG: " + conn.getResponseMessage());
if (responseCode == HttpURLConnection.HTTP_OK) { //success
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
Log.d("Msg","***ASR RESULT: " + response.toString());
JSONArray jresponse=new JSONObject(response.toString()).getJSONObject("Recognition").getJSONArray("NBest");
asrJsonString=jresponse.toString();
for(int i = 0 ; i < jresponse.length(); i++){
JSONObject jsoni=jresponse.getJSONObject(i);
if(jsoni.has("ResultText")){
String asrResult=jsoni.getString("ResultText");
//ActionManager.getInstance().addDebugMessage("ASR Result: "+asrResult);
Log.d("Msg","*** Result Text: "+asrResult);
result = asrResult;
}
}
Log.d("Msg","***ASR RESULT: " + jresponse.toString());
} else {
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
Log.d("Msg","POST FAILED: " + response.toString());
result = "";
}
} catch (Exception e) {
Log.d("Msg","HTTP Exception: " + e.getLocalizedMessage());
}
return result; //"Failed to fetch data!";
}
#Override
protected void onPostExecute(String result) {
if(!result.equals("")){
Log.d("Msg","onPostEXECUTE SUCCESS, consuming result");
//sendTextInputFromUser(result);
//ActionManager.getInstance().addDebugMessage("***ASR RESULT: "+asrJsonString);
runOnUiThread(new Runnable() {
#Override
public void run() {
}
});
}else{
Log.d("Msg","onPostEXECUTE FAILED" );
}
}
}
public void copyStream( InputStream is, OutputStream os) {
final int buffer_size = 4096;
try {
byte[] bytes = new byte[buffer_size];
int k=-1;
double prog=0;
while ((k = is.read(bytes, 0, bytes.length)) > -1) {
if(k != -1) {
os.write(bytes, 0, k);
prog=prog+k;
double progress = ((long) prog)/1000;///size;
Log.d("Msg","UPLOADING: "+progress+" kB");
}
}
os.flush();
is.close();
os.close();
} catch (Exception ex) {
Log.d("Msg","File to Network Stream Copy error "+ex);
}
}
The Django backend code is taken from: https://simpleisbetterthancomplex.com/tutorial/2016/08/01/how-to-upload-files-with-django.html and I am using the simple upload:
def simple_upload(request):
if request.method == 'POST' and request.FILES['myfile']:
myfile = request.FILES['myfile']
fs = FileSystemStorage()
filename = fs.save(myfile.name, myfile)
uploaded_file_url = fs.url(filename)
return render(request, 'core/simple_upload.html', {
'uploaded_file_url': uploaded_file_url
})
return render(request, 'core/simple_upload.html')
I have already disabled the need for CSRF using #csrf_exempt.
I am getting the error "MultiValueDictKeyError" since Java does not post the file with the name 'myfile' for request.FILES['myfile'] to catch. Is have tried removing the ['myfile'] and just use request.FILES but then I get an error on
filename = fs.save(myfile.name, myfile)
saying there is no name to fetch.
Can I post the file so that it it catched by
request.FILES['myfile']
or is there better/simpler Django backend-code to use for communication with Android/IOS.
Thanks in advance and I apologize if this is a stupid question but I am dead stuck.
Here I go again answering my own question.
I found the following code from Android:How to upload .mp3 file to http server?
Using that instead of How to upload a WAV file using URLConnection and changing the line: dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + existingFileName + "\"" + lineEnd);
To dos.writeBytes("Content-Disposition: form-data; name=\"myfile\";filename=\"" + existingFileName + "\"" + lineEnd);
fixed my problem.

Bitmap does not draw after being sent from Android to Server

I have been hitting my head against a wall on this one.
In my Android App, I select an image. That all seems to be working fine. I then convert it to a byte array and HTTP POST it to a server. The data written to file on the server side does not appear to be what the data started as.
When checking things end-to-end it seems that the file on my device is 1,066,896 bytes. The data on the server is 14,745,600 bytes.
Code from Selecting the Image:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case PICK_IMAGE: {
if (resultCode != RESULT_OK)
return;
Uri selectedImage = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);
MainActivityFragment fragment = (MainActivityFragment)Fragments.Get("Main");
new SendPic().execute(bitmap);
} catch (IOException exception) {
Log.e(TAG, "Could not load image", exception);
}
break;
}
}
}
If I check bitmap.getByteCount() in the above code, it will report 14,745,600, which makes me relatively confident that the data being sent to the server is good. But I can't figure out while the data read in is 14x larger than what I show on the device. It I take this same bitmap though, and apply it to an ImageView, the image looks just fine.
SendPic is a simple class that extends AsyncTask. No magic there.
public static String SendHttpImage(String path, Bitmap bitmap, String name, String filename) {
String attachmentName = "bitmap";
String attachmentFileName = "bitmap.bmp";
String crlf = "\r\n";
String twoHyphens = "--";
String boundary = "******";
try {
String url = "http://server.com/index.php?" + path;
URL u = new URL(url);
HttpURLConnection urlConnection = (HttpURLConnection)u.openConnection();
urlConnection.setUseCaches(false);
urlConnection.setDoOutput(true);
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Connection", "Keep-Alive");
urlConnection.setRequestProperty("Cache-Control", "no-cache");
urlConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
DataOutputStream request = new DataOutputStream(urlConnection.getOutputStream());
request.writeBytes(twoHyphens + boundary + crlf);
request.writeBytes("Content-Disposition: form-data; name=\"" + attachmentName + "\";filename=\"" + attachmentFileName + "\"" + crlf);
request.writeBytes(crlf);
int bytes = bitmap.getByteCount();
ByteBuffer buffer = ByteBuffer.allocate(bytes);
bitmap.copyPixelsToBuffer(buffer);
byte[] pixels = buffer.array();
request.write(pixels);
request.writeBytes(crlf);
request.writeBytes(twoHyphens + boundary + twoHyphens + crlf);
request.flush();
request.close();
InputStream responseStream = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader responseStreamReader = new BufferedReader(new InputStreamReader(responseStream));
String line = "";
StringBuilder stringBuilder = new StringBuilder();
while ((line = responseStreamReader.readLine()) != null) {
stringBuilder.append(line).append("\n");
}
responseStreamReader.close();
String response = stringBuilder.toString();
urlConnection.disconnect();
return response;
} catch (IOException exception) {
Log.e (TAG, "HTTP Post failed", exception);
return null;
}
}
Thank you for your help with this!
Many thanks to #Colin Gillespie. I compressed the data to jpeg before sending to the server, and it came out perfect on the other side. I'm still quite confused as to why my browser couldn't display the raw bitmap, but I won't look a gift-horse in the mouth.

Sinch - Message Api not working

Always getting the Bad Request error, error code - 400. Using commons-codec-1.10 jar file but its not available for native Android. here is my code
Date date = new java.util.Date();
DateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss'Z'");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
String timestamp = dateFormat.format(date);
String httpVerb = "POST";
String path = "/v1/sms/" + "+918003059919";
String contentType = "application/json";
String canonicalizedHeaders = "x-timestamp:" + timestamp;
String body = "{\"message\":\"" + "Hiiii" + "\"}";
byte[] data = md5Digest(body);
String contentMd5 = Base64.encodeToString(data, 0, data.length,
Base64.DEFAULT);
String stringToSign = httpVerb + "\n" + contentMd5 + "\n"
+ contentType + "\n" + canonicalizedHeaders + "\n"
+ path;
String signature = signature(SinchService.APP_SECRET,
stringToSign);
String authorization = "Application " + SinchService.APP_KEY
+ ":" + signature;
URL url = new URL("https://messagingApi.sinch.com" + path);
HttpsURLConnection connection = (HttpsURLConnection) url
.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("content-type",
"application/json");
connection.setRequestProperty("x-timestamp", timestamp);
connection.setRequestProperty("authorization", Base64.encodeToString(authorization.getBytes(),Base64.DEFAULT));
OutputStream os = connection.getOutputStream();
os.write(body.getBytes());
StringBuilder response = new StringBuilder();
int status = connection.getResponseCode();
System.out.println("resonse code: "+status);
InputStream iresponse = connection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = br.readLine()) != null)
response.append(line);
br.close();
os.close();
Using android.util.Base64 package to encode but not its not working.
Sinch applications are not automatically whitelisted for sending SMS. Contact them at dev#sinch.com to get your app whitelisted.

HttpURLConnection to Send image , audio and video files with parameter may (String or Json String) Android

I'm sharing the solution to send an image , audio or a video file with parameters using HttpURLConnection. Parameters may be (plain string or JSON).
(Android Client to PHP Back end)
Scenario:
Have to upload media files (audio , video and image with parameters).
Media files will be stored in a server folder and parameters to db.
I faced a problem that, image was uploaded successfully while parameters went missing.
Potential solution found
Choosing HttpURLConnection instead of Httpclient as recommend here
You may be wondering, Which client is best?
Apache HTTP client has fewer bugs on Eclair and Froyo. It is the best choice for these releases.
For Gingerbread and better, HttpURLConnection is the best choice. Its simple API and small size makes it great fit for Android. Transparent compression and response caching reduce network use, improve speed and save battery. New applications should use HttpURLConnection; it is where we will be spending our energy going forward.
Android Code:
public int uploadFile(final String sourceFileUri) {
String fileName = sourceFileUri;
HttpURLConnection conn = null;
DataOutputStream dos = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
File sourceFile = new File(sourceFileUri);
if (!sourceFile.isFile()) {
dialog.dismiss();
Log.e("uploadFile", "Source File not exist :" + filepath);
runOnUiThread(new Runnable() {
public void run() {
messageText.setText("Source File not exist :" + filepath);
}
});
return 0;
} else {
try {
FileInputStream fileInputStream = new FileInputStream(sourceFile);
URL url = new URL(upLoadServerUri);
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true); // Allow Inputs
conn.setDoOutput(true); // Allow Outputs
conn.setUseCaches(false); // Don't use a Cached Copy
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
// conn.setRequestProperty("ENCTYPE", "multipart/form-data");
conn.setRequestProperty("Content-Type","multipart/form-data;boundary=" + boundary);
conn.setRequestProperty("uploaded_file", fileName);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
//Adding Parameter name
String name="amir";
dos.writeBytes("Content-Disposition: form-data; name=\"name\"" + lineEnd);
//dos.writeBytes("Content-Type: text/plain; charset=UTF-8" + lineEnd);
//dos.writeBytes("Content-Length: " + name.length() + lineEnd);
dos.writeBytes(lineEnd);
dos.writeBytes(name); // mobile_no is String variable
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + lineEnd);
//Adding Parameter phone
String phone="9956565656";
dos.writeBytes("Content-Disposition: form-data; name=\"phone\"" + lineEnd);
//dos.writeBytes("Content-Type: text/plain; charset=UTF-8" + lineEnd);
//dos.writeBytes("Content-Length: " + name.length() + lineEnd);
dos.writeBytes(lineEnd);
dos.writeBytes(phone); // mobile_no is String variable
dos.writeBytes(lineEnd);
//Json_Encoder encode=new Json_Encoder();
//call to encode method and assigning response data to variable 'data'
//String data=encode.encod_to_json();
//response of encoded data
//System.out.println(data);
//Adding Parameter filepath
dos.writeBytes(twoHyphens + boundary + lineEnd);
String filepath="http://192.168.1.110/echo/uploads"+fileName;
dos.writeBytes("Content-Disposition: form-data; name=\"filepath\"" + lineEnd);
//dos.writeBytes("Content-Type: text/plain; charset=UTF-8" + lineEnd);
//dos.writeBytes("Content-Length: " + name.length() + lineEnd);
dos.writeBytes(lineEnd);
dos.writeBytes(filepath); // mobile_no is String variable
dos.writeBytes(lineEnd);
//Adding Parameter media file(audio,video and image)
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""+ fileName + "\"" + lineEnd);
dos.writeBytes(lineEnd);
// create a buffer of maximum size
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// read file and write it into form...
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0)
{
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
// send multipart form data necesssary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
serverResponseCode = conn.getResponseCode();
String serverResponseMessage = conn.getResponseMessage();
Log.i("uploadFile", "HTTP Response is : "+ serverResponseMessage + ": " + serverResponseCode);
if (serverResponseCode == 200) {
runOnUiThread(new Runnable() {
public void run() {
msg = "File Upload Completed.\n\n See uploaded file here : \n\n"
+ "c:/wamp/www/echo/uploads";
messageText.setText(msg);
Toast.makeText(MainActivity.this,
"File Upload Complete.", Toast.LENGTH_SHORT)
.show();
}
});
}
// close the streams //
fileInputStream.close();
dos.flush();
dos.close();
} catch (MalformedURLException ex) {
dialog.dismiss();
ex.printStackTrace();
runOnUiThread(new Runnable() {
public void run() {
messageText
.setText("MalformedURLException Exception : check script url.");
Toast.makeText(MainActivity.this,
"MalformedURLException", Toast.LENGTH_SHORT)
.show();
}
});
Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
} catch (final Exception e) {
dialog.dismiss();
e.printStackTrace();
runOnUiThread(new Runnable() {
public void run() {
messageText.setText("Got Exception : "+e.toString());
Toast.makeText(MainActivity.this,
"Got Exception : see logcat ",
Toast.LENGTH_SHORT).show();
}
});
Log.e("Upload file to server Exception",
"Exception : " + e.getMessage(), e);
}
dialog.dismiss();
return serverResponseCode;
}
}
Php code:
$file_path = "uploads/";
//receive parameters
$name=$_POST['name'];
$phone=$_POST['phone'];
$filepath=$_POST['filepath'];
//receive media files(image , audio and video)
$file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path))
{
echo "Success";
}
Hope this helps.
Any queries ask me.
You can use Retrofit for API call. Minimum support for Android is 2.3. Please check details.
You can make it more simpler! Try the following.
public static String uploadImage(Bitmap bitmap, String urlString) {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap = Local.resize(bitmap, 512, 512);
if(filename.toLowerCase().endsWith("jpg") || filename.toLowerCase().endsWith("jpeg"))
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, bos);
if(filename.toLowerCase().endsWith("png"))
bitmap.compress(Bitmap.CompressFormat.PNG, 70, bos);
ContentBody contentPart = new ByteArrayBody(bos.toByteArray(), filename);
ContentBody body1 = new StringBody("something");
ContentBody body2 = new StringBody("something");
org.apache.http.entity.mime.MultipartEntity reqEntity = new org.apache.http.entity.mime.MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("image", contentPart);
reqEntity.addPart("sample1", body1);
reqEntity.addPart("sample2", body2);
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty("Connection", "Keep-Alive");
conn.addRequestProperty("Content-length", reqEntity.getContentLength()+"");
conn.addRequestProperty(reqEntity.getContentType().getName(), reqEntity.getContentType().getValue());
OutputStream os = conn.getOutputStream();
reqEntity.writeTo(conn.getOutputStream());
os.close();
conn.connect();
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
Log.d("UPLOAD", "HTTP 200 OK.");
return readStream(conn.getInputStream());
//This return returns the response from the upload.
} else {
Log.d("UPLOAD", "HTTP "+conn.getResponseCode()+" "+conn.getResponseMessage()+".");
String stream = readStream(conn.getInputStream());
//Log.d("UPLOAD", "Response: "+stream);
return stream;
}
} catch (Exception e) {
Log.d("UPLOAD_ERROR", "Multipart POST Error: " + e + "(" + urlString + ")");
}
return null;
}

Cannot upload a file using HttpUrlConnection

I am trying to upload a zip file to our server along with the params using UrlConnection. This is uploading the file but it is not sending the parameters.
Here is the code.
private static final String CRLF = "\r\n"; // Line separator required by multipart/form-data.
private static final String CHARSET_UTF_8 = "UTF-8";
private static final String TWO_HYPHENS = "--";
public static void uploadReport(String filePath) {
File binaryFile = new File(filePath);
String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value.
String urlString = "https://mycompany.com/api/file/uad.json";
HttpsURLConnection connection = null;
DataOutputStream dataOutoutStream = null;
try {
URL url = new URL(urlString);
connection = (HttpsURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Cache-Control", "no-cache");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
OutputStream output = connection.getOutputStream();
dataOutoutStream = new DataOutputStream(output);
// Send normal param.
writeParameter(dataOutoutStream, boundary, "app_name", "My App Name");
writeParameter(dataOutoutStream, boundary, "app_version", "2.04");
// Send binary file.
writeBinaryFile(output, dataOutoutStream, boundary, binaryFile);
// End of multipart/form-data.
dataOutoutStream.writeBytes("--" + boundary + "--" + CRLF);
Log.v(tag, "response code " + connection.getResponseCode());
} catch(FileNotFoundException e) {
e.printStackTrace();
} catch(UnsupportedEncodingException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
} finally {
if (dataOutoutStream != null)
try {
dataOutoutStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Here is the code for my writeParameter method
private static void writeParameter(DataOutputStream dataOutputStream, String boundary, String paramName, String paramValue) throws IOException {
dataOutputStream.writeBytes(TWO_HYPHENS + boundary + CRLF);
dataOutputStream.writeBytes("Content-Disposition: form-data; name=\"" + paramName + "\"" + CRLF);
dataOutputStream.writeBytes("Content-Type: text/plain; charset=" + CHARSET_UTF_8 + CRLF);
dataOutputStream.writeBytes(CRLF);
dataOutputStream.writeBytes(paramValue + CRLF);
dataOutputStream.flush();
}
Here is the writeBinaryFile method
private static void writeBinaryFile(OutputStream output, DataOutputStream dataOutputStream, String boundary, File binaryFile) throws IOException {
dataOutputStream.writeBytes(TWO_HYPHENS + boundary + CRLF);
dataOutputStream.writeBytes("Content-Disposition: form-data; name=\"binaryFile\"; filename=\"" + binaryFile.getName() + "\"" + CRLF);
dataOutputStream.writeBytes("Content-Type: " + URLConnection.guessContentTypeFromName(binaryFile.getName()) + CRLF);
dataOutputStream.writeBytes("Content-Transfer-Encoding: binary" + CRLF);
dataOutputStream.writeBytes(CRLF);
dataOutputStream.flush();
InputStream input = null;
try {
input = new FileInputStream(binaryFile);
byte[] buffer = new byte[1024];
for (int length = 0; (length = input.read(buffer)) > 0;) {
output.write(buffer, 0, length);
}
// Important! Output cannot be closed. Close of writer will close output as well.
output.flush();
} finally {
if (input != null)
input.close();
}
dataOutputStream.writeBytes(CRLF);
dataOutputStream.flush();
}
May I know what I might be doing wrong.
I did check the Stackoverflow posts here and here.

Categories

Resources