user3669034 user3669034 - 6 months ago 84
Android Question

Upload Image to server using base64 encoding android

I am trying to upload image string to server but every time I uploaded it gives a message nothing receive from your side (Means empty string). I also checked the image string after encoding but still I can't upload the string of the image. Could you please help me?

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.util.Log;

public class UploadImages {
String _responseMain;
private int serverResponseCode;

public String upLoad2Server(String encode) {


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;



try {
Log.d("encodedImage", encode);
URL url = new URL(ConfigImages.UPLOAD_URL);
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setChunkedStreamingMode(1024);



conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("ENCTYPE", "multipart/form-data");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
conn.setRequestMethod("POST");
conn.connect();
conn.connect();



OutputStream outputStream = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
writer.write(encode);

writer.close();
outputStream.close();

//Read
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));

String line = null;
StringBuilder sb = new StringBuilder();

while ((line = bufferedReader.readLine()) != null) {
System.out.println("Uploading............");
sb.append(line);
}

bufferedReader.close();
_responseMain = sb.toString();
System.out.println("Response of Image Upload : " +_responseMain);


} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return _responseMain ;





}
}

Answer

You are not sending a parameter name like

 {"image_code":"AAARoABQAAAAEAAAEe"}
Comments