将图片网址和视频网址从Android应用程序发送到php服务器

将图片网址和视频网址从Android应用程序发送到php服务器

问题描述:

In my android app UI, user can select a video from gallery, after that i am fetching the selected video uri and creating a thumbnail of the video and storing it in users sdcard and getting the uri of the image.

Now i am using the below code to send the video to the server --

//Store video to server
                FileInputStream fileInputStream = new FileInputStream(selectedFile);
                URL url = new URL(SERVER_URL+"?emailid="+uEmailID);
                connection = (HttpURLConnection) url.openConnection();
                connection.setDoInput(true);//Allow Inputs
                connection.setDoOutput(true);//Allow Outputs
                connection.setUseCaches(false);//Don't use a cached Copy
                connection.setRequestMethod("POST");
                connection.setRequestProperty("Connection", "Keep-Alive");
                connection.setRequestProperty("ENCTYPE", "multipart/form-data");
                connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
                connection.setRequestProperty("uploaded_file",selectedFilePath);

                //creating new dataoutputstream
                dataOutputStream = new DataOutputStream(connection.getOutputStream());

                //writing bytes to data outputstream
                dataOutputStream.writeBytes(twoHyphens + boundary + lineEnd);
                dataOutputStream.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""
                        + selectedFilePath + "\"" + lineEnd);

                dataOutputStream.writeBytes(lineEnd);

                //returns no. of bytes present in fileInputStream
                bytesAvailable = fileInputStream.available();
                //selecting the buffer size as minimum of available bytes or 1 MB
                bufferSize = Math.min(bytesAvailable,maxBufferSize);
                //setting the buffer as byte array of size of bufferSize
                buffer = new byte[bufferSize];

                //reads bytes from FileInputStream(from 0th index of buffer to buffersize)
                bytesRead = fileInputStream.read(buffer,0,bufferSize);

                //loop repeats till bytesRead = -1, i.e., no bytes are left to read
                while (bytesRead > 0){
                    //write the bytes read from inputstream
                    dataOutputStream.write(buffer,0,bufferSize);
                    bytesAvailable = fileInputStream.available();
                    bufferSize = Math.min(bytesAvailable,maxBufferSize);
                    bytesRead = fileInputStream.read(buffer,0,bufferSize);
                }

                dataOutputStream.writeBytes(lineEnd);
                dataOutputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

                serverResponseCode = connection.getResponseCode();
                String serverResponseMessage = connection.getResponseMessage();

                Log.i(TAG, "Server Response is: " + serverResponseMessage + ": " + serverResponseCode);

                //response code of 200 indicates the server status OK
                if(serverResponseCode == 200){
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            textViewResponse.setText("File Upload completed.

");
                        }
                    });
                }

The video is successfully uploaded to server, its working. Now i want to send both the video and image url together, is that possible using my above code, any hint or idea is welcome.

It should be something like this:

FileInputStream imageStream = new FileInputStream(imageFile);
//Part below should go after the previous file boundary
 dataOutputStream.writeBytes("Content-Disposition: form-data; name=\"uploaded_image\";filename=\""+ imageFilePath + "\"" + lineEnd);
//Change this to whatever the content type is
 dataOutputStream.writeBytes("Content-Type: image/png"+lineEnd);

 dataOutputStream.writeBytes(lineEnd);

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

 bytesRead = imageStream.read(buffer,0,bufferSize);

 while (bytesRead > 0){
      dataOutputStream.write(buffer,0,bufferSize);
      bytesAvailable = imageStream.available();
      bufferSize = Math.min(bytesAvailable,maxBufferSize);
      bytesRead = imageStream.read(buffer,0,bufferSize);
}

dataOutputStream.writeBytes(lineEnd);
dataOutputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

In php the file should be available in the $_FILES['uploaded_image'] superglobal.

The multipart/form-data raw request body will look like this:

-----------------------boundary
Content-Disposition: form-data; name="uploaded_file"; filename="video.mp4"
Content-Type: video/mp4

[the bytes]
-----------------------boundary
Content-Disposition: form-data; name="uploaded_image"; filename="filename.png"
Content-Type: image/png

[the bytes]
-----------------------boundary--

Both can have name="uploaded_file" and in PHP your $_FILES['uploaded_file'] will contain details of both files similar to uploading multiple files. Forgot to add, only the last boundary should have the two extra hyphens suffixed.

add the into an object model and parse it to json using Gson object then send the POST request as a json:

Data data = new Data();
data.setImageUrl(imageUrl);
data.setVideoUrl(videoUrl);
String jsonObject = new Gson().toJson(data);
// send the jsonObject to server here