Android開發之路十四———–Android中實現視頻檔案的上傳

來源:互聯網
上載者:User
Android中實現視頻檔案的上傳協議分析

l  運行伺服器端web應用,

l  上傳視頻檔案,同時用httpwatch捕獲資料包,並進行講解:

 

 
 

POST /videoweb/video/manage.do HTTP/1.1

 

Accept: image/gif, image/jpeg,  image/pjpeg, image/pjpeg, application/x-shockwave-flash,  application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword,  */*

 

Referer:  http://192.168.1.102:8080/videoweb/

 

Accept-Language:  zh-cn,en-GB;q=0.5

 

User-Agent: Mozilla/4.0 (compatible; MSIE  8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727)

 

Content-Type: multipart/form-data;  boundary=---------------------------7db4e3237099a

 

Accept-Encoding: gzip, deflate

 

Host: 192.168.1.102:8080

 

Content-Length: 3224599

 

Connection: Keep-Alive

 

Cache-Control: no-cache

 

Cookie:  JSESSIONID=D677BD29B65CD7D9A973767BEB945CCF

 

 

 

-----------------------------7db4e3237099a

 

Content-Disposition: form-data;  name="method"

 

 

 

save

 

-----------------------------7db4e3237099a

 

Content-Disposition: form-data;  name="name"

 

 

 

YMGM

 

-----------------------------7db4e3237099a

 

Content-Disposition: form-data;  name="timelength"

 

 

 

1

 

-----------------------------7db4e3237099a

 

Content-Disposition: form-data;  name="video"; filename="F:\2011_Android\14\YMGM.rm"

 

Content-Type: application/octet-stream

 

 

 

 

-----------------------------7dbcb1c10762--

 

 

 

 

 

 

注意:詳細解釋分割線的使用,包括分割線的定義,使用

定義:

boundary=---------------------------7db4e3237099a

分割線的定義和使用

 
 

---------------------------7db4e3237099a   //定義

 

-----------------------------7db4e3237099a   //實體參數分割線,前面比定義時多了兩個“-”

 

-----------------------------7db4e3237099a--   //實體參數結束分割線,後面比定義時多了兩個“-”

 

 

上傳檔案的類型描述(可以參看tomcat\conf\web.xml檔案中的字典)

 

下面是關於實體內容中上傳檔案的基本資料描述:

 

Content-Disposition: form-data;name="video"; filename="F:\2011_Android\14\YMGM.rm"

Content-Type: application/octet-stream

 

 

注意數清楚斷行符號和換行的個數

 

 

用戶端應用的實現

1、  講解工具類FormFile

2、  講解工具類SocketHttpRequester

 

將上面用到的工具類複製到工程中

 

3、  具體操作

 

添加許可權

 
 

<uses-permission android:name="android.permission.INTERNET" />

 

<!-- 在SDCard中建立與刪除檔案許可權 -->

 

<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"  />

 

<!-- 往SDCard寫入資料許可權 -->

 

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"  />

 

 

 

布局:增加兩個UI組件

 
 

    <TextView

 

        android:layout_width="fill_parent"

 

        android:layout_height="wrap_content"

 

        android:text="@string/videoFile"  />

 

 

 

    <EditText

 

        android:id="@+id/videoFileEt"

 

        android:layout_width="fill_parent"

 

        android:layout_height="wrap_content"

 

        android:text="YMGM.rm" />

 

 

VideoService:增加方法saveVideo

 
 

public static boolean saveVideo(String name, String timelength,

 

 File file) throws  Exception{

 

    String path =

 

"http://192.168.1.102:8080/videoweb/video/manage.do";

 

    Map<String, String> params = new  HashMap<String, String>();

 

    params.put("name", name);

 

    params.put("timelength", timelength);

 

    params.put("method", "save");

 

    FormFile formfile = new  FormFile(file.getName(), file, "video", "application/octet-stream");

 

    return SocketHttpRequester.post(path,  params, formfile);

 

}

 

 

activity

 
 

package cn.class3g.video;

 

 

public class VideoClientActivity extends Activityimplements  OnClickListener {

 

 

 

    EditText nameEt, timeLengthEt, fileEt;

 

    Button saveBtn;

 

 

 

    protected void onCreate(Bundle  savedInstanceState) {

 

       super.onCreate(savedInstanceState);

 

       this.setContentView(R.layout.save_video_info);

 

       findViews();

 

    }

 

 

 

    private void findViews() {

 

       nameEt = (EditText) this.findViewById(R.id.nameEt);

 

       timeLengthEt = (EditText) this.findViewById(R.id.timeLengthEt);

 

       fileEt = (EditText) this.findViewById(R.id.videoFileEt);

 

       saveBtn = (Button) this.findViewById(R.id.saveBtn);

 

 

 

       saveBtn.setOnClickListener(this);

 

    }

 

 

 

    public void onClick(View v) {

 

 

 

       String videoName = nameEt.getText().toString();

 

       String timeLength = timeLengthEt.getText().toString();

 

       String videoFile = fileEt.getText().toString();

 

 

 

       try {

 

           File file = new File(Environment.getExternalStorageDirectory(),

 

                  videoFile);

 

           if (VideoService.saveVideo(videoName,  timeLength, file)) {

 

              Toast.makeText(this, R.string.success,  1).show();

 

           } else {

 

              Toast.makeText(this, R.string.error, 1).show();

 

           }

 

 

 

       } catch (Exception e) {

 

           Log.e("TAG", e.toString());

 

           Toast.makeText(this, R.string.error, Toast.LENGTH_SHORT).show();

 

       }

 

    }

 

}

 

 

測試:

先把測試用的視頻檔案拖入模擬器的sd card中

運行程式,執行上傳,查看伺服器是否有被上傳檔案

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.