2016-03-27 30 views
0

böylece kullanıcı orada yüklediğinde oraya e-posta ve ürün anahtar kodu yüklediğinde kurulum yapmak istiyorum böylece PHP'de e-posta ile yeni bir dizin oluşturabilirim . Ardından videoyu ürün anahtar kodu olarak adlandırın. Diziyi yüklemeye çalışırken kodu kaldırırsam videoyu iyi yükleyebilirim.

Android kodu:

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 kodu:

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

gidecekseniz farklı şekilde yapmak ya da yanlış hiçbir şey yok etmek ise söyle lütfen! teşekkür ederim!

+0

[bu bir denediniz mi?] (Http://stackoverflow.com/a/34127008/1318946) –

+0

hayır ben değil aslında var! –

cevap

0

MultipartEntity'yi bir Listeyle cadıda kullanmayı deneyin, standart anahtar değeriyle metinden istediğinizi ekleyin. Bir post metodu Örneği İsterseniz adapte etmeye çalışın. Daha sonra postanızı bir AsynchTask'taki parametrelerinizle arayın. HttpClient kullanarak dikkat edin. onaylanmadı ve HTTpClientbuilder'in yerini aldı.

// 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; 
       } 
+0

, bu kod işe yarar çünkü isnt MultipartEntity kullanımdan kaldırıldı mı? –

+0

evet sorun olmadan API 23 kadar çalışır. – user2043602

+0

oh iyi API 23.0.2 kullanıyorum! : /, bu can sıkıcı çünkü bu anlayamadığım tek şey bu! –