Video file upload protocol analysis in Android
L run the web application on the server,
L upload a video file, capture data packets with httpwatch, and explain the following:
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 ... ----------------------------- 7dbcb1c000062 -- |
Note: explain in detail how to use the split line, including the definition of the split line.
Definition:
Boundary = --------------------------- 7db4e3237099a
Definition and use of split lines
--------------------------- 7db4e3237099a // Definition ----------------------------- 7db4e3237099a // entity parameter split line. Two more "-" parameters are added before the split line. ----------------------------- 7db4e3237099a -- // The entity parameter end split line, followed by two more "-" |
Description of the type of the uploaded file (see the dictionary in the tomcat \ conf \ web. xml file)
The following describes the basic information about the object to be uploaded in the object content:
Content-Disposition: form-data; name = "video"; filename = "F: \ 2011_Android \ 14 \ YMGM. rm"
Content-Type: application/octet-stream
Note that the number of carriage returns and line breaks is clear.
Implementation of client applications
1. Description: FormFile
2. ethttprequester
Copy the tool class used above to the Project
3. Specific operations
Add permission
<Uses-permission android: name ="Android. permission. INTERNET"/> <! -- Create and delete file permissions in SDCard --> <Uses-permission android: name ="Android. permission. MOUNT_UNMOUNT_FILESYSTEMS"/> <! -- Write data to SDCard --> <Uses-permission android: name ="Android. permission. WRITE_EXTERNAL_STORAGE"/> |
Layout: Added two UI components.
<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: Added the saveVideo method.
Public static booleanSaveVideo (String name, String timelength, File file)ThrowsException { String path = "Http: // 192.168.1.102: 8080/videoweb/video/manage. do "; Map <String, String> params =NewHashMap <String, String> (); Params.Put("Name", name ); Params. put ("timelength", timelength ); Params. put ("method", "save "); FormFile formfile =NewFormFile (file. getName (), file, "video", "application/octet-stream "); ReturnSocketHttpRequester.Post(Path, params, formfile ); } |
Activity
PackageCn. class3g. video; ... Public classVideoClientActivityExtendsActivityImplementsOnClickListener { EditText nameEt, timeLengthEt, fileEt; Button saveBtn; Protected voidOnCreate (Bundle savedInstanceState ){ Super. OnCreate (savedInstanceState ); This. SetContentView (R. layout.Save_video_info); FindViews (); } Private voidFindViews (){ 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 voidOnclick (view v ){ String videoname = nameet. gettext (). tostring (); String timelength = timelengthet. gettext (). tostring (); String videofile = fileet. gettext (). tostring (); Try{ File file =NewFile (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 (); } } } |
Test:
First, drag the video files used for testing into the sd card of the simulator.
Run the program, execute the upload, and check whether the server has uploaded files.