How do I post key/value pairs with a file upload in android

I know this is the repeat question.There are many similar questions are there in stack overflow but I am asking this question because I don't understand how I can post some data with Image.

I want to pass first name and last name with the image to the server. Still now i tried this.

URL connectURL;
connectURL = new URL("some URL");
File sdImageMainDirectory = new File("/sdcard");
String existingFileName = sdImageMainDirectory.toString() +"/image.jpg";

String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
String Tag="3rd";
    HttpURLConnection conn = (HttpURLConnection) connectURL.openConnection();

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

    DataOutputStream dos = new DataOutputStream( conn.getOutputStream() );

    dos.writeBytes(twoHyphens + boundary + lineEnd);
    dos.writeBytes("Content-Disposition: form-data; name=\"firstname\"" + lineEnd);

    dos.writeBytes(twoHyphens + boundary + lineEnd);
    dos.writeBytes("Content-Disposition: form-data; name=\"lastname\"" + lineEnd);

    dos.writeBytes(twoHyphens + boundary + lineEnd);
    dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + existingFileName +"\"" + lineEnd);
    dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + existingFileName +"\"" + lineEnd);

    int bytesAvailable = fileInputStream.available();
    int maxBufferSize = 1024;
    int bufferSize = Math.min(bytesAvailable, maxBufferSize);
    byte[] buffer = new byte[bufferSize];

    int bytesRead =, 0, bufferSize);

    while (bytesRead > 0) {
        dos.write(buffer, 0, bufferSize);
        bytesAvailable = fileInputStream.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        bytesRead =, 0, bufferSize);
    dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

}catch (MalformedURLException ex) {
    Log.e(Tag, "error: " + ex.getMessage(), ex);
catch (IOException ioe) {
    Log.e(Tag, "error: " + ioe.getMessage(), ioe);


Check this Code for adding additional Values to Uploading Image to Server :

public class Task_TurnoutPost extends AsyncTask<Void, Void, Void> {
        private final ProgressDialog dialog = new ProgressDialog(ActivityName.this);
        JSONObject object_feed;
        // can use UI thread here
        protected void onPreExecute() {

        protected Void doInBackground(Void... arg0) {
            // TODO Auto-generated method stub
            try {

                HttpClient httpClient = new DefaultHttpClient();
                HttpPost postRequest = new HttpPost("Your LINK");
                MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
                reqEntity.addPart("F_Name", new StringBody("F_Name"));
                reqEntity.addPart("L_Name",new StringBody("L_NAME"));
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    bitmap.compress(CompressFormat.JPEG, 75, bos);
                    byte[] data = bos.toByteArray();
                    ByteArrayBody bab = new ByteArrayBody(data, "forest.jpg");
                    reqEntity.addPart("picture", bab);
                }catch(Exception e){
                    //Log.v("Exception in Image", ""+e);
                    reqEntity.addPart("picture", new StringBody(""));

                HttpResponse response = httpClient.execute(postRequest);
                BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
                String sResponse;
                StringBuilder s = new StringBuilder();
                while ((sResponse = reader.readLine()) != null) {
                    s = s.append(sResponse);
                object_feed = new JSONObject(s.toString());
                Log.v("Response for POst", s.toString());
            } catch (Exception e) {
                Log.e("e.getClass().getName()", ""+e);
            return null;

        protected void onPostExecute(Void result) {
            if (this.dialog.isShowing()) {


Where bitmap is your as Bitmap. In addition to it add a library and download it

Check this for you reference

Need Your Help

How to preserve the ModelMap between form submits?

java web-applications spring-mvc

I am adding to the ModelMap a Map which contains a list to go on a drop down an I am populating the ModelAttribute when the form initialises:

InterlockedExchange on two CPU cores

windows kernel interlocked

I have a Windows 7 driver where I want to synchronize access to a variable. Can I use InterlockedExchange for it?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.