2016-02-27 78 views
0

我目前有代碼將視頻上傳到服務器,但我不知道我將如何將字符串與視頻一起添加到服務器。 我想發送視頻後跟一個字符串發送到上傳。android上傳視頻和字符串到服務器

public String uploadVideo(String file) { 

    String fileName = file; 
    HttpURLConnection conn = null; 
    DataOutputStream dos = null; 
    String lineEnd = "\r\n"; 
    String twoHyphens = "--"; 
    String boundary = "*****"; 
    int bytesRead, bytesAvailable, bufferSize; 
    byte[] buffer; 
    int maxBufferSize = 1 * 1024 * 1024; 

    File sourceFile = new File(file); 
    if (!sourceFile.isFile()) { 
     Log.e("Huzza", "Source File Does not exist"); 
     return null; 
    } 

    try { 
     FileInputStream fileInputStream = new FileInputStream(sourceFile); 
     URL url = new URL(UPLOAD_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", fileName); 
     dos = new DataOutputStream(conn.getOutputStream()); 

     dos.writeBytes(twoHyphens + boundary + lineEnd); 
     dos.writeBytes("Content-Disposition: form-data; name=\"myFile\";filename=\"" + fileName + "\"" + 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 (MalformedURLException ex) { 
     ex.printStackTrace(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    if (serverResponseCode == 200) { 
     StringBuilder sb = new StringBuilder(); 
     try { 
      BufferedReader rd = new BufferedReader(new InputStreamReader(conn 
        .getInputStream())); 
      String line; 
      while ((line = rd.readLine()) != null) { 
       sb.append(line); 
      } 
      rd.close(); 
     } catch (IOException ioex) { 
     } 
     return sb.toString(); 
    }else { 
     return "Could not upload"; 
    } 
} 

Uploadactivity

@Override 
     protected String doInBackground(Void... params) { 
      Upload u = new Upload(); 
      String msg = u.uploadVideo(selectedPath); 
      return msg; 

     } 
    } 
    UploadVideo uv = new UploadVideo(); 
    uv.execute(); 

回答

-1

添加

conn.setRequestProperty("stringName", string); 

畢竟conn.setRequestProperty的 然後在服務器上,確保您recive它(在PHP這是 $varname = $_POST['stringName'];

+0

謝謝我想通過DoI發送字符串nBackground方法,你知道我可以通過uploadvideo(); – user3541743

相關問題