2012-06-22 26 views
7

Tôi đang gặp sự cố trên một phần nhất định của dự án hiện tại và cảm thấy như tôi đang bị kẹt ngay bây giờ. Tôi đang cố gắng thực hiện tải lên video bằng dữ liệu biểu mẫu và đăng bài HTTP. Tôi cảm thấy như tôi đang đánh một bức tường trong việc hiểu giao thức HTTP và đặc biệt đa dữ liệu biểu mẫu.Video tải lên Android cho máy chủ từ xa bằng cách sử dụng dữ liệu biểu mẫu nhiều phần HTTP

Tôi có URL để tải video lên ở định dạng http://videoupload.thecompany.com/VideoApp.xml?method=upload&objectType=person&objectId=777777. Tôi cũng cần bao gồm tiêu đề, mô tả và videoFile. Đây có phải là "dữ liệu nhiều phần" không?

tôi đã cố gắng thích nghi với giải pháp này để đáp ứng nhu cầu của tôi Upload video from Android to server?, và thiết lập các dữ liệu thêm sau tất cả các conn.setRequestProperty khác() gọi như vậy:

conn.setRequestProperty("title", "video title"); 
conn.setRequestProperty("description", "video description"); 

Nhưng điều này không làm việc cho tôi . Có một nhận xét từ tác giả gốc của mã để thêm dữ liệu biểu mẫu nhiều phần khoảng 30 dòng sau, nhưng tôi không hiểu tại sao. Cảm ơn vì bất kì sự giúp đỡ.

Trả lời

19

Đây là giải pháp hai bước mà tôi đã đưa ra, phần lớn từ thông tin và liên kết được tìm thấy here. Giải pháp này dễ dàng hơn cho tôi để nắm bắt hơn phương thức upload2server() trong một số bài viết SO liên quan. Hy vọng điều này sẽ giúp người khác.

1) Chọn tệp video từ thư viện.

Tạo biến số private static final int SELECT_VIDEO = 3; - không quan trọng bạn sử dụng số nào, miễn là số bạn sử dụng để kiểm tra sau này. Sau đó, sử dụng ý định chọn video.

Intent intent = new Intent(); 
intent.setType("video/*"); 
intent.setAction(Intent.ACTION_GET_CONTENT); 
startActivityForResult(Intent.createChooser(intent,"Select a Video "), SELECT_VIDEO); 

Sử dụng onActivityResult() để bắt đầu phương thức uploadVideo().

public void onActivityResult(int requestCode, int resultCode, Intent data) { 

    if (resultCode == RESULT_OK) { 

     if (requestCode == SELECT_VIDEO) { 
      System.out.println("SELECT_VIDEO"); 
      Uri selectedVideoUri = data.getData(); 
      selectedPath = getPath(selectedVideoUri); 
      System.out.println("SELECT_VIDEO Path : " + selectedPath); 

      uploadVideo(selectedPath); 
     }  
    } 
} 

private String getPath(Uri uri) { 
    String[] projection = { MediaStore.Video.Media.DATA, MediaStore.Video.Media.SIZE, MediaStore.Video.Media.DURATION}; 
    Cursor cursor = managedQuery(uri, projection, null, null, null); 
    cursor.moveToFirst(); 
    String filePath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA)); 
    int fileSize = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE)); 
    long duration = TimeUnit.MILLISECONDS.toSeconds(cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION))); 


    //some extra potentially useful data to help with filtering if necessary 
    System.out.println("size: " + fileSize); 
    System.out.println("path: " + filePath); 
    System.out.println("duration: " + duration); 

    return filePath; 
} 

2) Tới http://hc.apache.org/downloads.cgi, tiến hành download HttpClient mới nhất jar, thêm nó vào dự án của bạn, và tải lên video bằng cách sử dụng phương pháp sau đây:

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 description of the video"); 

    MultipartEntity reqEntity = new MultipartEntity(); 
    reqEntity.addPart("videoFile", filebodyVideo); 
    reqEntity.addPart("title", title); 
    reqEntity.addPart("description", description); 
    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() 

Một khi bạn đã làm việc đó bạn sẽ có thể muốn đặt nó vào một chủ đề và thêm một hộp thoại tải lên, nhưng điều này sẽ giúp bạn bắt đầu. Làm việc cho tôi sau khi tôi đã thử phương thức upload2Server() không thành công. Điều này cũng sẽ làm việc cho hình ảnh và âm thanh với một số tinh chỉnh nhỏ.

+0

Rất tuyệt! Điều này có thể sử dụng được cho video có kích thước lớn không? Hoặc nó có thể được sử dụng cho các loại tệp khác không? –

+1

@AliBagheriShakib sẽ hoạt động tốt cho video lớn hơn. Có, nó có thể được sử dụng cho các clip âm thanh hoặc các loại tệp khác rất giống nhau. Hãy cho tôi biết nếu bạn có bất kỳ vấn đề. –

+1

Có @Kyle Clegg. Nó làm việc hoàn hảo cho tôi. Vấn đề của tôi được giải quyết bro :) Tôi cũng đăng kinh nghiệm của riêng mình: http://stackoverflow.com/q/23504191/2624611 –

Các vấn đề liên quan