2015-05-18 18 views
0

我正在嘗試基於視頻的應用程序。我使用下面的方法上傳視頻文件,當我發送大尺寸的視頻文件時,它有更多的時間上傳。所以我需要壓縮數據,並且視頻文件大小應該小於30 MB。我英文不好,原諒我。任何人都可以給我解決我的問題嗎?如何在Android上更快地上傳視頻

private void doFileUpload(){ 
    HttpURLConnection conn = null; 
    DataOutputStream dos = null; 
    DataInputStream inStream = null; 
    String lineEnd = "rn"; 
    String twoHyphens = "--"; 
    String boundary = "*****"; 
    int bytesRead, bytesAvailable, bufferSize; 
    byte[] buffer; 
    int maxBufferSize = 1*1024*1024; 
    String responseFromServer = ""; 
    String urlString = "http://mywebsite.com/upload_audio.php"; 
    try 
    { 
    //------------------ CLIENT REQUEST 
    FileInputStream fileInputStream = new FileInputStream(new File(selectedPath)); 
    // open a URL connection to the Servlet 
    URL url = new URL(urlString); 
    // Open a HTTP connection to the URL 
    conn = (HttpURLConnection) url.openConnection(); 
    // Allow Inputs 
    conn.setDoInput(true); 
    // Allow Outputs 
    conn.setDoOutput(true); 
    // Don't use a cached copy. 
    conn.setUseCaches(false); 
    // Use a post method. 
    conn.setRequestMethod("POST"); 
    conn.setRequestProperty("Connection", "Keep-Alive"); 
    conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary); 
    dos = new DataOutputStream(conn.getOutputStream()); 
    dos.writeBytes(twoHyphens + boundary + lineEnd); 
    dos.writeBytes("Content-Disposition: form-data; name="uploadedfile";filename="" + selectedPath + """ + 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); 
    // close streams 
    Log.e("Debug","File is written"); 
    fileInputStream.close(); 
    dos.flush(); 
    dos.close(); 
    } 
    catch (MalformedURLException ex) 
    { 
     Log.e("Debug", "error: " + ex.getMessage(), ex); 
    } 
    catch (IOException ioe) 
    { 
     Log.e("Debug", "error: " + ioe.getMessage(), ioe); 
    } 
    //------------------ read the SERVER RESPONSE 
    try { 
      inStream = new DataInputStream (conn.getInputStream()); 
      String str; 

      while ((str = inStream.readLine()) != null) 
      { 
       Log.e("Debug","Server Response "+str); 
      } 
      inStream.close(); 

    } 
    catch (IOException ioex){ 
     Log.e("Debug", "error: " + ioex.getMessage(), ioex); 
    } 
    } 
+0

可能重複(http://stackoverflow.com/questions/5017093/upload-video-from-android-to-server) –

回答

0

您可以嘗試HttpClient jar下載最新的HttpClient的罐子,把它添加到您的項目,並使用以下方法上傳的視頻:[?上傳視頻從機器人到服務器]

private void uploadVideo(String videoPath) throws ParseException, IOException { 

HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost(YOUR_URL); 

FileBody filebodyVideo = new FileBody(new File(videoPath)); 
StringBody title = new StringBody("Filename: " + videoPath); 
StringBody description = new StringBody("This is a video of the agent"); 
StringBody code = new StringBody(realtorCodeStr); 

MultipartEntity reqEntity = new MultipartEntity(); 
reqEntity.addPart("videoFile", filebodyVideo); 
reqEntity.addPart("title", title); 
reqEntity.addPart("description", description); 
reqEntity.addPart("code", code); 
httppost.setEntity(reqEntity); 

// DEBUG 
System.out.println("executing request " + httppost.getRequestLine()); 
HttpResponse response = httpclient.execute(httppost); 
HttpEntity resEntity = response.getEntity(); 

// DEBUG 
System.out.println(response.getStatusLine()); 
if (resEntity != null) { 
System.out.println(EntityUtils.toString(resEntity)); 
} // end if 

if (resEntity != null) { 
    resEntity.consumeContent(); 
} // end if 

httpclient.getConnectionManager().shutdown(); 
} // end of uploadVideo() 
+0

感謝您的回覆,我會嘗試這 –

+0

肯定兄弟,快樂編碼....讓我知道如果你發現任何問題 –

+0

好的。作品不錯:) –