0

so i want to have it set up so when the user uploads there video there email and product key code go with it so in the PHP i can create a new directory with there email. Then name the video as the product key code. I can upload the video fine if i remove the code trying to upload the string.

Android code:

try{
                FileInputStream fileInputStream = new FileInputStream(sourceFile);
                URL url = new URL(UploadVideo_URL);
                conn = (HttpURLConnection) url.openConnection();
                conn.setDoInput(true);
                conn.setDoOutput(true);
                conn.setUseCaches(false);
                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("myFile", selectedPath);
                dos = new DataOutputStream(conn.getOutputStream());




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

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





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


                bytesAvailable = fileInputStream.available();
                Log.i("Huzza", "Initial .available : " + bytesAvailable);

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

                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);
                }

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

                serverResponseCode = conn.getResponseCode();

                fileInputStream.close();
                dos.flush();
                dos.close();

            } catch (Exception e) {
                e.printStackTrace();
                return "Product upload failed";
            }

PHP code:

<?php

$ProductOwnerEmail = $_POST['ProductOwnerEmail'];
$ProductKeyCode = $_POST['ProductKeyCode'];

$NewDirectory = "/var/www/html/ProductVideos/" . $ProductOwnerEmail;

mkdir($NewDirectory, 0777, true);


if($_SERVER['REQUEST_METHOD']=='POST'){
$file_name = $_FILES['myFile']['name'];
$file_size = $_FILES['myFile']['size'];
$file_type = $_FILES['myFile']['type'];
$temp_name = $_FILES['myFile']['tmp_name'];

$location = "/var/www/html/ProductVideos/$ProductOwnerEmail/" . $ProductKeyCode;

move_uploaded_file($temp_name, $location);
}
?>

If theres a different way to do it or theres nothing wrong please tell me! thank you!

Lazar Kukolj
  • 696
  • 3
  • 15
  • 43

1 Answers1

0

try to use MultipartEntity with a List in witch you add what you want of text with the standard key value. an Example of a post methode Try to adapte it if you want. then call your post with the your parameters in an AsynchTask for example. take care using HttpClient. it was deprecated and replaced HTTpClientbuilder.

    // traitement 



nameValuePairs.add(new BasicNameValuePair(Document, imagepath));
   nameValuePairs.add(new BasicNameValuePair(TITLE, "toto"));
   nameValuePairs.add(new BasicNameValuePair(NAME, value));
   nameValuePairs.add(new BasicNameValuePair(FIRSTNAME, value1));
   nameValuePairs.add(new BasicNameValuePair(DEscription, value3));

        public String post(String url, List<NameValuePair> nameValuePairs) {
                    String result ="fail";
                    HttpClient httpClient = new DefaultHttpClient();
                    HttpContext localContext = new BasicHttpContext();
                    HttpPost httpPost = new HttpPost(url);
                    try {
                        MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null, Charset.forName("UTF-8"));
                        for (int index = 0; index < nameValuePairs.size(); index++) {
                            if (nameValuePairs.get(index).getName().equalsIgnoreCase(Document)) {
            // If the key equals to "image", we use FileBody to transfer the data
                                entity.addPart(nameValuePairs.get(index).getName(), new FileBody(new File(nameValuePairs.get(index).getValue())));
                            } else {
                                Log.d(TAG, "getname:" + nameValuePairs.get(index).getName());
                                Log.d(TAG, "getvalue:" + nameValuePairs.get(index).getValue());
            // Normal string data
                                if (nameValuePairs.get(index).getValue() != null) {
                                    entity.addPart(nameValuePairs.get(index).getName(), new StringBody(nameValuePairs.get(index).getValue()));
                                } else {
            // traitement
                                    try {
                                        runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {

                                               //traitment
                                            }
                                        });
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }
                        }
                        httpPost.setEntity(entity);
                        HttpResponse response = httpClient.execute(httpPost, localContext);
                        InputStream inputStream = response.getEntity().getContent();
                        System.out.println(response.getStatusLine().getStatusCode());
                        Log.d("response=", String.valueOf(response.getStatusLine().getStatusCode()));

                        if (String.valueOf(response.getStatusLine().getStatusCode()).contains("200")) {

                           result = "sucess";
                        } else {
                            result = "fail";
                        }
                       Log.d("response=" , String.valueOf(response.getStatusLine()));
                    } catch (IOException e) {
                        Log.e(TAG, "error upload file", e);
                    }
                    return result;
                }
user2043602
  • 237
  • 2
  • 4
  • 15
  • does this code work because isnt MultipartEntity deprecated? – Lazar Kukolj Mar 27 '16 at 06:33
  • oh well i am using API 23.0.2! :/, this is annoying because this is the only thing i cant figure out! – Lazar Kukolj Mar 27 '16 at 06:39
  • if i can get the video to upload and my variables i am set! – Lazar Kukolj Mar 27 '16 at 06:39
  • it is ok for the MultipartEntity. for httpClient use the httpclientbuilder – user2043602 Mar 27 '16 at 06:41
  • i add the _MultipartEntity_ line in my code and it doesnt work! BTW my Try block is in a **AsyncTask** – Lazar Kukolj Mar 27 '16 at 06:43
  • dependencies { compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , add this to your gradle : version: '4.3.5.1' compile('org.apache.httpcomponents:httpmime:4.3') { exclude module: "httpclient" } } – user2043602 Mar 27 '16 at 06:45
  • not yet but the new class to change it is httpclientBuilder. it was replaced because of battery use. you can still use it you have only to add a dependencies in your gradel for the future version – user2043602 Mar 27 '16 at 06:52