0

In my application I need upload a image on server with a string value.

Below is the code I am using to achieve this:-

// open a URL connection to the Servlet
                FileInputStream fileInputStream = new FileInputStream(
                        sourceFile);
                URL url = new URL(url_profile_path);

                // Open a HTTP connection to the URL
                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);
                conn.setRequestProperty("unique_id",          String.valueOf(Globals.list_user.get(0).id));

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

                dos.writeBytes(twoHyphens + boundary + lineEnd);
                dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""
                        + fileName + "\"" + 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("Content-Disposition: form-data; name=\"unique_id\";String.valueOf(Globals.list_user.get(0).id)=\""
                        + String.valueOf(Globals.list_user.get(0).id) + "\"" + lineEnd);
                dos.writeBytes(lineEnd);
                dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

                // Responses from the server (code and message)
                serverResponseCode = conn.getResponseCode();
                String serverResponseMessage = conn.getResponseMessage();

                Log.i("uploadFile", "HTTP Response is : "
                        + serverResponseMessage + ": " + serverResponseCode);

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

                StringBuilder response = new StringBuilder();
                char[] b = new char[512];
                int read;
                while ((read = bufferedReader.read(b)) != -1) {
                    response.append(b, 0, read);
                }
                Log.d("MultipartServer", response.toString());


                // close the streams //
                fileInputStream.close();
                dos.flush();
                dos.close();

Using this code image get uploaded perfectly but it is not sending the another parameter tagged as 'unique_id' whose value is 'String.valueOf(Globals.list_user.get(0).id)'.

Will anybody please explain me what should I do to achieve it? Thanks in advance.

Vinay Raut
  • 119
  • 2
  • 10

0 Answers0