0

I am uploading video from android app.i am able to add only video with this code,but i am giving request with video file,but its not working.i added code with this question,help me out from this.

 public int uploadFile(final String sourceFileUri) {
        HttpURLConnection conn = null;
        DataOutputStream dos = null;
        String lineEnd = "\r\n";

        String action ="action=videoUpload&id="+pHelper.getuser_id()+"&project_id="+Constants.proj_id+"&site_id="+count_et;
        Log.d("bf_encoding",action);
        // encode
        byte[] data = new byte[0];
        try {
            data = action.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String value = Base64.encodeToString(data, Base64.DEFAULT);

        String twoHyphens = "--";
        String boundary = "*****";
        String mm = "gokgo8gg4ko4gco4okg4ws4o04k44w0go4k";
        int bytesRead, bytesAvailable, bufferSize;
        byte[] buffer;
        int maxBufferSize = 1 * 1024 * 1024;
        File sourceFile = new File(sourceFileUri);
        if (!sourceFile.isFile()) {
            return 0;
        } else {
            try {
                FileInputStream fileInputStream = new FileInputStream(sourceFile);
                URL url = new URL("https://rahumanmusic.co.in/ar_site/api/video");
                Log.d("WebService", "url=" + url);
                // 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("Headers","X-API-KEY=gokgo8gwskkog4ko4gco4okgo04k44w0go4k");

                //  conn.getHeaderFieldDate("X-API-KEY", Long.parseLong(mm));
                conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
                Log.d("valuevalue",value);

                conn.setRequestProperty("video_upload", filepathUrl1.getName());
                conn.setRequestProperty("value", value);
                dos = new DataOutputStream(conn.getOutputStream());
                dos.writeBytes(twoHyphens + boundary + lineEnd);
                dos.writeBytes("Content-Disposition: form-data; name=\"video_upload\";filename=\"" + filepathUrl1.getName() + "\"" + 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);

                // Responses from the server (result)
                int responseCode = conn.getResponseCode();
                if (responseCode == HttpsURLConnection.HTTP_OK) {
                    String line;
                    BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                    while ((line = br.readLine()) != null) {

                        result += line;
                    }

                }
                fileInputStream.close();
                dos.flush();
                dos.close();
            } catch (MalformedURLException ex) {
                ex.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
            Log.d("ssk", "result" + result);
            return serverResponseCode;
        } // End else block
    }

In above code,converted base 64 string as value not passing in request. i am stuck in this task,help me out guys

Raja Sankar
  • 39
  • 10
  • @Kyle Clegg Thank you very much..happy coding.https://stackoverflow.com/questions/11164398/android-upload-video-to-remote-server-using-http-multipart-form-data – Raja Sankar Nov 24 '17 at 19:53

1 Answers1

1

Use this library for multipart requests, i-e image, video or any other data for POST request

Mubashar Javed
  • 749
  • 1
  • 5
  • 12